首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException:列“hourId”不能为null

com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException:列“hourId”不能为null
EN

Stack Overflow用户
提问于 2013-11-19 19:37:10
回答 3查看 23.4K关注 0票数 2

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

代码语言:javascript
复制
"com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: 
Column 'hourId' cannot be null"

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

我的代码是

代码语言:javascript
复制
<%
    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);

    }
EN

回答 3

Stack Overflow用户

发布于 2013-11-19 19:45:53

该错误说明hourId的值为NULL,因此必须避免出现该错误。例如,您可以使用:

代码语言:javascript
复制
String hourId = request.getParameter("hourId");
if (hourId==null)
    hourId="";

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

代码语言:javascript
复制
 ALTER TABLE tblstaffallocation
   MODIFY COLUMN hourId INTEGER NULL;
票数 0
EN

Stack Overflow用户

发布于 2013-11-19 23:55:11

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

代码语言:javascript
复制
if (hourId==null)
    hourId="";

希望这能对你有所帮助。

干杯!

票数 0
EN

Stack Overflow用户

发布于 2018-07-09 13:46:20

在创建表时,列hourId被设置为not null,而您正在尝试使用NULL值进行更新。您可以更改table以接受hourId为null,或者为hourId设置一些缺省值

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/20070494

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档