Search code examples
javamysqlsqlsqlexception

java.sql.SQLException: Parameter index out of range (5 > number of parameters, which is 4) on Netbeans


I started to learn making java GUI applications with Netbeans. I'm following a tutorial on youtube: https://www.youtube.com/watch?v=j0aEUB2Efuk. But I'm facing an error with mysql, I did the same as the guy in the video but is not working. Here's the code:

public boolean editClient(int id, String fname, String lname, String phone, String email) {
        PreparedStatement st;
        String editQuery = "UPDATE `clients` SET `first_name` = ?, `last_name = ?`, `phone` = ?, `email` = ? WHERE `id` = ? ";

        try {

            st = my_connection.createConnection().prepareStatement(editQuery);

            st.setInt(1, id);
            st.setString(2, fname);
            st.setString(3, lname);
            st.setString(4, phone);
            st.setString(5, email);


            return st.executeUpdate() > 0;

        } catch (SQLException ex) {
            Logger.getLogger(CLIENT.class.getName()).log(Level.SEVERE, null, ex);
            return false;
        }
    }

The sql query seems to be ok or maybe is a bug with JDBC driver.


Solution

  • You have added a quote after last_name=?' hence its considering it as literal.

    Place it after last_name and before =

    and it should look like this,

    String editQuery = "UPDATE `clients` SET `first_name` = ?, `last_name` = ?, `phone` = ?, `email` = ? WHERE `id` = ? "; 
    

    Upvote the answer if it was helpful.