Java run by MySQL trigger

后端 未结 3 1844
太阳男子
太阳男子 2021-01-05 07:40

I am trying to create some MySQL code that will invoke a Java program from a trigger.

Here is what I have so far:

CREATE TRIGGER trig_name after inse         


        
相关标签:
3条回答
  • 2021-01-05 07:49

    A direct answer: no you can't call a java method from a mysql trigger. If you had an oracle database you could, but not mysql.

    To do what you want to do with mysql you can

    • make the code that updates the database also notify the swing application. Or you can
    • make the trigger accumulate data on pending operations in a separate table that you read periodically from the swing app.
    0 讨论(0)
  • 2021-01-05 07:51

    Calling a java method from an SQL database isn't a standard feature. The Informix DB can call a shell script from a stored procedure, but I don't know of a feature like this in MySQL (I'm not an expert on mysql).

    The closest thing that works with all databases would be to have a thread and periodically poll the database for new records.

    SELECT * FROM studentinfo WHERE id > last_seen_id
    

    Or you could use a timestamp:

    SELECT * FROM studentinfo WHERE create_date >= last_seen_create_date
    

    In this case you would have to filter duplicated rows which have already loaded from the previous run.

    0 讨论(0)
  • 2021-01-05 08:01

    Though not a standard feature this is very well possible with MySQL. You can use the SELECT .. INTO OUTFILE statement from inside the trigger to write to a named pipe (Windows) or memroy filesystem (Linux). Both of those can easily be monitored from Java code (or any other code for that matter). Using this technique you will avoid polling and because no actual disk access takes place either you will have good performance.

    I have written a Java package for this actually so I'm 100% sure it is possible and performs well. Unfortunately I am not allowed to share my efforts here (my previous answer was deleted by a moderator) so you will have to code it yourself, sorry.

    0 讨论(0)
提交回复
热议问题