无法从 .sql 文件创建存储过程 - JDBC
我正在尝试使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
考虑使用像 liquibase 这样的工具。它支持mysql,但有一些小问题。
Consider using a tool like liquibase. It supports mysql, there are some minor issues though.
试试这个方法
http://forums.mysql.com/read.php?46,271411,271411
如果需要在命令行中运行它,您可以放置分隔符
mysql>\. yoursqlfile.sql
它刚刚在命令行中使用
delimiter //
和END//
运行。命令行:
希望有帮助
Try this way
http://forums.mysql.com/read.php?46,271411,271411
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 atEND//
.commandline :
hope it helps abit
如果您仅使用一个分隔符(例如
\\
或我使用$$
)并且不更改中间脚本(无DELIMITER XX
)然后你就可以运行相当复杂的脚本。例如
If you use only one delimiter (eg.
\\
or I used$$
) and don't change mid script (noDELIMITER XX
) then you can run reasonably complex scripts.For example