我一直在用netbeans15写登录页面。当我为card number和password字段设置正确的值时,它可以正常工作。
但是,当我将card number和password的值放在这个语句Rs= St.executeQuery();之后时,代码将终止,甚至不会执行它的下一个语句System.out.println("Resultset"+ Rs);。
Rs甚至不包含错误的card number和password的空值。
我的问题是如何为错误的card number和password执行wrong语句?代码没有输入否则块错误的文本框!!
Connection Con = null;
PreparedStatement pst = null;
ResultSet Rs=null;
Statement St;
private void LoginTbMouseClicked(java.awt.event.MouseEvent evt) {
if(UnameTb.getText().isEmpty() || PasswordTb.getText().isEmpty()){
JOptionPane.showMessageDialog(this, "Enter Card and PIN Number");
}else{
String Query1 = "select * from AccountTbl where `Card Number`=? and `PIN`=?";
try{
Con = DriverManager.getConnection("jdbc:mysql://localhost:3306/CREDITDEBITDb","Amaity","Alok@2022");
PreparedStatement St = Con.prepareStatement(Query1);
Rs= St.executeQuery();
System.out.println("Resultset"+ Rs);
if(Rs.next()){
new MainMenu().setVisible(true);
this.dispose();
}else{
JOptionPane.showMessageDialog(this, "Wrong Card Numner or PIN");
}
}catch(HeadlessException | SQLException e){
}
} // TODO add your handling code here:
} 发布于 2022-09-17 01:57:09
您看不到任何异常,因为您将它们捕获,而在这里什么也不做:
catch(HeadlessException | SQLException e){
// bare minimum is log message or
e.printStackTrace();
// ideally: throw e
// or deal with issue here
}您应该记录并传递报告的异常。
错误是您没有为绑定变量赋值“?在声明中。假设这些是字符串,则将它们绑定到:
St.setString(1, cardNum);
St.setString(2, pin);
ResultSet rs = St.executeQuery();如果您坚持Java命名约定(camel case变量),它会帮助团队。
重构您的代码,这样JDBC和GUI代码就不会混合。您可以在不使用UI代码的情况下进行助手调用,调用这些代码只是为了完成数据库工作:
public boolean checkLogin(String cardNo, String pin) { ... }https://stackoverflow.com/questions/73750945
复制相似问题