我正在爲我的註冊系統開發一個登錄示例。在做一些研究的同時,獲取我在互聯網上看到的一些代碼。我將在用戶名爲&密碼的字段中登錄。我在哪裏遇到使用SQL的錯誤。任何幫助,提示將欣賞!Java Jdbc SQL異常(語法錯誤)
ERROR
Error message: Syntax error: Encountered "," at line 1, column 42.
Error code: -1
SQL State: 42X01
這是編譯器位於第1行指向誤差,柱42我用Windows的監聽器的可予用於系統關閉。
窗口監聽代碼
//Window Listener
addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}//window Closing
});
代碼這是我登錄。當我完成填寫用戶名和密碼文本框,並點擊登錄按鈕哪裏我遇到錯誤。
loginButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
String inputUsername = usernameTextField.getText().trim();
String inputPassword = String.valueOf(passwordPasswordField.getText());
String inputUserType = listUser.getSelectedItem().toString();
if(inputUsername.isEmpty() && inputPassword.isEmpty()){
JOptionPane.showMessageDialog(null, "Please fill up!");
}
else if(inputUsername.isEmpty()){
JOptionPane.showMessageDialog(null, "Please enter your username");
}
else if(inputPassword.isEmpty()){
JOptionPane.showMessageDialog(null, "Please enter your password");
}
else{
String myQuery = "SELECT * FROM USERTYPE WHERE USERNAME = ?, PASSWORD = ?";
try(Connection myConnection = DBUtil.getConnection(DBType.JDBC);
PreparedStatement myPs = myConnection.prepareStatement(myQuery);
ResultSet myResultSet = myPs.executeQuery()){
while(myResultSet.next())
myPs.setString(1, inputUsername);
myPs.setString(2, inputPassword);
myPs.executeUpdate();
JOptionPane.showMessageDialog(null, "Succesfuly Inserted!");
//end of else
}//end of try
catch(SQLException ex) {
DBUtil.processException(ex);
}//end of catch
}//end of else
}//end of action performed
});//end of action listener`
DBUtil我的連接和異常這是我拋開我的連接和異常哪個地方拋出的錯誤
private static final String dbUrl = "jdbc:derby://localhost:1527/Info";
private static final String username = "john";
private static final String pass = "john";
//Argument for DBType
public static Connection getConnection(DBType dbType) throws SQLException{
return DriverManager.getConnection(dbUrl,username,pass);
}
public static void processException(SQLException e){
System.err.println("Error message: "+e.getMessage());
System.err.println("Error code: "+e.getErrorCode());
System.err.println("SQL State: "+e.getSQLState());
}
'其中username =,PASSWORD ='之前,首先設置參數 - ?在手動你發現語法?它應該是'WHERE USERNAME =?和PASSWORD =?'。 –
謝謝你的答案差不多。你是上帝! :) – Francisunoxx