[英]Can I run a “source” command (SQL script) from a JDBC connection?
I'm writing an application that has a data access layer to abstract the underlying connections to SQLITE3 or MySQL databases. 我正在编写一个具有数据访问层的应用程序,以抽象化到SQLITE3或MySQL数据库的基础连接。
Thanks to some help here yesterday I was shown how to use a process builder to run a command line import into the SQLITE3 DB using output redirection. 昨天在这里有了一些帮助,向我展示了如何使用流程生成器来使用输出重定向将命令行导入到SQLITE3 DB中。
Now I am trying to create the same database but in MySQL by importing a dump file. 现在,我试图通过导入转储文件在MySQL中创建相同的数据库。 The load works fine from the command line client.
从命令行客户端加载工作正常。 I just tell it to source the file and the DB is created successfully.
我只是告诉它源文件,并且数据库创建成功。
However I am trying to do this through code at runtime and my method for executing a SQL statement fails to execute the source command. 但是,我试图在运行时通过代码来执行此操作,并且我执行SQL语句的方法无法执行源命令。
I suspect that this is because "source" is not SQL but I don't know what else to use to try and run it. 我怀疑这是因为“源”不是SQL,但是我不知道尝试使用其他什么来运行它。
My error message is: 我的错误消息是:
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
The failing command string: 失败的命令字符串:
source /tmp/ISMCoreActionPack_mysql.sql;
My method is: 我的方法是:
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;
}
Can anyone suggest how to do this? 谁能建议该怎么做?
You cannot run 'source' command, because it's not supported by JDBC driver, only MySQL. 您不能运行'source'命令,因为JDBC驱动程序不支持它,只有MySQL不支持。
My advice to you the following. 我对您的建议如下。 Write some parser, which reads queries from a file, and executes them using JDBC statements.
编写一些解析器,该解析器从文件读取查询,并使用JDBC语句执行查询。
source
is not part of MySQL's dialect of SQL; source
不是MySQL的SQL方言的一部分; it is a MySQL shell command. 这是一个MySQL Shell命令。 Still, you shouldn't need to write your own parser.
尽管如此,您仍不需要编写自己的解析器。 You could use something like SqlTool as explained in this answer .
您可以使用SqlTool之类的方法,如本答案所述 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.