2013-10-05 51 views
-2

我有三個表1.用戶2.分支3用戶分支。 我試圖解決登錄表單。但是當登錄按鈕被點擊時,顯示這個錯誤java.sql.SQLException:參數索引超出範圍(1>參數數量,即0)。錯誤顯示java.sql.SQLException:參數索引超出範圍(1>參數數量,爲0)

public Boolean loginApplication(Connection con, String uname, String pwd, String brnch)  { 
    try { 
     PreparedStatement ps = con.prepareStatement("Select u.username,u.password," 
       + "b.branchname from user u, branch b ,userbranch ub" 
       + "where u.userid = ub.userid and b.branchid=ub.branchid "); 
     ps.setString(1, uname); 
     ps.setString(2, pwd); 
     ps.setString(3, brnch); 
     ResultSet rs = ps.executeQuery(); 
     System.out.println("query return " + rs); 
     if (rs.next()) { 
      return true; 
      //true if query found any corresponding data 
     } 
     else{ 
      return false; 
     } 
    } 
     catch (SQLException ex) { 
     System.out.println("Error while validating " + ex); 
     return false; 
    } 
} 

private void buttonloginActionPerformed(java.awt.event.ActionEvent evt) { 
    String uname=username.getText(); 
    String upass=userpassword.getText(); 
    String ubranch=userbranch.getSelectedItem().toString().trim(); 
    if(evt.getSource()==buttonlogin){ 
    if(user.loginApplication(connect.getCon(),uname,upass,ubranch)){ 
     System.out.println("success"); 
     MainForm mainForm=new MainForm(); 
     mainForm.setVisible(true); 
    } 
    } 
    else{ 
     JOptionPane.showMessageDialog(null, "Login failed!","Failed!!", 
            JOptionPane.ERROR_MESSAGE); 
     } 
} 

顯示錯誤:

java.sql.SQLException: Parameter index out of range (1 > number of parameters, which is 0). 
+0

你的職位是非常難讀,由於所有的空行沒有理由。請對其進行編輯,同時銘記您要求別人幫助您 - 讓您儘可能輕鬆閱讀是您的責任。 –

+0

源代碼中的單個空白行是* always *就足夠了。 '{'之後或'}'之前的空行通常也是多餘的。 –

回答

1

基本上,問題是由錯誤消息所指示的。你的SQL語句沒有參數,但你試圖設置一些參數。

SQL語句中的(未命名)參數由?佔位符指示。

2

您的SQL沒有任何參數:

Select u.username,u.password,b.branchname from user u, branch b, userbranch 
ubwhere u.userid = ub.userid and b.branchid=ub.branchid 

所以,當您嘗試設置參數,它的失敗。你可能想要:

and u.userid = ? and u.password = ? and b.branchid = ? 

...或類似的東西。除此之外,建議您以純文本格式存儲密碼,從安全角度來看,這可能是可怕的

哦,我想你想ubwhere之間的空間......

+0

現在顯示此錯誤@Jon Skeet ... com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException:您的SQL語法中有錯誤;檢查與您的MySQL服務器版本相對應的手冊,以便在第1行'u.password ='',b.branchname ='Select'附近使用正確的語法 – Snahar

+0

@Snahar閱讀本答案的最後一行。 –

相關問題