hsql database connection using java
I have a scenario where my Java program has to continuously communicate with the database table, for example my Java program has to get the data of my ta开发者_如何学JAVAble when new rows are added to it at runtime. There should be continuous communication between my program and database.
If the table has 10 rows initially and 2 rows are added by the user, it must detect this and return the rows.
With HSQLDB, this is supported without continuous polling.
HSQLDB TRIGGERs support synchronous or asynchronous notifications to be sent by the trigger event. The target may be the user's app or any other app.
See http://hsqldb.org/doc/2.0/guide/triggers-chapt.html
For HSQL you can use trigger. Ref: http://hsqldb.org/doc/2.0/guide/triggers-chapt.html
Trigger Action in Java
A trigger action can be written as a Java class that implements the org.hsqldb.Trigger interface. This interface has a single method which is called when the trigger is activated, either before or after the event. When the method is called by the engine, it supplies the type of trigger as an int value defined by the interface(as type argument), the name of the trigger (as trigName argument), the name of the table (as tabName argument), the OLD ROW (as oldRow argument) and the NEW ROW (as newRow argument). The oldRow argument is null for row level INSERT triggers. The newRow argument is null for row level DELETE triggers. For table level triggers, both arguments are null (that is, there is no access to the data). The triggerType argument is one of the constants in the org.hsqldb.Trigger interface which indicate the type of trigger, for example, INSERT_BEFORE_ROW or UPDATE_AFTER_ROW.
CREATE TRIGGER t BEFORE UPDATE ON customer
REFERENCING NEW AS newrow FOR EACH ROW
BEGIN ATOMIC
IF LENGTH(newrow.firstname) > 10 THEN
CALL my_java_function(newrow.firstname, newrow.lastname);
END IF;
END
What do you mean by continuous communication? The connection to database should be kept open once established until you finish the task here. But you will have to keep polling to fetch new records.
This is not possible. You'll have to regularly poll the database in order to detect changes.
精彩评论