Querying a database using Java

I got stuck at some point when I need to get database changes in Java code. The query is to update, add, delete any record in any db table; must be recognized by the java program. How can I implement JMS? or java thread?

Update: Thank you guys for your support. I actually use Oracle as DB and Weblogic 10.3. Actually, I want to receive updates from a table in which I only have read permission, so guys what you all offer. I can not update the database. The only thing I can do is just read the database and, if there are any changes to the table, I should get information / notification that certain rows of data have been added / deleted or updated.

+5
source share
7 answers

If the database cannot send the message in Java, you will need to poll this thread.

A better, more efficient model will be the one that works when changes occur. A database running Java internally (e.g. Oracle) can do this.

+8
source

We do this by polling the database using the EJB timer task. In fact, we have a status that we update when we process this line.

So, the EJB timer thread invokes a procedure that captures rows that are marked as "raw".

Dirty, but also very simple and reliable. Especially after a crash or something else, he can still rise from the place where he crashed without much difficulty.

, (, ).

+6

, , Tib Rendezvous, Java-.

IMHO ( multi-cast, Tib ..). , .

+3

, , - . - , Java , . Java, DAO EJB, ( , ).

+1

- API - , , JMS API, . , .

+1

. . / , Java- , .

datamodel, , , /. /. , , , MySQL:

CREATE TABLE mytable (
    id BIGINT NOT NULL AUTO_INCREMENT PRIMARY KEY,
    somevalue VARCHAR(255) NOT NULL,
    lastupdate TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    INDEX (lastupdate)
)

/ lastupdate . INSERT INTO mytable (somevalue) VALUES (?) UPDATE mytable SET somevalue = ? WHERE id = ?, .

, Java , ( Timer TimerTask ScheduledExecutorService Runnable Callable), :

Date now = new Date();
statement = connection.prepareStatement("SELECT id FROM mytable WHERE lastupdate BETWEEN ? AND ?");
statement.setDate(1, this.lastTimeChecked);
statement.setDate(2, now);
resultSet = statement.executeQuery();
while (resultSet.next()) {
    // Handle accordingly.
}
this.lastTimeChecked = now;

: , . , / . Java- / (, ) SQL- , .

+1

. , .

:

  • pk
  • , , + .
  • // .
  • Java , , .

: ? ? ?

0

All Articles