Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to run sql script file using JDBI

Tags:

java

mysql

jdbi

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.

like image 355
vaibhav.g Avatar asked Feb 09 '23 05:02

vaibhav.g


1 Answers

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();
}
like image 50
Deinlandel Avatar answered Feb 13 '23 23:02

Deinlandel