saran
saran

Reputation: 1291

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 table 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.

Upvotes: 0

Views: 1027

Answers (4)

Nikhil
Nikhil

Reputation: 590

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

Upvotes: 1

fredt
fredt

Reputation: 24352

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

Upvotes: 3

JB Nizet
JB Nizet

Reputation: 691625

This is not possible. You'll have to regularly poll the database in order to detect changes.

Upvotes: 0

adarshr
adarshr

Reputation: 62573

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.

Upvotes: 0

Related Questions