Executing Multiple Statement MySQL Scripts in Java JDBC

You're running your Integration Tests, populate your MySQL database with a multi-statement MySQL Script and see the following response:

You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax...

Your syntax is just fine. You need a library that gives you the ability to run multi-statement MySQL Scripts. That library is Ibatis.

We're going to read a SQL Script from a file and execute it in a JUnit @BeforeClass method. Our script is straightforward, truncate tables and insert some fresh data for testing.

Here is our @BeforeClass method. We use the Ibatis ScriptRunner to read a script from a file, then create an Ibatis Reader to run the script. Everything else is straight-up JDBC, or in our case, the syntax associated with the Bootique JDBC Module.

Here's the Maven Dependency info where you can see for our needs we are limiting the usages to Test Scope.

Now go run that multiple statement MySQL script!

More Like This Post