简体   繁体   English

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

[英]Importing a (mysql) database dump programmatically through Java

How can I import a mysql database dump file (contains insert and create table statements) programmatically through a java program. 如何通过java程序以编程方式导入mysql数据库转储文件(包含insert和create table语句)。 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. PS - 在Rails中,我使用fixture来填充测试数据库。 I made rails rails create the underlying tables through setting the environment to test, anything similar in Java. 我通过设置要测试的环境来创建底层表来创建底层表,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. 如果您可以在运行导入的任何地方访问mysql可执行文件,则可以从java启动新进程并执行此命令。 Something like this: 像这样的东西:

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

Backup: 备份:

/******************************************************/
//Database Properties
/******************************************************/
String dbName = “dbName”;
String dbUser = “dbUser”;
String dbPass = “dbPass”;

/***********************************************************/
// Execute Shell Command
/***********************************************************/
String executeCmd = “”;
executeCmd = “mysqldump -u “+dbUser+” -p”+dbPass+” “+dbName+” -r backup.sql”;
}
Process runtimeProcess =Runtime.getRuntime().exec(executeCmd);
int processComplete = runtimeProcess.waitFor();
if(processComplete == 0){

out.println(“Backup taken successfully”);

} else {

out.println(“Could not take mysql backup”);

}

Restore: 恢复:

/******************************************************/
//Database Properties
/******************************************************/
String dbName = “dbName”;
String dbUser = “dbUser”;
String dbPass = “dbPass”;

/***********************************************************/
// Execute Shell Command
/***********************************************************/
String executeCmd = “”;

executeCmd = new String[]{“/bin/sh”, “-c”, “mysql -u” + dbUser+ ” -p”+dbPass+” ” + dbName+ ” < backup.sql” };

}
Process runtimeProcess =Runtime.getRuntime().exec(executeCmd);
int processComplete = runtimeProcess.waitFor();
if(processComplete == 0){

out.println(“success”);

} else {

out.println(“restore failure”);

}

Personally I would disrecommend loading a regular SQL dump in this way, because you would need non-trivial code to parse or at least tokenize SQL. 我个人不赞成以这种方式加载常规SQL转储,因为你需要非平凡的代码来解析或至少标记化SQL。

I would recommend using CSV data dumps, you can load these with a the LOAD DATA INFILE syntax. 我建议使用CSV数据转储,您可以使用LOAD DATA INFILE语法加载它们。 See: http://dev.mysql.com/doc/refman/5.1/en/load-data.html 请参阅: http//dev.mysql.com/doc/refman/5.1/en/load-data.html

Of course, you would still need to ensure the target tables exist, but if you know you only have to parse table creation DDL stattemnts, that will drastically simplify your java code. 当然,您仍然需要确保目标表存在,但是如果您知道只需要解析表创建DDL stattemnts,那么这将极大地简化您的Java代码。

Note that you can use mysqldump to extract CSV data from your database, see: http://dev.mysql.com/doc/refman/5.1/en/mysqldump.html#option_mysqldump_tab 请注意,您可以使用mysqldump从数据库中提取CSV数据,请参阅: http//dev.mysql.com/doc/refman/5.1/en/mysqldump.html#option_mysqldump_tab

Effective solution can be found here: 有效的解决方案可在此处找到:

https://stackoverflow.com/a/1044837 https://stackoverflow.com/a/1044837

This explains how to run any sql script over jdbc. 这解释了如何通过jdbc运行任何sql脚本。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM