【发布时间】:2016-03-01 07:59:44
【问题描述】:
我正在为我的注册系统制作登录示例。在做一些研究时,抓取一些我在互联网上看到的代码。我将在用户名和密码字段中登录。我在使用 SQL 时遇到错误的地方。任何帮助,提示将不胜感激!
错误
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());
}
【问题讨论】:
-
WHERE USERNAME = ?, PASSWORD = ?- 您在哪个手册中找到了该语法?应该是WHERE USERNAME = ? and PASSWORD = ?。 -
谢谢你的回答几乎一样。你是神! :)