tags:

views:

625

answers:

2

Are there any JAVA API's for polling a database.

Should be able to a) Be able to get all the data from a table to start with. b) Poll it every configurable minutes.

+2  A: 

In principle JDBC combined with the Timer class should be enough to handle the requirement that you have set forth.

The Timer API page describes how to schedule a task, that can be fired periodically. The wikipedia page on Java Database Connectivity contains a pretty good code sample on how to query all the data on a specific table. Tying these two items together should give you what you want.

There is more complicated ways to achieve what you desire, such as using Quartz as a Job Scheduler. But for such a simple application, I would probably stick with the build-in libraries.

Mario Ortegón
+1  A: 

Technically, that's what an entity bean does.

So, if you feel like it, run your code in a EJB container :) I'm half-serious : you could configure it to be very lightweight (Tomcat+OpenEJB for example), and ejb 3 won't make your code dependant on that technology. That would avoid writing any code for this task.

Sun doc:

An important parameter for tuning read-only beans is the refresh period, represented by the deployment descriptor entity refresh-period-in-seconds. For CMP beans, the first access to a bean loads the bean’s state. The first access after the refresh period reloads the data from the database. All subsequent uses of the bean uses the newly refreshed data (until another refresh period elapses). For BMP beans, an ejbLoad() method within an existing transaction uses the cached data unless the refresh period has expired (in which case, the container calls ejbLoad() again).

This parameter enables the EJB component to periodically refresh its “snapshot” of the database values it represents. If the refresh period is less than or equal to 0, the bean is never refreshed from the database (the default behavior if no refresh period is given).

BraveSirFoobar