无法从 .sql 文件创建存储过程 - JDBC

发布于 2025-01-06 23:21:37 字数 1497 浏览 0 评论 0原文

我正在尝试使用 jdbc 在安装 Web 项目期间执行 .sql 文件。谷歌建议的 2 个选项:手动解析脚本(通过拆分“;”字符)或使用 ant。我更喜欢简单的方式,所以 ant 是一个不错的选择。这是我用来设置数据库的代码:

public void executeSql(String sqlFilePath) {
        final class SqlExecuter extends SQLExec {
            public SqlExecuter() {
                Project project = new Project();
                project.init();
                setProject(project);
                setTaskType("sql");
                setTaskName("SQL Init");
            }
        }

        SqlExecuter executer = new SqlExecuter();
        executer.setSrc(new File(sqlFilePath));
        executer.setDriver("com.mysql.jdbc.Driver") ;
        executer.setPassword("123456");
        executer.setUserid("root");
        executer.setUrl("jdbc:mysql://localhost:3306/abc");
        executer.execute();
    }

代码工作正常,直到它满足创建过程的部分

DELIMITER //
CREATE PROCEDURE LOG (IN period INT)
BEGIN
    INSERT INTO log_archive
        SELECT * FROM ablog
            WHERE log_date < DATE_SUB(CURRENT_DATE(), INTERVAL period DAY);
    DELETE FROM ablog
        WHERE log_date < DATE_SUB(CURRENT_DATE(), INTERVAL period DAY);
END//
DELIMITER ;

并给出此错误:

引起:com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException:您的 SQL 语法有错误;检查与您的 MySQL 服务器版本相对应的手册,了解在第 1 行的“DELIMITER // CREATE PROCEDURE LOG(IN period INT) BEGIN INSERT INTO log_archi”附近使用的正确语法

如果我删除存储过程部分,那么它将运行良好。 sql 文件也从 mysql 命令提示符成功执行。您有什么建议来解决这个问题吗?

I am trying to execute a .sql file during the installation of my web project by using jdbc. 2 options suggested from google: either parsing the script manually (by splitting the ";" character) or using ant. I prefer the simple way so ant is a good choice. This is the code i use to set up the database:

public void executeSql(String sqlFilePath) {
        final class SqlExecuter extends SQLExec {
            public SqlExecuter() {
                Project project = new Project();
                project.init();
                setProject(project);
                setTaskType("sql");
                setTaskName("SQL Init");
            }
        }

        SqlExecuter executer = new SqlExecuter();
        executer.setSrc(new File(sqlFilePath));
        executer.setDriver("com.mysql.jdbc.Driver") ;
        executer.setPassword("123456");
        executer.setUserid("root");
        executer.setUrl("jdbc:mysql://localhost:3306/abc");
        executer.execute();
    }

The code working fine until it meets the part of creating the procedure

DELIMITER //
CREATE PROCEDURE LOG (IN period INT)
BEGIN
    INSERT INTO log_archive
        SELECT * FROM ablog
            WHERE log_date < DATE_SUB(CURRENT_DATE(), INTERVAL period DAY);
    DELETE FROM ablog
        WHERE log_date < DATE_SUB(CURRENT_DATE(), INTERVAL period DAY);
END//
DELIMITER ;

and give this error:

Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: 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 'DELIMITER // CREATE PROCEDURE LOG(IN period INT) BEGIN INSERT INTO log_archi' at line 1

if i remove the stored procedure part, then it will run well. The sql file is also executed succesfully from the mysql command prompt. Do you have any suggestion to solve this problem ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

痴者 2025-01-13 23:21:37

考虑使用像 liquibase 这样的工具。它支持mysql,但有一些小问题。

Consider using a tool like liquibase. It supports mysql, there are some minor issues though.

青衫儰鉨ミ守葔 2025-01-13 23:21:37

试试这个方法
http://forums.mysql.com/read.php?46,271411,271411

CREATE PROCEDURE LOG (period INT)
BEGIN
    INSERT INTO log_archive
        SELECT * FROM ablog
            WHERE log_date < CURRENT_DATE- INTERVAL period DAY;
    DELETE FROM ablog
        WHERE log_date < CURRENT_DATE - INTERVAL period DAY;
END

如果需要在命令行中运行它,您可以放置​​分隔符
mysql>\. yoursqlfile.sql
它刚刚在命令行中使用 delimiter //END// 运行。


命令行:

delimiter //
CREATE PROCEDURE LOG (period INT)
BEGIN
    INSERT INTO log_archive
        SELECT * FROM ablog
            WHERE log_date < CURRENT_DATE- INTERVAL period DAY;
    DELETE FROM ablog
        WHERE log_date < CURRENT_DATE - INTERVAL period DAY;
END//

希望有帮助

Try this way
http://forums.mysql.com/read.php?46,271411,271411

CREATE PROCEDURE LOG (period INT)
BEGIN
    INSERT INTO log_archive
        SELECT * FROM ablog
            WHERE log_date < CURRENT_DATE- INTERVAL period DAY;
    DELETE FROM ablog
        WHERE log_date < CURRENT_DATE - INTERVAL period DAY;
END

you can put delimiters if need be to run it in the commandline
mysql>\. yoursqlfile.sql
It has just worked from commandline with delimiter // and at END//.


commandline :

delimiter //
CREATE PROCEDURE LOG (period INT)
BEGIN
    INSERT INTO log_archive
        SELECT * FROM ablog
            WHERE log_date < CURRENT_DATE- INTERVAL period DAY;
    DELETE FROM ablog
        WHERE log_date < CURRENT_DATE - INTERVAL period DAY;
END//

hope it helps abit

蝶…霜飞 2025-01-13 23:21:37

如果您仅使用一个分隔符(例如 \\ 或我使用 $$)并且不更改中间脚本(无 DELIMITER XX)然后你就可以运行相当复杂的脚本。

dbConnection = DriverManager.getConnection(connectionString, user, password);
statement = dbConnection.createStatement();
for (String sqlpart : sqlFileContents.split("\\$\\$"))
{
    if (!sqlpart.trim().isEmpty())
        statement.addBatch(sqlpart);
}
statement.executeBatch();

例如

DROP PROCEDURE IF EXISTS Log $
CREATE PROCEDURE Log (period INT)
BEGIN
    INSERT INTO log_archive
        SELECT * FROM ablog
            WHERE log_date < CURRENT_DATE- INTERVAL period DAY;
    DELETE FROM ablog
        WHERE log_date < CURRENT_DATE - INTERVAL period DAY;
END $

If you use only one delimiter (eg. \\ or I used $$) and don't change mid script (no DELIMITER XX) then you can run reasonably complex scripts.

dbConnection = DriverManager.getConnection(connectionString, user, password);
statement = dbConnection.createStatement();
for (String sqlpart : sqlFileContents.split("\\$\\$"))
{
    if (!sqlpart.trim().isEmpty())
        statement.addBatch(sqlpart);
}
statement.executeBatch();

For example

DROP PROCEDURE IF EXISTS Log $
CREATE PROCEDURE Log (period INT)
BEGIN
    INSERT INTO log_archive
        SELECT * FROM ablog
            WHERE log_date < CURRENT_DATE- INTERVAL period DAY;
    DELETE FROM ablog
        WHERE log_date < CURRENT_DATE - INTERVAL period DAY;
END $
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文