Java Jdbc SQL异常(语法错误)

时间:2016-03-01 07:59:45

标签: java jdbc

我正在为我的注册系统登录样本。在进行一些研究时,我会抓住一些我在互联网上看到的代码。我将在用户名和用户名字段中登录密码。我在哪里遇到使用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());
}

2 个答案:

答案 0 :(得分:2)

您需要在查询中使用and代替逗号,,如下所示:

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

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

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

答案 1 :(得分:1)

首先,你的代码中存在很多语法错误,要么你只是从某个地方复制粘贴它,要么你不小心把你的问题写入错误并希望找到正确的答案。

第二件事,

使用您正在尝试调用的查询,executeUpdate不正确。它是一个Select查询,您必须在调用executeQuery

之前先设置参数
String myQuery = "SELECT * FROM USERTYPE WHERE `USERNAME` = ? AND `PASSWORD` = ?";

          try(Connection myConnection = DBUtil.getConnection(DBType.JDBC);
                       PreparedStatement myPs = myConnection.prepareStatement(myQuery){


                            myPs.setString(1, inputUsername);
                            myPs.setString(2, inputPassword);
                       ResultSet myResultSet = myPs.executeQuery();


                        //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`