简体   繁体   中英

Getting java.sql.SQLException: No value specified for parameter 1 while updating record in servlet using mysql

I am getting java.sql.SQLException:

No value specified for parameter 1 error

while updating email address which is 11th column in database.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM