嗨我当前有一个查询,我正在尝试运行但是每次我尝试运行此查询我得到一个数组索引超出界限,我不知道为什么这是因为我还没有声明任何数组我的代码,任何人都可以帮忙吗?
public void actionPerformed(ActionEvent argO) {
try{
String query="select * from Users where Username ='admin' and password='password2' ";
PreparedStatement pst=connection.prepareStatement(query);
pst.setString(1, text_username.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, "Username and password is correct");
frmMychatApp.dispose();
AdminPage admin = new AdminPage();
admin.setVisible(true);
}
else if(count>1)
{
JOptionPane.showMessageDialog(null, "Duplicate Username and password");
}
else{
JOptionPane.showConfirmDialog(null, "Username and password is not correct. Please Try Again");
}
rs.close();
pst.close();
}catch (Exception e)
{
JOptionPane.showMessageDialog(null, e);
e.printStackTrace();
}
答案 0 :(得分:1)
抛出异常是因为查询中没有要填充参数的占位符。您应该使用?
个字符而不是硬编码值:
String query="select * from Users where Username =? and password=? ";
PreparedStatement pst=connection.prepareStatement(query);
pst.setString(1, text_username.getText());
pst.setString(2, passwordField.getText());