4

I am using jdbi to make connection to db and execute sql command.

dbi = new DBI("jdbc:mysql://"+dbHostName+"/"+dbName, "root", "");
    dbi.withHandle(new HandleCallback<Object>() {
        @Override
        public Object withHandle(Handle handle) throws Exception {
            handle.execute("Query to execute")
            return null;
        }
    });

Now i want to run sql file using jdbi. I googled a lot but couldn't figure out how.

vaibhav.g
  • 729
  • 1
  • 9
  • 28
  • Read here : https://docs.oracle.com/javase/tutorial/essential/io/file.html and replace "Query to execute"' by what you read. – StephaneM Dec 23 '15 at 14:16

1 Answers1

8

You should read your sql file to string and then execute it like

String script = ".. your sql file contents here ..";
try (Handle h = dbi.open()) {
    h.createScript(script).execute();
}
Deinlandel
  • 1,023
  • 8
  • 25
  • ... this doesn't actually work. It interprets the SQL and sometimes it breaks. For example on Oracle it gives error if your SQL contains stuff like trigger definitions with multiple statements... – GACy20 Apr 28 '22 at 14:16