简体   繁体   English

我可以通过JDBC连接运行“源”命令(SQL脚本)吗?

[英]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.

相关问题 我可以为 JDBC 连接指定 *source* IP 地址吗? - Can I specify *source* IP address for a JDBC connection? 如何运行.sql脚本JDBC? - How run .sql script JDBC? 可以从JDBC中运行sqlite .import命令吗? - Can one run the sqlite .import command from within JDBC? 如何使用JDBC执行此sql备份脚本? - How can I execute this sql backup script with JDBC? 如何在没有 iBatis 的情况下在 JDBC 中运行 SQL 脚本? - How to run SQL script in JDBC without iBatis? 我如何从org.springframework.jdbc.datasource.DriverManagerDataSource类获取jdbc连接 - How can i get jdbc connection from org.springframework.jdbc.datasource.DriverManagerDataSource class oracle.jdbc.pool.OracleDataSource为每个新连接运行一个命令 - oracle.jdbc.pool.OracleDataSource run a command for every new connection 如何从命令行运行Ruby脚本并使用Java代码获取响应? - How can I run a Ruby script from command line and get the response using Java code? 我怎么能告诉Java代码运行脚本,因为它是从普通用户的命令行运行的? - How can i tell Java code run the script as it was running from command line as normal user? 如何找到在 Java 连接中运行的所有 SQL 更新(从事务开始到提交)? - How can I find all the SQL updates run in a Java Connection (from transaction start to commit)?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM