0

Hello I want to put 2 values to 2 columns in my table. I get those values from txtField and textField_1 (which are 2 textboxes on an GUI).

The problem is that whenever I push the button to register those values I get an syntax error on my mysql.

String query = "INSERT INTO Registration (Username , Password ) VALUES (? ,?)";
                java.sql.PreparedStatement pst = connection.prepareStatement(query);
                pst.setString(1,textField.getText());
                pst.setString(2,textField_1.getText());
                int rs = pst.executeUpdate(query);

If I put static values instead of ?, is working.

  • It is the standard , "You got an MySQL error in your syntax, check for..." – Radu Maftei Jan 08 '17 at 11:35
  • Possible duplicate of [How to use prepared statement for select query in Java?](http://stackoverflow.com/questions/24692296/how-to-use-prepared-statement-for-select-query-in-java) – hotzst Jan 08 '17 at 11:36
  • 1
    `query` parameter not needed. http://alvinalexander.com/java/java-mysql-insert-example-preparedstatement – Riad Jan 08 '17 at 11:39

1 Answers1

1
String query = "INSERT INTO Registration (Username , Password ) VALUES (? ,?)";
                java.sql.PreparedStatement pst = connection.prepareStatement(query);
                pst.setString(1,textField.getText());
                pst.setString(2,textField_1.getText());
                int rs = pst.executeUpdate();

use executeUpdate()

SpringLearner
  • 13,738
  • 20
  • 78
  • 116