通过Java以编程方式导入(mysql)数据库转储 [英] Importing a (mysql) database dump programmatically through Java

查看:523
本文介绍了通过Java以编程方式导入(mysql)数据库转储的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何通过java程序以编程方式导入mysql数据库转储文件(包含insert和create table语句)。我需要这个作为单元测试的设置阶段。

How can I import a mysql database dump file (contains insert and create table statements) programmatically through a java program. I need this as the setup phase of a unit test.

很遗憾,这不起作用:

Connection conn = dbConnectionSource.getConnection();
Statement stmt = conn.createStatement();
stmt.execute(FileUtils.readFileToString(new File("./some-sql-file")));
conn.close();

谢谢
-A

Thanks, -A

PS - 在Rails中,我使用fixtures来填充测试数据库。

PS - In Rails, I used fixtures for filling a test database. I made rails rails create the underlying tables through setting the environment to test, anything similar in Java.

推荐答案

你可以开始一个新的进程从java和执行此命令,如果你有访问mysql可执行文件,无论你在哪里运行导入。类似这样:

You could start a new process from java and execute this command if you have access to the mysql executable wherever you are running the import. Something like this:

Runtime rt = Runtime.getRuntime();
Process pr = rt.exec("mysql -p -h ServerName DbName < dump.sql");

这篇关于通过Java以编程方式导入(mysql)数据库转储的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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