Pre-newbie help needed

Warning: I know zero about Java :slight_smile:

I am trying to connect (read only) to an IBM Cloudscape database. This
is what eventually became Derby. I have a list of jar files:

db2j.jar
db2jcc.jar
db2jcview.jar
db2jnet.jar
db2jtools.jar

that a real Java application uses to access the same database. The
database is in a local directory like:

find db -type f -print | head -20
db/OpcClientDb/db.lck
db/OpcClientDb/dbex.lck
db/OpcClientDb/log/log.ctrl
db/OpcClientDb/log/log48978.dat
db/OpcClientDb/log/logmirror.ctrl
db/OpcClientDb/seg0/A1
db/OpcClientDb/seg0/c10.dat
db/OpcClientDb/seg0/c101.dat
db/OpcClientDb/seg0/c111.dat
db/OpcClientDb/seg0/c121.dat
db/OpcClientDb/seg0/c130.dat
db/OpcClientDb/seg0/c141.dat
db/OpcClientDb/seg0/c150.dat
ā€¦

Poking at the internet so far Iā€™ve some up with this:

#! /usr/bin/env jruby

include Java

require ā€˜lib/db2j.jarā€™
require ā€˜lib/db2jcc.jarā€™
require ā€˜lib/db2jcview.jarā€™
require ā€˜lib/db2jnet.jarā€™
require ā€˜lib/db2jtools.jarā€™

url = ā€˜jdbc:db2j:db/OpcClientDbā€™
conn = java.sql.DriverManager.get_connection(url)

which gets me:

DriverManager.java:602:in `getConnectionā€™: java.sql.SQLException: No
suitable driver found for jdbc:db2j:db/OpcClientDb
(plus a stack trace)

So I am guessing that I need to load the classes in the jar files but I
canā€™t figure out how to do that. Iā€™ve tried things like:

import ā€œcom.ibm.db2jā€
or
include_class java::db2j
or
java_import java.lang.com.ibm.db2j

all of which donā€™t seem to be even close to what I need.

If I do:

jar tf lib/db2j.jar | head -20

I get things like:

com/ibm/db2j/aggregates/AggregateDefinition.class
com/ibm/db2j/aggregates/Aggregator.class
com/ibm/db2j/authentication/UserAuthenticator.class
com/ibm/db2j/catalog/AliasInfo.class
com/ibm/db2j/catalog/DefaultInfo.class
com/ibm/db2j/catalog/DependableFinder.class
com/ibm/db2j/catalog/IndexDescriptor.class
com/ibm/db2j/catalog/ReferencedColumns.class
com/ibm/db2j/catalog/Statistics.class
com/ibm/db2j/catalog/TypeDescriptor.class
com/ibm/db2j/core/BootStrap.class

How do I figure out what to pass to import or java_import or include?

Thank you for your time,
Perry

Are these java classes view by the jre thats runs Jruby? Sounds like a
classpath problemā€¦
El 17/05/2013 03:50, ā€œPerry S.ā€ [email protected] escribi:

Hi Perry, you need to load the driver class (right) before connecting
using DriverManager.

Assuming the class name is right do smt like: Java::JavaClass.for_name
ā€˜com.ibm.db2j.jdbc.DB2jDriverā€™
Make sure you do this after the .jar files are required, yet before
connecting ā€¦

Hope it helps, K.

please note that jar | grep only works if the driver author named
their
java.sql.Driver implementation with ā€œDriverā€ in the class name.

since you mentioned 0% Java knowledge you can not expect to 100%
understand
this :slight_smile: - itā€™s a Java (actually JDBC) thing ā€¦

scanning (and thus loading) all .jar classes for driver implementations
(which might have arbitrary names as mentioned) would certainly be a
performance hit - thereā€™s a Java auto-discovery convention
(META-INF/java.sql.Driver text file inside the .jar) that gets around
this
itā€™s likely that your driver does not conform with it thus the manual
loading.

enjoy Java with JRuby - itā€™s certainly more enjoyable this way !

Weee!!!

Thank you so much. I now get:

WARNING: Cloudscape (instance c013800d-013e-b298-9d99-00c0a801c300) is
attempting to boot the database
/Users/pedzan/Source/OPCstuff/ruby-stuff/db/OpcClientDb even though
Cloudscape (instance c013800d-013d-b6e5-23bc-0009318f4f00) may still be
active. Only one instance of Cloudscape should boot a database at a
time. Severe and non-recoverable corruption can result and may have
already occurred.

But I am pretty sure that is because they create file locks and I have a
snapshot of the DB while its open.

Now, I just need to figure out how to dump what I need but thats
probably a topic for another list.

By the way (for the next guy who might follow). I did:

jar tf lib/db2j.jar | grep Driver

to get the exact name (which Karol had predicted correctly) ā€“ and then
changed the slashes into dots.

The thing I donā€™t 100% understand is why not just do this for all the
classes when the jar file is loaded? Would that be a huge performance
hit or something?

Thank you very much!
Perry

The following does not directly help and its unsolicited but if you use
Ruby Sequel which I am currently using via jdbc drivers to hook up to
Oracle and Postgres all of your load issues should go away. Furthermore,
the ruby sequel dsl is nice to work with but you can also just pass it
raw sql or a combination of. Disclaimer I have not tried this out with
Cloudscape but if thereā€™s a jdbc driver for it , my understanding is
that it should work. May need to drop that jar under the jruby libs
directory.

Check it out, you might be happier.

Charles

Thanks. I will definitely check it out.

On top of Karolā€™s mentioning of performance reasonsā€¦There is no nice
API
for reflectively querying all classes loaded. Jar loading possibly
could
be done via examining classpath (although classpath can change at
runtime),
but JVM can also dynamically generate classes on the fly (or even get
loaded by JNI). The ability to just auto-process all defined things is
difficult even if you ignore the performance penalty of processing
thousands of classes you probably will never consume.

-Tom