I am starting to use MySQL with JDBC.
Class.forName("com.mysql.jdbc.Driver"); conn = DriverManager.getConnection("jdbc:mysql:///x", "x", "x"); stmt = conn.createStatement(); stmt.execute( "CREATE TABLE amigos" + "("+ "id int AUTO_INCREMENT not null,"+ "nombre char(20) not null,"+ "primary key(id)" + ")");
I have 3-4 tables to create and this doesn’t look good.
Is there a way to run a .sql script from MySQL JDBC?
Advertisement
Answer
Ok. You can use this class here (posted on pastebin because of file length) in your project. But remember to keep the apache license info.
It’s ripoff of the iBatis ScriptRunner with dependencies removed.
You can use it like this
Connection con = .... ScriptRunner runner = new ScriptRunner(con, [booleanAutoCommit], [booleanStopOnerror]); runner.runScript(new BufferedReader(new FileReader("test.sql")));
That’s it!