2016-03-01 92 views
0

我正在爲我的註冊系統開發一個登錄示例。在做一些研究的同時,獲取我在互聯網上看到的一些代碼。我將在用戶名爲&密碼的字段中登錄。我在哪裏遇到使用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()); 
} 
+0

'其中username =,PASSWORD ='之前,首先設置參數 - ?在手動你發現語法?它應該是'WHERE USERNAME =?和PASSWORD =?'。 –

+0

謝謝你的答案差不多。你是上帝! :) – Francisunoxx

回答

2

您需要在您的查詢像使用and,而不是逗號,如下:

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

而且您需要設置參數2myPs.executeQuery()前象下面這樣:

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

它已經擺脫了錯誤。現在我有錯誤「至少有一個當前語句的參數未初始化。」我已經打電話給我的setString 1和2.哪個第一個索引是用戶名和密碼的第二個?謝謝 – Francisunoxx

+0

@MiaLegaspi看到更新的答案 – Abdelhak

+0

你的救星。謝謝。我剛開始學習jdbc,感謝你的幫助。 – Francisunoxx

1

首先,有在你的代碼,所以很多語法錯誤,要麼你剛剛從什麼地方複製粘貼,或者你不小心把你的問題與錯誤,並希望能找到合適的回答。

第二件事,

與您試圖調用executeUpdate的查詢是不正確的。這是一個選擇查詢,你必須調用的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` 
+0

謝謝你已經在工作。也許我只是對語法有點混淆。順便說一下,這非常有幫助! :) – Francisunoxx