java导入mysql数据库包,通过Java以编程方式导入(mysql)数据库转储

该博客探讨了如何通过Java程序来导入包含INSERT和CREATE TABLE语句的MySQL数据库dump文件。作者引用了在Rails中使用fixtures填充测试数据库的经验,并寻求在Java中类似的方法。解决方案建议使用Runtime类启动一个新进程,执行mysql命令行工具来导入dump.sql文件。
摘要由CSDN通过智能技术生成

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.

Unfortunately this doesn't work:

Connection conn = dbConnectionSource.getConnection();

Statement stmt = conn.createStatement();

stmt.execute(FileUtils.readFileToString(new File("./some-sql-file")));

conn.close();

Thanks,

-A

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.

解决方案

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值