Here is what I vainly tried to do:
int x = 0;
PreparedStatement ps;
String query = "INSERT INTO staff VALUES (?,?,?,?,?,?)";
try {
ps = ConnectDb.getConnection().prepareStatement(query);
x ;
//ps.setInt(1, x);
ps.setString(2, nameTxt.getText());
ps.setString(3, emailTxt.getText());
ps.setString(4, address.getText());
ps.setString(5, phone.getText());
ps.setString(6, genderCb.getSelectedItem().toString());
ps.execute();
if (x > 0) {
JOptionPane.showMessageDialog(null, "Staff added successfully");
}
} catch (Exception ex) {
JOptionPane.showMessageDialog(null,ex,"Error: Staff not added!",JOptionPane.ERROR_MESSAGE);
frm.setVisible(true);
}
}
}
How can I continuously add record to the database without getting any exception on the auto-incremental primary key (e.g StaffID)?
CodePudding user response:
Create a sequence in database and pass that sequence name in insert query.
CodePudding user response:
Thank you anyway, I solved it by removing the x variable and the first parameter in my query.