[英]Getting java.sql.SQLException: No value specified for parameter 1 while updating record in servlet using mysql
I am getting java.sql.SQLException: 我正在获取java.sql.SQLException:
No value specified for parameter 1 error
没有为参数1错误指定值
while updating email address which is 11th column in database. 同时更新数据库中第11列的电子邮件地址。
This is the code: 这是代码:
try {
con = DriverManager.getConnection("jdbc:mysql://localhost:3308/authenticate", "root", "root");
st = con.createStatement();
String query = "update custt set email =? where accno =?";
PreparedStatement ps = con.prepareStatement(query);
ps.executeUpdate();
System.out.println("updated");
//st.executeUpdate(query);// create a statement
ps.setInt(2, acn);
ps.setString(11, eml);
//eml=rs.getString(11); // set input parameter 1
System.out.println("updated value"+acn);
System.out.println("updated value"+eml);
// acnn = rs.getInt(2);
/// session.setAttribute("Accno", acnn);
//session.setAttribute("C_email", eml);
// System.out.println("updated");
} catch (SQLException ex) {
Logger.getLogger(UpdateDetails.class.getName()).log(Level.SEVERE, null, ex);
}
you are executing the statement before you pass the parameters. 您在传递参数之前正在执行语句。 This is how it should look like.
这就是它的外观。
String query = "update custt set email =? where accno =?";
PreparedStatement ps = con.prepareStatement(query);
ps.setString(1, eml);
ps.setInt(2, acn);
ps.executeUpdate();
System.out.println("updated");
The first parameter is always index 1 and the second parameters is index 2, etc regardless of the order of columns in database. 无论数据库中列的顺序如何,第一个参数始终为索引1,第二个参数始终为索引2,依此类推。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.