gpt4 book ai didi

mysql - Database.getConnection() 错误 -->参数索引超出范围(1 > 参数数量,为 0)

转载 作者:行者123 更新时间:2023-11-29 23:17:21 25 4
gpt4 key购买 nike

我开发了一个应用程序,它显示 id、subject、indexno 和其他信息。每个字段都有单独的文本框,每当用户填写表单并点击提交按钮时,该值都必须插入到我的数据库中。如果我尝试将其提交到数据库,则什么也不会发生。请看下面的代码。

        try {
Connection con = null;
PreparedStatement pst = null;
String id = "", subject = "", indexNo = "", additional = "";
Class.forName("com.mysql.jdbc.Driver").newInstance();
int updateQuery = 0;
if (id == null && subject == null && indexNo == null && additional == null) {
if (id == "" && subject == " " && indexNo == "" && additional == "") {

con = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "KUMAR");

String query="insert into circularindex(id,subject,indexNo,additional)values(?,?,?,?)";
pst = con.prepareStatement(query);

pst.setString(1, id);
pst.setString(1, subject);
pst.setString(2, indexNo);
pst.setString(3, additional);
pst.executeUpdate();
}
}
} catch (Exception ex) {
System.out.println("Database.getConnection() Error -->" + ex.getMessage());
}

请帮助我解决上述编码错误并执行必要的操作。

最佳答案

设置参数时似乎存在拼写错误:

pst.setString(1, id);
pst.setString(1, subject);
pst.setString(2, indexNo);
pst.setString(3, additional);

您通过覆盖另一个参数来设置第一个参数两次。你应该这样做

pst.setString(1, id);
pst.setString(2, subject);
pst.setString(3, indexNo);
pst.setString(4, additional);

关于mysql - Database.getConnection() 错误 -->参数索引超出范围(1 > 参数数量,为 0),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27660014/

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