使用MySQL和JDBC运行.sql脚本 [英] Running a .sql script using MySQL with JDBC

查看:128
本文介绍了使用MySQL和JDBC运行.sql脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开始在MySQL中使用MySQL。

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)" +
            ")");

我要创建3-4个表,这看起来不太好。

I have 3-4 tables to create and this doesn't look good.

有没有办法从MySQL JDBC运行.sql脚本?

Is there a way to run a .sql script from MySQL JDBC?

推荐答案

好的。您可以在此项目中使用此类(由于文件长度而在pastebin上发布)。但请记住保留apache许可证信息。

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.

JDBC ScriptRunner

iBatis ScriptRunner的ripoff删除了依赖项。

It's ripoff of the iBatis ScriptRunner with dependencies removed.

你可以像这样使用它

Connection con = ....
ScriptRunner runner = new ScriptRunner(con, [booleanAutoCommit], [booleanStopOnerror]);
runner.runScript(new BufferedReader(new FileReader("test.sql")));

就是这样!

这篇关于使用MySQL和JDBC运行.sql脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆