Execute .sql script using JDBC and MySQL
Problem: When developing applications using MySQL and JDBC, creating tables through multiple statements can become tedious. Is there a more efficient way to execute SQL scripts using JDBC?
Answer: Yes, JDBC provides a convenient solution to run .sql scripts. By using the ScriptRunner class, developers can easily execute SQL scripts. A ScriptRunner implementation example is provided on Pastebin for reference.
Usage:
To use ScriptRunner, follow these steps:
An example is as follows:
<code class="language-java">Connection con = ...; ScriptRunner runner = new ScriptRunner(con); runner.runScript(new BufferedReader(new FileReader("test.sql")));</code>
This approach allows developers to programmatically execute SQL scripts, simplifying the process of creating tables and improving the readability and maintainability of JDBC-based applications.
The above is the detailed content of How Can I Efficiently Execute SQL Scripts with JDBC and MySQL?. For more information, please follow other related articles on the PHP Chinese website!