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

 我来答
从空去听8
2017-06-09 · TA获得超过7441个赞
知道大有可为答主
回答量:6907
采纳率:93%
帮助的人:5871万
展开全部

肯定不可以,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语法错误

推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式