0

I'm trying to do a batch insert with java in a test database for my college project, but I'm getting an error and I could not figure out why.. selects statements are working but the insert will just not work. thanks in advance.

Error: Exception in thread "main" java.sql.BatchUpdateException: 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 at com.mysql.jdbc.StatementImpl.executeBatch(StatementImpl.java:1193) at test.main(test.java:29)

    public static void main(String[] args) throws Exception
    {
            Class.forName("com.mysql.jdbc.Driver");
            Connection con = DriverManager.getConnection ("jdbc:mysql://myserverIP:3306/databaseName","root", "");

            Statement statement = con.createStatement();

            String [] queries = {
                "insert into name (first,last) values ('Joao', 'Silva')",
                "insert into name (first,last) values ('Jose', 'Santos')",
                "insert into name (first,last) values ('Lucas', 'Maluco')",
                "insert into name (first,last) values ('Cesar', 'Pereira')",

            };

            for (String query: queries) {
            statement.addBatch(query);
            statement.executeBatch();
            }
            statement.close();
            con.close();
    }
2
  • Forgot to mention that I'm not getting any errors while compiling the code, but only when I run it. Commented Mar 2, 2014 at 21:20
  • SQL Injection alert: Consider using PreparedStatement Commented Mar 2, 2014 at 21:33

1 Answer 1

2

Execute the batch after all the queries has been added to the Statement rather than per individual query

for (String query: queries) {
   statement.addBatch(query);
}
statement.executeBatch();
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.