Issue
I am trying to insert data into a CUSTOMER table.
private void c_enterActionPerformed(java.awt.event.ActionEvent evt) {
String insertSQL = "insert into CUSTOMER(CUST_ID, CUST_NIC, CUST_FNAME,CUST_LNAME, CUST_EMAIL, CUST_ADDRESS, CUST_PHONE, CUST_IMG) values(?,?,?,?,?,?,?,?)";
try{
ps = con.prepareStatement(insertSQL);
ps.setString(1,c_id_text.getText());
ps.setString(2,c_nic_text.getText());
ps.setString(3,c_fname_text.getText());
ps.setString(4,c_lname_text.getText());
ps.setString(5,c_email_text.getText());
ps.setString(6,c_address_text.getText());
ps.setString(7,c_phone_text.getText());
ps.setString(8,img_path_txt.getText());
ps.execute();
JOptionPane.showMessageDialog(null, "New Customer Inserted\nCongratulations!");
c_id_text.setText("");
c_nic_text.setText("");
c_fname_text.setText("");
c_lname_text.setText("");
c_email_text.setText("");
c_address_text.setText("");
c_phone_text.setText("");
img_path_txt.setText("");
updateTable();
}catch(Exception e){
JOptionPane.showMessageDialog(null, "Insertion error: "+e);
}
}
The table was created using:
CREATE TABLE CUSTOMER(
CUST_ID INT PRIMARY KEY GENERATED ALWAYS AS IDENTITY (START WITH 1, INCREMENT BY 1),
CUST_NIC VARCHAR(14),
CUST_FNAME VARCHAR(20),
CUST_LNAME VARCHAR(25),
CUST_EMAIL VARCHAR(45),
CUST_ADDRESS VARCHAR(60),
CUST_PHONE INTEGER,
CUST_IMG VARCHAR(100));
SELECT * FROM AKASH.CUSTOMER FETCH FIRST 100 ROWS ONLY;
I have disabled the CUST_ID text as shown. It's telling me "Attempt to modify an identity column 'CUST_ID'.
Now, for claritfication: I know what is happening. But I don't know how to fix it.
I tried to remove ps.setString(1,c_id_text.getText());
, that didn't work.
I also tried to remove CUST_ID from String insertSQL...
, but to no avail.
If I try to input data from the SQL using "Insert row" button, it works and the CUST_ID column displays ""
Solution
Found my mistake. Solved after commenting out
ps.setString(1,c_id_text.getText());
and c_id_text.setText("");
as well as removing CUST_ID form the insertSQL line of code.
Answered By - Akash Digumber
Answer Checked By - Marilyn (JavaFixing Volunteer)