2

I am developing an application with java on netbeans/windows 7. I was trying to insert data to database with PreparedStatement using SQL. So this is my code;

private void addInfoActionPerformed(java.awt.event.ActionEvent evt) {                                        
    Connection conn;    
    PreparedStatement pst;
    String url = "jdbc:derby://localhost:1527/records";
    String SQL_INSERT = "INSERT INTO records"+
                "VALUES(?,?,?)";
    String name, surname, number;
    try {
        conn = DriverManager.getConnection(url, "system", "app");
        System.out.println("connected to db");
        pst = conn.prepareStatement(SQL_INSERT);
        name = nameField.getText();
        surname = surnameField.getText();
        number = numberField.getText();
        System.out.println("got data from textfields");
        pst.setString(1, name);
        pst.setString(2, surname);
        pst.setString(3, number);
        System.out.println("variables set");
        pst.executeUpdate();
        System.out.println("sql command executed");
        pst.close();
        conn.close();
    } catch (SQLException ex) {
        Logger.getLogger(addition.class.getName()).log(Level.SEVERE, null, ex);
    }

}  

But I got an error like this;

java.sql.SQLSyntaxErrorException: Syntax error: Encountered "?" at line 1, column 27.

Name of my table is records and it has three coloumns named; name, surname and number. As I can understand from the println lines, there is a problem with that line;

pst = conn.prepareStatement(SQL_INSERT);

or maybe I created SQL_INSERT string and SQL code wrong. I couldn't figure out what is the problem exactly.

1
  • The error is an SQL syntax error, not a Java syntax error. Nothing at all wrong with Derby, but give HSQLDB a look if you like ... it's soon to be at ver. 2.3.0. Commented Jun 11, 2013 at 13:39

1 Answer 1

8

You're missing a space in

String SQL_INSERT = "INSERT INTO records"+
            "VALUES(?,?,?)";

When you do the concatenation, it produces "INSERT INTO recordsVALUES(?,?,?)"

Change it to

String SQL_INSERT = "INSERT INTO records"+
            " VALUES(?,?,?)";
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.