MySQL String not executing in Java [duplicate]

This question already has an answer here:

  • How to execute multiple SQL statements from java2 answers

I am trying and failing to get a String SQL command to execute on an existing mySQL database using a simple test program in Netbeans 8.0.2. Strange thing is the same SQL command executes fine when put directly into the IDE. Appreciate the help and please correct me on any terminology, I'm new and working it out from online tutorials. Thanks

package testdb1;

 * @author x
import java.sql.*;

public class TestDB1 {

    //Driver name and database URL    
    static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
    static final String DB_URL = "jdbc:mysql://localhost:3306/mynewdatabase?zeroDateTimeBehavior=convertToNull";

    static final String username = "****";
    static final String password = "**********";

    public static void main(String[] args) {

    Connection conn = null;
    Statement stmnt = null;


     //Open connection
     conn = DriverManager.getConnection(DB_URL, username, password);

     //Execute query
     System.out.println("Creating statement...");
     stmnt = conn.createStatement();
     String sql = "START TRANSACTION; " +
                  "UPDATE customer_test_accounts " +
                  "SET balance = balance + 1000 " +
                  "WHERE accountnumber = 2; " +
                  "COMMIT; " +



Creating statement...
com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: 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 'UPDATE customer_test_accounts SET balance = balance + 1000 WHERE accountnumber =' at line 1
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(
    at java.lang.reflect.Constructor.newInstance(
    at com.mysql.jdbc.Util.handleNewInstance(
    at com.mysql.jdbc.Util.getInstance(
    at com.mysql.jdbc.SQLError.createSQLException(
    at com.mysql.jdbc.MysqlIO.checkErrorPacket(
    at com.mysql.jdbc.MysqlIO.checkErrorPacket(
    at com.mysql.jdbc.MysqlIO.sendCommand(
    at com.mysql.jdbc.MysqlIO.sqlQueryDirect(
    at com.mysql.jdbc.ConnectionImpl.execSQL(
    at com.mysql.jdbc.ConnectionImpl.execSQL(
    at com.mysql.jdbc.StatementImpl.executeQuery(
    at testdb1.TestDB1.main(
BUILD SUCCESSFUL (total time: 2 seconds)

You are doing JDBC Transactions in a wrong way. Putting all junk in sql string won't work.

Instead use :

con.setAutoCommit(false); Set auto commit to false

When a connection is created, it is in auto-commit mode. This means that each individual SQL statement is treated as a transaction and is automatically committed right after it is executed.

con.commit(); For commit

con.rollback(); For rollback

See Using Transactions and follow examples

just write this simple sql statement and try.... "UPDATE customer_test_accounts SET balance = balance + 1000 WHERE accountnumber = 2"

You can try the code: String sql = "UPDATE customer_test_accounts " + "SET balance = balance + 1000 " + "WHERE accountnumber = 2"; stmnt.executeUpdate(sql);

The problem is quite simple and pointed out by @Jens for java. Same deal for php. Why talk about php you wonder? Concept is the same in that mysql needs to have multiple stmt batches turned on or allowed.

How to execute multiple SQL statements from java

Executing multiple SQL queries in one statement with PHP

Otherwise it bombs and you focus on cleaning the sql string which is not the problem. Again the problem is preparing the connection or stmt to allow such a batch. In the case here, it bombs after the innards reach the first semi-colon regardless of how well constructed it appears in the eyes of a .sql file script running it or it sitting in a stored proc.

What Others Are Reading