本文目錄一覽:
- 1、關於mysql中的觸發器能調用JAVA嗎的搜索推薦
- 2、java如何調用MySQL的觸發器
- 3、java中怎麼創建mysql的觸發器
- 4、mysql觸發器
- 5、mysql觸發器原生支持調用外部程序么
- 6、Mysql觸發器可以調用Java或者js程序嗎
關於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代碼
原創文章,作者:RKKK,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/139903.html