2

I surf the net and don't search what I need. So please you help me. When I try to execute this SQL statement in Eclipse

private final String INSERT_ = "INSERT INTO department VALUES (?, ?, ?)";

I have next error:You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '?, ?, ?)' at line 1 This is my code

public void insert(int id, String name, String description) throws SQLException
{
    Department result = null;
    Connection conn = ConnectionManager.getConnection();
    PreparedStatement stmt = conn.prepareStatement(INSERT_);
    stmt.setInt(1, id);
    stmt.setString(2, name);
    stmt.setString(3, description);
    stmt.executeUpdate(INSERT_);
    System.out.println("Record is inserted into table!");
}

Thanks in advance!

1
  • 4
    I surf the net and don't search what I need. yes, I've noticed that a lot here on SO. Commented Nov 2, 2015 at 18:57

1 Answer 1

14

You shouldn't pass the SQL String to executeUpdate, since your PreparedStatement was already initialized with the SQL String + the parameters.

Change

stmt.executeUpdate(INSERT_);

to

stmt.executeUpdate();
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.