"Parameter index out of range (3 > number of parameters, which is 2)." when updating table

Rio Ablas :

I need help with a final project I'm making for college, I'm making a Library kind of System. I'm new to mySQL.

I'm having a problem on code saying, "Parameter index out of range (3 > number of parameters, which is 2)."

PreparedStatement book_edit = book_db.prepareStatement("update books set ?='?' where id=?");

            book_edit.setString(1, column); //where column is to set the column of the table
            book_edit.setString(2, input); //where input is what you want to change
            book_edit.setInt(3, book_id); //the primary key in my database

            int i = book_edit.executeUpdate();

            if (i != 0) {
                System.out.println("UPDATED!!");
            }
            else {
                System.out.println("Failed to Add Data!");
            }
Eran :

The column name cannot be a variable of the PreparedStatement. It must be part of the SQL String:

PreparedStatement book_edit = book_db.prepareStatement("update books set " + column + "= ? where id = ?");

book_edit.setString(1, input); //where input is what you want to change
book_edit.setInt(2, book_id); //the primary key in my database

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=145816&siteId=1