17

Executing Multiple Statement MySQL Scripts in Java JDBC

 3 years ago
source link: https://nixmash.com/java/executing-multiple-statement-mysql-scripts-in-java-jdbc/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

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.

ibatis0404a.png

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.

ibatis0404b.png

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

ibatis0404c.png

Now go run that multiple statement MySQL script!

CategoriesJavaTagsBootique, Java, MySQL, Testing


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK