不能从.sql文件存储过程 - JDBC [英] Cannot create stored procedure from .sql file - JDBC

查看:222
本文介绍了不能从.sql文件存储过程 - JDBC的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图通过使用JDBC安装我的web项目的过程中执行一个.sql文件。从谷歌建议2个选择:要么手动解析脚本(通过拆分;字符)或使用蚂蚁。我preFER简单的方式,使蚂蚁是一种不错的选择。这是code我用它来建立数据库:

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();
    }

在code工作正常,直到它符合创建过程的一部分

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 ;

和给这个错误:

产生的原因:com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException:你在你的SQL语法错误;检查对应于你的MySQL服务器版本在1号线附近使用'D​​ELIMITER // CREATE PROCEDURE LOG(期INT)BEGIN INSERT INTO log_archi'正确的语法手册

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

如果我删除存储过程的一部分,那么它会运行良好。也从mysql命令提示符下成功执行SQL文件。你有什么建议,以解决这个问题?

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 ?

推荐答案

考虑使用像 liquibase 。它支持MySQL,也有一些小问题,但。

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

这篇关于不能从.sql文件存储过程 - JDBC的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆