简体   繁体   中英

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

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

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 )

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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