integrity constraint violation: NOT NULL check constraint

放肆的年华 提交于 2020-02-02 13:19:07

问题


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

This code is connecting to database but whenever i insert an attribute, it gives the above mentioned error.

this database is being used by two classes. first class will insert the class name into the ClassName column, then i will click on Add attribute button to open the above mentioned class. when i insert attribute in this and press the "Add" button, it gives the following error:

net.ucanaccess.jdbc.UcanaccessSQLException: UCAExc:::3.0.7 integrity constraint violation: NOT NULL check constraint; SYS_CT_10359 table: TABLE1 column: CLASSNAME


回答1:


Looks like the table TABLE1 has a NOT NULL constraint on the column CLASSNAME.

It means, you cannot insert a new row into the table without value for the CLASSNAME column.

After inserting the CLASSNAME, you should be updating the same row with the Attributes.

Your current code tries to insert a new row with only Attributes, hence the constraint throws an error.

Your update statement should be as below.

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

Also, check if the table has any other constraints (including a unique primary key )



来源:https://stackoverflow.com/questions/40136185/integrity-constraint-violation-not-null-check-constraint

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!