获取java.sql.SQLException:使用mysql更新servlet中的记录时,没有为参数1指定值

时间:2016-12-11 09:03:24

标签: java mysql jdbc

我收到了java.sql.SQLException:

  

没有为参数1错误指定值

更新数据库中第11列的电子邮件地址。

这是代码:

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

1 个答案:

答案 0 :(得分:2)

在传递参数之前执行语句。 这就是它的样子。

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

无论数据库中列的顺序如何,第一个参数始终为索引1,第二个参数为索引2等。