Use UDF sys_exec to do this.

You can use this link to use sys_exec function. It says,

sys_exec sys_exec takes one command string argument and executes it. Syntax

sys_exec(arg1) Parameters and Return Values

arg1 : A command string valid for the current operating system or execution environment. returns An (integer) exit code returned by the executed process. Installation

Place the shared library binary in an appropriate location. Log in to mysql as root or as another user with sufficient privileges, and select any database. Then, create the function using the following DDL statement: CREATE FUNCTION sys_exec RETURNS INT SONAME 'lib_mysqludf_sys.so'; The function will be globally available in all databases. The deinstall the function, run the following statement: DROP FUNCTION sys_exec;

For executing Java program you should fill arg1 as "java <absolute path to precompiled program to run>".  Like,sys_exec('java /home/Desktop/helloWorld') this is passed as an argument. helloWorld here is a class which is being not called by exec function.

Note: path to java should be configured before hand.

Problem: I've got a table which holds certain records. After the insert has been done, I want to call an external program (php script) via MySQL's sys_* UDFs. Now, the issue - the trigger I have passes the ID of the record to the script. When I try to pull the data out via the script, I get 0 rows. During my own testing, I came to a conclusion that the trigger invokes the php script and passes the parameters BEFORE the actual insert occured, thus I get no records for given ID. I've tested this on MySQL 5.0.75 and 5.1.41 (Ubuntu OS). I can confirm that parameters get passed to the script before actual insert happens because I've added sleep(2); to my php script and I've gotten the data correctly. Without sleep(); statement, I'm receiving 0 records for given ID.

My question is - how to fix this problem without having to hardcode some sort of delay within the php script? I don't have the liberty of assuming that 2 seconds (or 10 seconds) will be sufficient delay, so I want everything to flow "naturally", when one command finishes - the other gets executed.

I assumed that if the trigger is of type AFTER INSERT, everything within the body of the trigger will get executed after MySQL actually inserts the data.

Table layout:

CREATETABLE test (
id int notnull auto_increment PRIMARYKEY,
random_data varchar(255)notnull);

Trigger layout:

DELIMITER $$CREATETRIGGER`test_after_insert` AFTER INSERTON`test`FOR EACH ROWBEGINSET@exec_var = sys_exec(CONCAT('php /var/www/xyz/servers/dispatcher.php ', NEW.id));END;$$

DELIMITER ;

Disclaimer: I know the security issues when using sys_exec function, my problem is that the MySQL doesn't insert FIRST and THEN call the script with necessary parameters. If anyone can shed some light on how to fix this or has a different approach that doesn't involve SELECT INTO OUTFILE and using FAM - I'd be very grateful. Thanks in advance.

Yes, using the MySQL User-Defined Function (see MySQL 5.5 FAQ: Triggers) and installing thelib_mysqludf_sys

Then, for example, you can write your own trigger calling the sys_exec like this:

delimiter |CREATETRIGGER testtrigger BEFORE UPDATEON T1
FOR EACH ROWBEGINDECLARE result int(10);IF NEW.Flag <> OLD.Flag THENSET result = sys_exec('/path/to/javabin -jar your.jar');-- other kind of works and checks...ENDIF;END;|

The result contains the exit code of the external program

There are other useful functions in this library:

  • sys_eval : executes an arbitrary command, and returns it's output.
  • sys_get : gets the value of an environment variable
  • sys_set : create an environment variable, or update the value of an existing environment variable
  • sys_exec : executes an arbitrary command, and returns it's exit code

More info here

Try it on a dev env and...

Be very careful in deciding whether you need this function. UDFs are available to all database users - you cannot grant EXECUTE privileges for them. As the commandstring passed to sys_execcan do pretty much everything, exposing the function poses a very real security hazard.

use sql trigger call java function的更多相关文章

  1. java.sql.date与java.util.date区别以及数据库中插入带时分秒的时间

    java.sql.Date,java.sql.Time和java.sql.Timestamp三个都是java.util.Date的子类(包装类). java.sql.Date是java.util.Da ...

  2. 解决Apache CXF 不支持传递java.sql.Timestamp和java.util.HashMap类型问题

    在项目中使用Apache开源的Services Framework CXF来发布WebService,CXF能够很简洁与Spring Framework 集成在一起,在发布WebService的过程中 ...

  3. java.sql.Date to java.util.Date

    发这篇博文的题目可能无法直接表示内容,但是确实是java.sql.Date和java.util.Date. 今天在使用'net.sf.json.JSONObject'封装json数据的时候,碰到很奇怪 ...

  4. JAVA学习.java.sql.date 与java.util.date以及gettime()方法的分析

    java.sql.Date 是针对SQL语句使用的,它只包含日期而没有时间部分. java.util.Date 就是在除了SQL语句的情况下面使用. 它都有getTime方法返回毫秒数,返回的是自19 ...

  5. 在线数据库表(sql语句)生成java实体类工具

    相信每个做java开发的读者,都接触过SQL建表语句,尤其是在项目开发初期,因为数据库是项目的基石. 在现代项目开发中,出现了许多ORM框架,通过简单的实体映射,即可实现与数据库的交互,然而我们最初设 ...

  6. mybatis invalid comparison: java.sql.Timestamp and java.lang.String报错解决方法

    这个错的意思是:java.sql.Timestamp和java.lang.String无效的比较 错误的原因是:拿传入的时间类型参数与空字符串进行比较就会报这个异常 解决方法:只保留非null判断就可 ...

  7. java.sql.date和java.util.date的区别和转换

    不同点:java.util.Date是在除了SQL语句的情况下面使用的.java.sql.Date是针对SQL语句使用的,它只包含日期而没有时间部分java.util.Date 是 java.sql. ...

  8. 在一个千万级的数据库查寻中,如何提高查询效率?分别说出在数据库设计、SQL语句、java等层面的解决方案。

    在一个千万级的数据库查寻中,如何提高查询效率?分别说出在数据库设计.SQL语句.java等层面的解决方案. 解答: 1)数据库设计方面: a. 对查询进行优化,应尽量避免全表扫描,首先应考虑在 whe ...

  9. java.sql.Date和java.util.Date的不同和相互转换方式

    一:前言 这是我在新的公司写的第一份博客吧,来了又一个星期了吧,但是在来的那几天我真的很迷茫的感觉这里是很不适合我的样子,而且我又是来实习的,我很不愿意啊,自己做的又是java web,最原始的ser ...

随机推荐

  1. DOS批处理命令-引数取得

    参数传递对程序来说,是一个很重要的事情,所以,获得传递的参数是很重要的,接下来,我们来探讨下获得传递的参数的N种方式. 1.%N  获得传递的第N个参数(N最大为9) 就是传递过去的参数原样值(并且忽 ...

  2. Swift静态属性

    在介绍静态属性之前,我们先来看一个类的设计,有一个Account(银行账户)类,假设它有3个属性:amount(账户金额).interestRate(利率)和owner(账户名).在这3个属性中,am ...

  3. java小经验

    从事互联网金融,常常会碰到文件处理,以前都是傻傻的解析,这次我不想这么傻了,做个小小的封装,咱也以oop的思想来完成. 文件解析处理一般分两种模式:分隔符与定长,目前工作五年也就这两种. 封装思想: ...

  4. (转)深入探讨在集群环境中使用 EhCache 缓存系统

    简介: EhCache 是一个纯 Java 的进程内缓存框架,具有快速.精干等特点,是 Hibernate 中默认的 CacheProvider.本文充分的介绍了 EhCache 缓存系统对集群环境的 ...

  5. opencv 手写选择题阅卷 (一)表格设计与识别

    (一)答题表格设计与识别 实际设计好的表格如下图 为了图像精确,表格和四角的标记都是由程序生成的,文字和数据是后期排版软件添加上去的. 图中四角的四个黑方块主要用来定位表格,然后就可以切割出每个单元格 ...

  6. root-systerm-bin是什么program

    root-systerm-bin是什么program http://packages.ubuntu.com/lucid/root-system-bin

  7. 一个CFile::Remove引起的奇怪问题

    今天收到测试的一个反馈,我们的一个程序,在WIN7.WIN8下安装后,运行不起来,在进程列表中可以看到,但就是不出来窗口,同样的程序在XP下正常,在UAC关闭的情况下也正常,在以管理员权限运行时也正常 ...

  8. DTCMS自定义标签:面包屑导航,栏目中通过栏目调用名称获得栏目名称

    DTcms.Web.UI\Label\category.cs中增加标签 /// <summary> /// 自定义:通过类别name获得类别title /// </summary&g ...

  9. .net 将excel转成html文件

    最近在做一个打印预览功能,但是开始没有头绪后来用excel做了一个模板,然后根据excel模板来生成新的excel并将其存储为html,可以通过http请求在浏览器中读取,并且打印,其他的不多说.方法 ...

  10. css中单位px,em,rem的区别

    1,px像素(Pixel).相对长度单位.像素px是相对于显示器屏幕分辨率而言的. 2,em是相对长度单位.相对于当前对象内文本的字体尺寸.如当前对行内文本的字体尺寸未被人为设置,则相对于浏览器的默认 ...