java-无法从.sql文件创建存储过程-JDBC
作者:互联网
我正在尝试使用jdbc在安装Web项目的过程中执行.sql文件.谷歌建议的2个选项:手动解析脚本(通过拆分“;”字符)或使用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 ;
并给出此错误:
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文件.您对解决这个问题有什么建议吗?
解决方法:
考虑使用诸如liquibase之类的工具.它支持mysql,尽管有一些小问题.
标签:jdbc,java,mysql,stored-procedures,ant 来源: https://codeday.me/bug/20191201/2082854.html