我创建了2个JFrame:“Patients_Login”框架和“Doctors_Login”框架。我在“Patients_Login”框架的“登录”按钮中写了一个actionPerformed
方法,它工作正常。虽然我将完全相同的代码复制粘贴到“Doctors_Login”框架中的“登录”按钮,但它给了我这个错误:
java.sql.sqlexception:没有为参数1指定值
click here to see a capture of the error
这是“Patients_Login”框架(工作)中登录按钮的代码:
btnLogIn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try{
//connecting to database
Class.forName("com.mysql.jdbc.Driver");
Connection con=DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/clinic", "root", "newpass");
//verifying ID and password code
String query="select * from patients where P_ID=? and Password=?";
PreparedStatement pst=con.prepareStatement(query);
pst.setString(1, idTXT.getText());
pst.setString(2, passwordField.getText());
ResultSet rs=pst.executeQuery();
int count =0;
while(rs.next()){
count=count+1;
}
if (count ==1){
JOptionPane.showMessageDialog(null, "Welcome!");
patient_login.setVisible(false);
patient_frame.setVisible(true);
patient_appointments.setVisible(false);
}
else{
JOptionPane.showMessageDialog(null, "Incorrect username or password.");
}
rs.close();
pst.close();
//second query: displaying user info in JLabels
String query1="select * from patients where P_ID=?";
PreparedStatement pst1=con.prepareStatement(query1);
pst1.setString(1, idTXT.getText());
ResultSet rs1=pst1.executeQuery();
while (rs1.next()){
IDdraw_LBL.setText(rs1.getString("P_ID"));
namedraw_LBL.setText(rs1.getString("Name"));
msdraw_LBL.setText(rs1.getString("Medical_Situation"));
}
pst1.close();
}catch(Exception exx) {
exx.printStackTrace();
}
}
});
这是“Doctor_Login”框架中登录按钮的代码(给出错误):
btnLogin.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try{
//connecting to database
Class.forName("com.mysql.jdbc.Driver");
Connection con=DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/clinic", "root", "newpass");
//verifying ID and password code
String query="select * from doctors where ID=? and Password=?";
PreparedStatement pst=con.prepareStatement(query);
pst.setString(1, docidTXT.getText());
pst.setString(2, passwordField.getText());
ResultSet rs=pst.executeQuery();
int count =0;
while(rs.next()){
count=count+1;
}
if (count ==1){
JOptionPane.showMessageDialog(null, "Welcome!");
Doctor_login.setVisible(false);
doctor_Appointments.setVisible(false);
Doctor_Frame.setVisible(true);
}
else{
JOptionPane.showMessageDialog(null, "Incorrect username or password.");
}
rs.close();
pst.close();
//second query: displaying user info in JLabels
String query1="select * from doctors where ID=?";
PreparedStatement pst1=con.prepareStatement(query1);
pst1.setString(1, docidTXT.getText());
ResultSet rs1=pst1.executeQuery();
while (rs1.next()){
DRnamedraw_LBL.setText(rs1.getString("Name"));
no_of_patientsLBL.setText(rs1.getString("Patients"));
}
pst1.close();
}catch(Exception exx) {
exx.printStackTrace();
}
}
});
显然,两个代码完全相同,但我更改了对象名称和sql语句。
注意:在我点击登录按钮之前运行项目时会发生错误。
注意2:在错误弹出窗口中单击“确定”后,项目工作正常。
任何帮助或评论都将不胜感激。