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. Android中SurfaceView的使用详解

    Android中SurfaceView的使用详解 http://blog.csdn.net/listening_music/article/details/6860786 Android NDK开发 ...

  3. ASP.NET中页面加载时文本框(texbox控件)内有文字获得焦点时文字消失

    代码如下: <asp:TextBox ID="TextBox1" runat="server" Height="26px" MaxLe ...

  4. Manifest文件简介

    每个Android项目都包含一个Manifest文件-Android Manifest.xml,它存储在项目层次中的最底层.Manifest可以定义应用程序及其组件和需求的结构和元数据. 它包含了组成 ...

  5. windows 7 64 bit 使用 virtual box 的经验

    本人电脑是联想thinkpad E535的机子,安装的是64bitwindows7 旗舰版 为了更好的工作,我安装了虚拟机virtualbox最新版 很不幸,我出现了多次蓝屏的情况,我换到32位系统下 ...

  6. 【Android】Android SDK在线更新镜像服务器

    Android SDK在线更新镜像服务器 中国科学院开源协会镜像站地址: IPV4/IPV6: http://mirrors.opencas.cn 端口:80 IPV4/IPV6: http://mi ...

  7. 一款兼容pc 移动端的tab切换

    利用touchslider.js插件来制作的tab切换,可任意修改很方便~~~ 样式: <style> .box-163css{ width:100%; position:relative ...

  8. PHPExcel读取excel03/07版到数组

    想总结下PHPExcel的读取excel到数组的function,用的时候直接调取,懒…… 参考了以下链接: http://www.jb51.net/article/29071.htm http:// ...

  9. Catalyst揭秘 Day1 Catalyst本地解析

    Catalyst揭秘 Day1 Catalyst本地解析 今天开始讲下Catalyst,这是我们必须精通的内容之一: 在Spark2.x中,主要会以Dataframe和DataSet为api,无论是D ...

  10. Python学习笔记2——模块的发布

    1.为模块nester创建文件夹nester,其中包含:nester.py(模块文件): """这是"nester.py"模块,提供了一个名为prin ...