Link to home
Start Free TrialLog in
Avatar of Errang Genevre
Errang Genevre

asked on

Continuously query a database in Java

Hello,

I'm trying to see if its possible to maintain a connection to a database and get the new records as they are inserted into the table.

I'm connecting to a db2 database, and I have no idea when the table gets updated (not regular intervals).

Appreciate any help!
SOLUTION
Avatar of Member_2_2484401
Member_2_2484401
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of dpearson
dpearson

Like Dave said, you need to poll to get this information - i.e. run a query every so many minutes or seconds to see if there's new data.

Personally, I'd suggest just keeping track of the primary key of the table and asking for any records that have a value larger than the primary key that you last loaded.  That way you can be sure the query is efficient (as searches by primary key are always indexed).

Doug
Avatar of Errang Genevre

ASKER

There is no timestamp column, and the "primary key" is a composite key; so I have no easy way to tell what's added... And to make matters worse, I have no guarantees that newer entries keys will be greater than the previous entries.

Also, I can't change the table in anyway, or have a view/triggers, nothing... All I have is a select statement.

Any hopes of achieving it with just that and some form of clever looping strategy in Java?
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Sadly no... Working on proprietary systems so I can't share any schema details; but there is no auto increment column, and I don't have access to any other tables.

I knew it was a long shot; but I was hoping Java had some features I didn't know about, maybe keeping the connection allowed Java to maybe keep tabs on the table or something.
This is of course why it's often a good idea to have a timestamp 'updated' column in tables
You aren't authorized to disclose table definitions, modify table definitions, create SPs/triggers nor perhaps do much that's truly useful (outside of a Java app). However, should we also assume that a customer DBA can't do those things for you?

There may numerous things that could be done.

What DB2 are you connecting to? (LUW? For i? For z?) Version?
Nope, can't do modifications of any sort... Not entirely sure of the database version myself.