简体   繁体   English

在mysql5.5上通过JDBC创建触发器时出错

[英]Error while creating trigger through JDBC on mysql5.5

This is my code : 这是我的代码:

triggerBuilder.append("DROP TRIGGER IF EXISTS `insert_associated_inquiry`; ");
triggerBuilder.append(" DELIMITER %% ");

triggerBuilder.append(" CREATE TRIGGER insert_associated_inquiry BEFORE UPDATE ON inquiry ");
triggerBuilder.append(" FOR EACH ROW Begin ");    

triggerBuilder.append(" insert into associated_inquiries(inquiry_id , subject , content , inquiry_date , preferred_date ) " );
triggerBuilder.append("values");
        triggerBuilder.append(" ( " );
            triggerBuilder.append(" OLD.id , ");
            triggerBuilder.append(" OLD.subject , " );
            triggerBuilder.append(" OLD.content , " );
            triggerBuilder.append(" OLD.created_on , " );
            triggerBuilder.append(" OLD.preffered_date " );
        triggerBuilder.append(" ) ; ");

triggerBuilder.append(" END %% ");

triggerBuilder.append(" DELIMITER ; ");

con.createStatement().execute(triggerBuilder.toString());

And this is the error thrown : 这是抛出的错误:

 com.mysql.jdbc.exceptions.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 TRIGGER insert_associated_inquiry 
 BEFORE UPDATE ON inquiry ' at line 1

What could be the reason and solution for this error. 可能是此错误的原因和解决方案。 Help please.Thanks. 请帮助。谢谢。

Don't use delimiters with JDBC and MySQL. 不要在JDBC和MySQL中使用定界符。 Delimiters are only used by the MySQL console so that it can tell when a trigger, stored procedure, etc that you're typing in has ended. 分隔符仅由MySQL控制台使用,以便它可以告知您键入的触发器,存储过程等何时结束。 In JDBC, you put the whole SQL string together and then send it to the database. 在JDBC中,您将整个SQL字符串放在一起,然后将其发送到数据库。 Because you're in control of when the SQL gets sent to the database, there's no need to use delimiters. 因为您可以控制何时将SQL发送到数据库,所以无需使用定界符。

I removed the two DELIMITER lines and the use of the %% delimiter from your code, and sent the DROP TRIGGER command to the database separately. 我从代码中删除了两条DELIMITER行和%%分隔符的使用,并将DROP TRIGGER命令分别发送到数据库。 The code I was left with is as follows: 我剩下的代码如下:

con.createStatement().execute("DROP TRIGGER IF EXISTS `insert_associated_inquiry`");

triggerBuilder.append(" CREATE TRIGGER insert_associated_inquiry BEFORE UPDATE ON inquiry ");
triggerBuilder.append(" FOR EACH ROW Begin ");    

triggerBuilder.append(" insert into associated_inquiries(inquiry_id , subject , content , inquiry_date , preferred_date ) " );
triggerBuilder.append("values");
        triggerBuilder.append(" ( " );
            triggerBuilder.append(" OLD.id , ");
            triggerBuilder.append(" OLD.subject , " );
            triggerBuilder.append(" OLD.content , " );
            triggerBuilder.append(" OLD.created_on , " );
            triggerBuilder.append(" OLD.preffered_date " );
        triggerBuilder.append(" ) ; ");

triggerBuilder.append(" END ");

con.createStatement().execute(triggerBuilder.toString());

This code appeared to work, in that I could run this code without error regardless of whether the trigger already existed. 该代码似乎有效,因为无论触发器是否存在,我都可以无错误地运行此代码。 If the trigger did not previously exist it was created. 如果触发器以前不存在,则会创建它。

原因是因为“定界符”不是mysql标准的一部分,只需删除定界符语句,您的代码就可以工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM