gpt4 book ai didi

Java SQL 语句 - 索引数组越界

转载 作者:行者123 更新时间:2023-12-01 11:45:01 24 4
gpt4 key购买 nike

嗨,我当前有一个正在尝试运行的查询,但是每次我尝试运行此查询时,我都会得到一个数组索引越界,我不确定为什么这是因为我没有在其中声明任何数组我的代码,有人可以帮忙吗?

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();
}

最佳答案

抛出异常是因为查询中没有要填充的参数的占位符。您应该使用 ? 字符而不是硬编码值:

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());

关于Java SQL 语句 - 索引数组越界,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29219758/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com