我正在使用jdbi建立与db的连接并执行sql命令。
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;
}
});
现在我想使用jdbi运行sql文件。我搜索了很多,但无法弄清楚如何。
答案 0 :(得分:7)
您应该将sql文件读取为字符串,然后像
一样执行它String script = ".. your sql file contents here ..";
try (Handle h = dbi.open()) {
h.createScript(script).execute();
}