我正在創建一個簡單的註冊框架,將記錄添加到數據庫中。它每次運行SQL查詢時都會給我一條錯誤消息,該查詢會在數據庫中添加記錄,但它仍然會添加它們,但是由於我的程序停滯不前,而不是打開另一個窗口。Java,代碼有效,但仍然拋出異常
這裏的那部分代碼:
regButton.addActionListener(new ActionListener() {
@Override public void actionPerformed(ActionEvent e) {
//Execute when button is pressed
if( uNameField.getText().equals("")
|| new String(newPassField.getPassword()).equals("")
|| new String(repeatPassField.getPassword()).equals("")) {
errorLabel.setForeground(Color.red);
errorLabel.setText("Some fields are left blank");
}
else if(new String(newPassField.getPassword()).equals(
new String(repeatPassField.getPassword()))){
Statement stmt;
ResultSet res;
try
{
//SET USERNAME AND PASSWORD FROM FIELDS TO UPPER CASE
String username = uNameField.getText().toUpperCase();
String password = new String(newPassField.getPassword()).toUpperCase();
//SQL INSERT QUERY
String sql;
sql = "INSERT INTO Employees VALUES ('" +username +"','" +password +"');";
stmt = con.createStatement();
res = stmt.executeQuery(sql);
System.out.println("Added to database!");
con.close();
}
catch(SQLException exe) {
System.out.println("Error creating or running statement: " + e.toString());
try {
con.close();
}
catch(Exception eex){}
}
}
else {
errorLabel.setForeground(Color.red);
errorLabel.setText("Password missmatch");
}
}
每次它會註冊一個新員工(用戶)會顯示這一次「錯誤創建或運行語句:......」雖然,我可以找到員工列表中新增員工。
什麼可能導致此問題?
你能打印你吞嚥並將其添加到您的問題除外(S)的堆棧跟蹤? – Thor84no
你可以打印堆棧跟蹤通過使用變量'EXE' – Ankur