我想检查我的ID是否已经存在:
sql2 = "SELECT stid FROM student";
stmt.executeUpdate(sql2);
rs = stmt.executeQuery(sql2);
while(rs.next()){
id = rs.getString("stid");
if(tf_insert1.equals(id)){
JOptionPane.showMessageDialog(null, "ID is already exists");
id = tf_insert1.getText();
name = tf_insert2.getText();
address = tf_insert3.getText();
gender = tf_insert4.getText();
ip = tf_insert5.getText();
tf_insert1.setText(id);
tf_insert2.setText(name);
tf_insert3.setText(address);
tf_insert4.setText(gender);
tf_insert5.setText(ip);
任何解决这个问题的想法???
答案 0 :(得分:0)
请检查一下。我修改了两部分的代码。
checking the id exists or not
insert section
的另一个。 希望它有助于解决您的问题。
private void yourFunction(java.awt.event.FocusEvent evt) {
DBUtil util = new DBUtil();
try {
Connection con = util.getConnection();
PreparedStatement stmt = con.prepareStatement(
"SELECT stid FROM student where stid = ?");
stmt.setLong(1, Long.parseLong(stid.getText()));
ResultSet rs=stmt.executeQuery();
bool recordAdded = false;
while(!rs.next()){
recordAdded = true;
}
if( recordAdded ){
// your code for insertion.
}else{
JOptionPane.showMessageDialog(null, "ID is already exists");
}
} catch (Exception ex) {
Logger.getLogger(DATAENTRY.class.getName()).log(Level.SEVERE, null, ex);
}