我有三個表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).
你的職位是非常難讀,由於所有的空行沒有理由。請對其進行編輯,同時銘記您要求別人幫助您 - 讓您儘可能輕鬆閱讀是您的責任。 –
源代碼中的單個空白行是* always *就足夠了。 '{'之後或'}'之前的空行通常也是多餘的。 –