Newbie question: java batch scripts?

J

JGH

What's the "right" way to write a batch script in java?

I've written an object that allows me to get data from a database. I can
use this object via jsp. Now I want to write a script that will be run
nightly via a cron job.

Say, for example, I have an object that allows me to
insert/update/delete records from an Oracle table. I want to open a text
file and import those rrecords into the Oracle table.

In perl, I'd include the code for the object via a "use" statement. But
in java, do I have to create another class to extend the class that
does the actual work? And then, perhaps, in cron I'd put:

0 0 * * * /usr/local/bin/java import.class

I just want to make sure I'm doing it "right". The above cron entry
seems kind of strange. I'd rather say:

0 0 * * * /home/inventory/import

And in /home/inventory/import

#!/usr/local/bin/java
[java code here]

But maybe I'm just thinking in perl.
 
W

Wendy S

JGH said:
I've written an object that allows me to get data from a database. I can
use this object via jsp. Now I want to write a script that will be run
nightly via a cron job.

Good job, that will separate out the data access code so you can reuse it.
I'm going to call this class OracleDAO (Oracle Data Access Object) since I
don't know what you named it.
In perl, I'd include the code for the object via a "use" statement. But
in java, do I have to create another class to extend the class that
does the actual work?

Close... the class that does the actual work will probably have
import package.name.OracleDAO;
at the top, and then it will instantiate and call methods on an object of
that type. I'll call it ImportData.
And then, perhaps, in cron I'd put:
0 0 * * * /usr/local/bin/java import.class

Leave off the .class part, and Java class names usuallly start with an
uppercase letter. Maybe:
0 0 * * * /usr/local/bin/java package.name.ImportData

The reason you can't just put a bunch of Java code in a "script" is that it
needs to be compiled before you can run it. Perl, I believe, is interpreted
at runtime. (??) So there's an extra step with Java.

BeanShell might be worth a look: http://www.beanshell.org/home.html
 
K

klynn47

Normally when I write scripts to invoke the Java interpreter, I do the
following in sh.

#!/bin/sh

CLASSPATH=.... /path-jsdk/bin/java ...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,535
Members
45,008
Latest member
obedient dusk

Latest Threads

Top