我正在编写一个具有数据访问层的应用程序,以抽象到SQLITE3或MySQL数据库的底层连接。
多亏了这里的一些帮助,昨天向我展示了如何使用进程构建器通过输出重定向运行到SQLITE3 DB的命令行导入。
现在,我正在尝试通过导入转储文件在MySQL中创建相同的数据库。从命令行客户端加载可以很好地工作。我只需要告诉它获取文件的源文件,数据库就创建成功了。
但是,我试图在运行时通过代码来实现这一点,而我用于执行SQL语句的方法无法执行源命令。
我怀疑这是因为"source“不是SQL,但我不知道还能用什么来尝试和运行它。
我的错误消息是:
java.sql.SQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'source /tmp/ISMCoreActionPack_mysql.sql' at line 1失败的命令字符串:
source /tmp/ISMCoreActionPack_mysql.sql;我的方法是:
public Boolean executeSqlStatement(String sql) {
Boolean rc = false;
try {
Connection connection = getConnection();
Statement statement = connection.createStatement();
rc = statement.execute(sql);
connection.close();
} catch (SQLException e) {
e.printStackTrace();
System.err.println(e.getClass().getName() + ": " + e.getMessage());
System.exit(1);
}
return rc;
}有谁能建议如何做到这一点吗?
发布于 2018-08-27 00:51:47
你不能运行'source‘命令,因为JDBC驱动程序不支持它,只支持MySQL。
以下是我给你的建议。编写一些解析器,它从文件中读取查询,并使用JDBC语句执行它们。
发布于 2018-08-27 05:11:35
source不是MySQL语言的一部分;它是一个MySQL外壳命令。不过,您不应该编写自己的解析器。您可以像this answer中解释的那样使用SqlTool。
https://stackoverflow.com/questions/52028320
复制相似问题