永发信息网

关于mysql中的触发器能调用JAVA吗的搜索推荐

答案:2  悬赏:0  手机版
解决时间 2021-02-27 20:43
  • 提问者网友:几叶到寒
  • 2021-02-27 08:02
关于mysql中的触发器能调用JAVA吗的搜索推荐
最佳答案
  • 五星知识达人网友:玩家
  • 2021-02-27 09:29
肯定不可以,mysql不能调用java代码,但是可以在java中创建触发器
1.使用SQL创建触发器
DELIMITER $$CREATE TRIGGER `catefiles_trigger` AFTER INSERT ON `catefiles` FOR EACH ROWbegin 
declare num1 int; set num1 = (select num from est_client_catescan_status where cateid=new.cateId and taskid=new.taskId and clientid=new.clientId); if(num1>=0) then update catescan_status set num=num1+1 where cateid=new.cateId and taskid=new.taskId and clientid=new.clientId; else insert catescan_status(cateid,num,status,taskid,clientid) values(new.cateId,1,0,new.taskid,new.clientId); end if; end$$2.在Java程序里创建触发器
String sql=+" CREATE TRIGGER catefiles_trigger AFTER INSERT ON catefiles FOR EACH ROW"
+" begin"
+" declare scannum int;"
+" set scannum = (select num from est_client_catescan_status where" 
+" cateid=new.cateId and taskid=new.taskId and clientid=new.clientId);"
+" if(scannum>=0) then"
+" update catescan_status set num=scannum+1  where cateid=new.cateId and taskid=new.taskId and clientid=new.clientId;"
+" else" 
+" insert catescan_status(cateid,num,status,taskid,clientid) values(new.cateId,1,0,new.taskid,new.clientId);"
+" end if;"
+" end";
Connection con = DbConnectionManager.getConnection();
PreparedStatement  pstmt = con.prepareStatement(sql);
pstmt.execute();3.可以看出区别:在java中创建触发器,不需要限定符DELIMITER ,加上的话执行过程中会报MySQL语法错误
全部回答
  • 1楼网友:舊物识亽
  • 2021-02-27 10:20
触发器跟你是用什么语言是没有关系的.它只是在你数据库的某个表发生了改变.如做了insert update delete操作的时候它就被触发了... 所以你要清楚你是在对哪个表进行什么样的触发操作..
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯