首页 > 编程知识 正文

java由mysql触发器运行,mysql中触发器的创建

时间:2023-12-27 22:27:53 阅读:326918 作者:VOPJ

本文目录一览:

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

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

java如何调用MySQL的触发器

触发器顾名思意就是在某个动作执行时自动触发执行的,不用调用,比如你是在add和delete数据时加触发器,只要你定义的对,数据库在向你指定的那张表add和delete数据时,该触发器就会自动触发

java中怎么创建mysql的触发器

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();

mysql触发器

触发器只有经事件触发后才能执行,上面对时间的判断在触发器里面,没有事件触发是不会运行的,这种情况一般利用mysql的事务调度器(event scheduler)和存储过程比较容易实现。具体如下:

事务调度器

CREATE EVENT `del_lclass_tbl ` ON SCHEDULE EVERY 1 DAY STARTS '2009-05-11 01:00:00' ON COMPLETION NOT PRESERVE ENABLE DO call sp_delete_lclass_tbl(null);

存储过程

delimiter $

drop procedure if exists sp_delete_lclass_tbl;$

create procedure sp_delete_lclass_tbl()

begin

delete from lclass_tbl where lClass_nStatus in (1,3)

and lClass_nExecuteID in (select PPI_NID from planpracticeitem_tbl where ppi_sApplicationEndDate now());

end;$

delimiter ;

mysql触发器原生支持调用外部程序么

mysql 通过函数执本地命令、外部程序

昨天接到一个需求,要求在mysql的触发器中执行一个外部程序。

一开始没有什么头绪,后来发现嘿嘿。

id=211

有个叫mysqludf的一个东西,用起来还不错。

不过这个东西仅仅在linux下试了试,效果还行。

步骤如下:

一、解压附件的压缩包之后

如果不想自己编译的话,把lib_mysqludf_sys.so文件放到 mysql的lib/mysql/plugin/

目录下。

二、执行chcon -t texrel_shlib_t mysql/lib/mysql/plugin/lib_mysqludf_sys.so

三、创建函数

DROP FUNCTION IF EXISTS lib_mysqludf_sys_info;

DROP FUNCTION IF EXISTS sys_get;

DROP FUNCTION IF EXISTS sys_set;

DROP FUNCTION IF EXISTS sys_exec;

DROP FUNCTION IF EXISTS sys_eval;

CREATE FUNCTION lib_mysqludf_sys_info RETURNS string SONAME 'lib_mysqludf_sys.so';

CREATE FUNCTION sys_get RETURNS string SONAME 'lib_mysqludf_sys.so';

CREATE FUNCTION sys_set RETURNS int SONAME 'lib_mysqludf_sys.so';

CREATE FUNCTION sys_exec RETURNS int SONAME 'lib_mysqludf_sys.so';

CREATE FUNCTION sys_eval RETURNS string SONAME 'lib_mysqludf_sys.so';

四、测试

1、准备sh文件

在linux系统中执行下面的命令

su mysql

mkdir /mysqlUDFtest

cd mysqlUDFtest

vi test.sh

#/bin/sh

date testlog.txt

chmod +x ./test.sh

2、准备数据库表和触发器

选择一个数据库执行如下命令:

CREATE TABLE test1(a1 INT) type=InnoDB;

CREATE TABLE test2(a2 INT) type=InnoDB;

CREATE TABLE test3(a3 INT NOT NULL AUTO_INCREMENT PRIMARY KEY) type=InnoDB;

CREATE TABLE test4(

a4 INT NOT NULL AUTO_INCREMENT PRIMARY KEY,

b4 INT DEFAULT 0

) type=InnoDB;

DELIMITER |

DROP TRIGGER /*!50032 IF EXISTS */ `test`.`testref`|

create trigger `test`.`testref` BEFORE INSERT on `test`.`test1`

for each row BEGIN

DECLARE done INT DEFAULT 999;

INSERT INTO test2 SET a2 = NEW.a1;

DELETE FROM test3 WHERE a3 = NEW.a1;

UPDATE test4 SET b4 = b4 + 1 WHERE a4 = NEW.a1;

set done = sys_exec("/mysqlUDFtest/test.sh");

IF done != 0 then

INSERT INTO `$amp;amp ;$gt;22.t3`="" (a,b); end="" if;END;

|

Mysql触发器可以调用Java或者js程序吗

调用js就有点复杂了

调用java用轮循的方式,java循环去查数据库,根据相应的规则执行java代码

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。