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

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

问题描述

我开始将 MySQL 与 JDBC 结合使用.

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 的翻版.

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天全站免登陆