Java com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException:列“hourId”不能为空

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20070494/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 22:52:45  来源:igfitidea点击:

com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Column 'hourId' cannot be null

javamysqljdbc

提问by user2951465

My project is faculty allocation. when i try to insert data, it shows the following exception.

我的项目是师资分配。当我尝试插入数据时,它显示以下异常。

"com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: 
Column 'hourId' cannot be null"

Can anyone help me how to overcome this problem? Please provide sample code to avoid this exception.

谁能帮我解决这个问题?请提供示例代码以避免此异常。

My coding is

我的编码是

<%
    Connection con = null;
    String StaffName = request.getParameter("StaffName");
   // String subcode = request.getParameter("subcode");
    String hourId = request.getParameter("hourId");
    String daysId = request.getParameter("daysId");
    String date = request.getParameter("date");


  //String queryText = "insert into tblstaffallocation (StaffName, subcode,hourId, daysId, date) values('"+StaffName+"','"+subcode+"','+hourId+','+daysId+','+date+')";

    try {
          Class.forName("com.mysql.jdbc.Driver");
          con = DriverManager.getConnection("jdbc:mysql://localhost:3306/StaffAllocation","root","success");

       // PreparedStatement stat = con.PrepareStatement();
        String updateString ="INSERT INTO tblstaffallocation (StaffName,hourId,daysId,date) VALUES (?,?,?,?)";

        PreparedStatement preparedStatement = con.prepareStatement(updateString);


        preparedStatement.setString(1, StaffName);
        preparedStatement.setString(2, hourId);
        preparedStatement.setString(3, daysId);
        preparedStatement.setString(4, date);
        preparedStatement.executeUpdate();
        //int rst = stat.executeUpdate("insert into tblstaffallocation ('StaffName','hourId', 'daysId', 'date') values('"+StaffName+"','"+hourId+"','"+daysId+"','"+date+"')");

        %>
        <table cellpadding="4" border="1" cellspacing="4" align="center">
        <th>StaffName</th><th>hourId</th><th>daysId</th><th>date</th>
        <%
        Statement st=con.createStatement();
        ResultSet rs=st.executeQuery("select * from tblstaffallocation");
        while(rs.next()){
            rs.getString(1);
            rs.getString(2);
            rs.getString(3);
            rs.getString(4);

        }
        } catch (Exception e) { 
        out.print(e);

    }

回答by Oscar Pérez

The error says that hourIdhas got a NULLvalue, so you must avoid it. For exemple, you could use:

错误说hourId有一个NULL值,所以你必须避免它。例如,您可以使用:

String hourId = request.getParameter("hourId");
if (hourId==null)
    hourId="";

As long as the column accepts an empty string. If not, you must change the table definition to allow nulls:

只要该列接受空字符串。如果没有,您必须更改表定义以允许空值:

 ALTER TABLE tblstaffallocation
   MODIFY COLUMN hourId INTEGER NULL;

回答by Balaji Reddy

This is good practice to have primary key per table and key property of the primary key is not allowing null values... ORM/Hibernate strictly follows this principle where each entity/persistent bean should have primary key.. Answer to your question is you are trying to insert null value to a primary\non nullable column .... so you are getting the exception... Below code is not good practice. If you intentionally made your column nullable then obviously no point trying to insert null/empty value.. Its better to design your data model before actually starting implementing the application.

这是一个很好的做法,每个表都有主键,主键的键属性不允许空值...... ORM/Hibernate 严格遵循这个原则,其中每个实体/持久 bean 都应该有主键.. 你的问题的答案是你正在尝试将空值插入到主\不可为空的列中......所以你得到了异常......下面的代码不是一个好习惯。如果你故意让你的列可以为空,那么尝试插入空/空值显然没有意义。最好在实际开始实现应用程序之前设计你的数据模型。

if (hourId==null)
    hourId="";

Hope this will be helpful...

希望这会有所帮助...

Cheers!

干杯!

回答by jagdeeshGughalot

Let us check scenario of hibernate.
Solution :

让我们检查一下休眠的场景。
解决方案 :

1.check entity mapping with table.
2. check the primary key and foreign key relationship.
3. primary key cannot be "not null" in table, it can be null in other table.

1.检查实体与表的映射。
2.检查主键和外键的关系。
3.主键在表中不能为“not null”,在其他表中可以为null。

回答by Bhumi Nandan Chettri

column hourId is set to not null while creating the table and you are trying to update with null value. you can alter table to accept hourId as null or set some default value for hourId

创建表时,列 hourId 设置为非空,并且您尝试使用空值进行更新。您可以更改表以接受 hoursId 为 null 或为 hoursId 设置一些默认值