且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何使用“源"运行脚本文件?命令?

更新时间:2023-10-21 09:57:34

您无法使用 JDBC 驱动程序执行此操作.source 只是 MySQL 命令行工具支持的一个命令.看这里:

You cannot do this with the JDBC driver. source is only a command supported by the MySQL command line tool. See here:

http://forums.mysql.com/read.php?39,406094,406329#msg-406329

这是命令行工具的命令列表.大多数支持作为 JDBC 查询语句.

Here's the list of commands for the command-line tool. Most are not supported as JDBC query statements.

http://dev.mysql.com/doc/refman/5.5/en/mysql-commands.html

您必须从代码中的文件加载 SQL 命令,并将它们发送到 JDBC 执行方法.类似的东西:

You will have to load your SQL commands from the file in your code and send them to JDBC execute methods. Something like:

Statement stm = con.createStatement();
BufferedReader reader = new BufferedReader(new FileReader(new File(...)));
while (true) {
    String line = reader.readLine();
    if (line == null) {
        break;
    }
    // this is the trick -- you need to pass different SQL to different methods
    if (line.startsWith("SELECT")) {
        stm.executeQuery(line);
    } else if (line.startsWith("UPDATE") || line.startsWith("INSERT")
        || line.startsWith("DELETE")) {
        stm.executeUpdate(line);
    } else {
        stm.execute(line);
    }
}
stm.close();