【问题标题】:com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Column 'hourId' cannot be nullcom.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException:列 'hourId' 不能为空
【发布时间】:2013-12-02 22:38:47
【问题描述】:

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

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

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

我的编码是

<%
    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);

    }

【问题讨论】:

  • 将 column_name 更改为可为空
  • 我试过了,当我将数据插入表中时,编码工作。但是当我从 tblstaffallocation 中选择 * 时。除人员名称外,所有输入的值都存储为空值。
  • @user2951465 hourid 列不接受 null。确保 request.getParameter("hourId");不返回 null
  • @lucas 我不明白你,我应该提供什么名字?请更具体
  • @user2951465 抱歉,我的意思是 hourId :)

标签: java mysql jdbc


【解决方案1】:

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

【讨论】:

    【解决方案2】:

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

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

    【讨论】:

      【解决方案3】:

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

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

      希望这会有所帮助...

      干杯!

      【讨论】:

      • 但如果我想在数据库中保留以前的值而不接触它。我该怎么做呢
      • 如果之前的值为 null,如果它给出 SQLException,则必须插入零,修改表的列以接受字符串,或将列默认设置为 0。
      【解决方案4】:

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

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

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

       ALTER TABLE tblstaffallocation
         MODIFY COLUMN hourId INTEGER NULL;
      

      【讨论】:

      • 我试过你所说的,它显示“java.sql.SQLException: Incorrect integer value: '' for column 'hourId' at row 1”异常
      • 列是int,因此它需要一个整数作为输入。如果你给 "" 作为参数,它会返回一个 SQL 异常。
      • 所以我必须替换什么?请有点困惑
      • 您想在hourId 中允许空值吗?如果是这样,请尝试给定的 SQL。如果没有,当请求参数为空时,您必须决定在表字段中需要什么。这将取决于您的程序...我们无法决定。通常使用0 来表示“无值”字段,但这取决于您使用它的目的。
      猜你喜欢
      • 2015-12-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-07-15
      • 1970-01-01
      • 2015-09-09
      • 1970-01-01
      相关资源
      最近更新 更多