gpt4 book ai didi

Java Jdbc SQL 异常(语法错误)

转载 作者:行者123 更新时间:2023-12-01 21:47:54 24 4
gpt4 key购买 nike

我正在为我的注册系统制作登录示例。在做一些研究时,抓取一些我在互联网上看到的代码。我将在用户名和密码字段中登录。我在使用 SQL 时遇到错误。任何帮助、提示将不胜感激!

错误

Error message: Syntax error: Encountered "," at line 1, column 42.
Error code: -1
SQL State: 42X01

这是编译器在第 1 行第 42 列指出错误的地方。我使用 Windows Listener 来关闭系统。

窗口监听器代码

//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());
}

最佳答案

您需要在查询中使用 and 而不是逗号 ,,如下所示:

   String myQuery = "SELECT * FROM USERTYPE WHERE USERNAME = ? and PASSWORD = ?"

并且您需要在 myPs.executeQuery() 之前设置 2 参数,如下所示:

  PreparedStatement myPs = myConnection.prepareStatement(myQuery);
myPs.setString(1, some_userName);
myPs.setString(2, some_password);

关于Java Jdbc SQL 异常(语法错误),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35717514/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com