开发者

Can MySQL triggers or stored procedures be written in Java?

I have two databases. Changes like edits, insertions to one, need to be made to the second one as well and vice versa.

Actually, one database is an old legacy database (with a very bad Entity-Relationship structure) and a legacy app front-end currently used by users. The second database is a newly built, better restructure of the legacy with a separate app. front-end.

I want both apps (accessing the legacy and the new database respectively) to run simultaneously so users can have the option to use both applications and changes in one app are visible across the other.

I want to write开发者_JAVA技巧 triggers which call stored procedures, which restructure the data and put it in the opposite database.

My question is:

  • Is my line of execution as it is supposed to be? I mean, triggers >call> stored procedures >call> database.
  • Can triggers / stored procedures be written in Java?
  • Any good/ recommended tips, tutorials etc etc out there?

There are many links on google but none of them are useful. I wonder if MySQL and Java work together when it comes to MySQL triggers? is it possible or not?. Is there a better way of achieving what I need?


Triggers are named database objects. They define some action that the database should take when certain database related events occur. They are written in SQL. Their execution is transparent to the user. You write your Java JDBC code as usual and the DBMS will automatically execute the appropriate trigger whenever necessary.

mysql> delimiter //
mysql> CREATE TRIGGER insert_trigger BEFORE INSERT ON Customer
    -> FOR EACH ROW
    -> BEGIN
    -> UPDATE Customer SET Price=Price-10 WHERE CustomerGroup=32 and CityCode=11;
    -> END;
    -> //

This example shows you how to make your trigger write to another database. Be careful with auto-increment attributes.

I think you should forget about Java stored procedures in MySQL, but you could always move the business logic to your own Java program.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜