gpt4 book ai didi

java - 用于自动更新的mysql中的触发器出错

转载 作者:太空宇宙 更新时间:2023-11-03 10:54:14 25 4
gpt4 key购买 nike

My Previous question 的回答是对的。触发器出错

这里我在我的程序中使用它出现了以下错误

2014-02-25T11:51:52.737+0530  ERROR  line 1:2: unexpected token: CREATE
2014-02-25T11:51:52.737+0530 ERROR line 1:2: unexpected token: CREATE
line 1:2: unexpected token: CREATE

这是代码

@SuppressWarnings("unchecked")
public static void expMember(){


Session session = HibernateUtil.getSessionFactory().getCurrentSession();
session.setFlushMode(FlushMode.MANUAL);
session.beginTransaction();

java.util.Date utilDate=new java.util.Date();
java.sql.Date sqldate=new java.sql.Date(utilDate.getTime());

List<Member> members = (List<Member>) session.createQuery(
"from Member where exp_status = 'false' ").list();;

for(Member i : members){

Date exp=i.getRenewDate();

try {

members = (List<Member>) session.createQuery(
" CREATE TRIGGER autoupdation_"+ sqldate +" BEFORE UPDATE ON tbl-member FOR EACH
ROW BEGIN IF"+ exp .compareTo(sqldate)+" THEN SET NEW.status = 1;
ELSE SET NEW.status = 0; END IF;END;").list();

}

catch(Exception exception){

}
}
}

最佳答案

您正在尝试运行原生 sql 查询,因此对于那些请使用 createSQLQuery 而不是 createQuery

还有为什么要动态创建触发器。您在循环中创建触发器的逻辑是错误的。您只需创建一个触发器,它可以在创建数据库时完成。

The MySQL Table Editor in MYSQL workbench  provides a work space that has tabs used to perform these actions:

Columns: Add or modify columns

Indexes: Add or modify indexes

Foreign Keys: Add or modify foreign keys

Triggers: Add or modify triggers

Partitioning: Manage partitioning

Options: Add or modify various general, table, and row options

请检查 https://docs.jboss.org/hibernate/orm/3.3/reference/en-US/html/querysql.html http://www.mkyong.com/hibernate/hibernate-native-sql-queries-examples/

https://dev.mysql.com/doc/workbench/en/wb-table-editor.html

关于java - 用于自动更新的mysql中的触发器出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22006684/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com