繁体   English   中英

违反完整性约束:NOT NULL检查约束

[英]integrity constraint violation: NOT NULL check constraint

ResultSet rs;

PreparedStatement ps;

Connection con;

public Attribute() {


    try{

         con = DriverManager.getConnection("jdbc:ucanaccess://D:/programming/myassignment/Database1.accdb");
        System.out.println("Java is now connected to database");


    }catch(Exception ex){
        System.out.println(ex);
    }

JButton btnAdd = new JButton("Add");
    btnAdd.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {


  try{

PreparedStatement pstmt = (PreparedStatement) con.prepareStatement("insert into table1(Attributes) values(?)");
                   pstmt.setString(1, textField.getText());
                   pstmt.executeUpdate();
                   pstmt.close();





            }catch (Exception ex){
                System.out.println(ex);

            }

        }
    });
    btnAdd.setBounds(152, 203, 89, 23);
    contentPane.add(btnAdd);

这段代码正在连接到数据库,但是每当我插入一个属性时,它就会出现上述错误。

该数据库由两个类使用。 第一个班级将班级名称插入ClassName列,然后单击“添加属性”按钮以打开上述班级。 当我在其中插入属性并按下“添加”按钮时,出现以下错误:

net.ucanaccess.jdbc.UcanaccessSQLException:UCAExc ::: 3.0.7完整性约束违反:NOT NULL检查约束; SYS_CT_10359表:TABLE1列:CLASSNAME

看起来表TABLE1在列CLASSNAME上具有NOT NULL约束。

这意味着,如果没有CLASSNAME列的值,则不能在表中插入新行。

插入CLASSNAME后,您应该使用Attributes更新同一行。

您当前的代码尝试插入仅包含属性的新行,因此约束引发错误。

您的更新声明应如下所示。

PreparedStatement pstmt = (PreparedStatement) con.prepareStatement("update table1 set Attributes = ? where CLASSNAME = ?");
pstmt.setString(1, textField.getText());
pstmt.setString(2, "Previously Inserted Classname");

另外,检查表是否还有其他约束(包括唯一的主键)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM