2 回答
TA贡献1833条经验 获得超4个赞
将此行移动到您的try-catch
块中
JOptionPane.showMessageDialog(null, "The data has been saved successfully!!", "Success", JOptionPane.INFORMATION_MESSAGE);
由于此行已从您的try-catch
-中排除- 在处理异常后,同一行将再次执行。
TA贡献1802条经验 获得超5个赞
有两种可能。
出现异常时首先返回
catch (SQLException ex) {
Logger.getLogger(AddFrame.class.getName()).log(Level.SEVERE, null, ex);
JOptionPane.showMessageDialog(null, ex, "Error", JOptionPane.ERROR_MESSAGE);
return;
}
第二个将成功消息移动到 try 块中
try{
String url = "jdbc:mysql://localhost:3306/employee?autoReconnect=true&useSSL=false";
String uname = "root";
String pass = "password";
String query = "insert into emp values (";
Connection con = DriverManager.getConnection(url,uname,pass);
Statement st = con.createStatement();
query += idTextField.getText() + "," + fNameTextField.getText() + "," + lNameTextField.getText() + ")";
st.executeUpdate(query);
JOptionPane.showMessageDialog(null, "The data has been saved successfully!!", "Success", JOptionPane.INFORMATION_MESSAGE);
}
catch (SQLException ex) {
Logger.getLogger(AddFrame.class.getName()).log(Level.SEVERE, null, ex);
JOptionPane.showMessageDialog(null, ex, "Error", JOptionPane.ERROR_MESSAGE);
}
添加回答
举报