gpt4 book ai didi

java 参数索引超出范围(1 > 参数数量,mysql 为 0 - 简单登录

转载 作者:行者123 更新时间:2023-11-29 13:02:27 24 4
gpt4 key购买 nike

我正在尝试用 Java(在 j Creator 中)检查用户名和密码,但无法获得正确的语法。如何通过传递用户名和密码来实现对数据库的简单检查?这是给我带来问题的代码:

      try
{
String userN, passW, host, database;
userN = "Michael";
passW = "Blaine22";
host = "localhost";
database = "maintenance_work";

Connection conn = DriverManager.getConnection
("jdbc:mysql://"+host+":3306/"+database, userN, passW);

conn.setAutoCommit(false);
ResultSet rs = null;
PreparedStatement pst = null;

Boolean user_Check_pass;

String sql =("SELECT * from operators where user_name = " + user + " and pass_word= " + pass);

pst = conn.prepareStatement(sql);

pst.setString(1, user);
pst.setString(2, pass);
rs = pst.executeQuery();

if (rs.next())
{
System.out.println ("Username and Password correct");
user_Check_pass = true;
}
else
{
System.out.println ("invalid username and password");
user_Check_pass = false;
}
}
catch (SQLException e)
{
e.printStackTrace();
}
return false;

最佳答案

您需要按如下方式更改查询 -

String sql = "SELECT * from operators where user_name = ? and pass_word = ?";

? 是 setXXX(index, value) 方法所取代的内容。

关于java 参数索引超出范围(1 > 参数数量,mysql 为 0 - 简单登录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23159928/

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