wood burning stoves 2.0*
The moose likes JDBC and the fly likes Java program to Connect to database Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Databases » JDBC
Bookmark "Java program to Connect to database" Watch "Java program to Connect to database" New topic

Java program to Connect to database

Kerin Kapoor

Joined: Jan 21, 2014
Posts: 14

I'm trying to write a simple java program to connect to Oracle database using Jrockit as my jvm.. Jrockit is being used by another 3rd party app (weblogic). I'm just using it to run my simple java program.

I'm new to java so following the steps provided here http://www.javacamp.org/moreclasses/jdbc/jdbc.html

The program gets compiled but I get the below error while running the program -

java.lang.classNotFoundException: 3Doracle.jdbc.driver.OracleDriver
JDBC Class found
SQL exception occured java.sql.SQLException: No suitable driver found for jdbc: oracle thin:@hostname:port:dbname

Please let me know the steps that need to be followed to make this connection work.

If I go to wlserver_10.3/server/ext/jdbc/oracle/11g..I can see the ojdbc5_g.jar,ojdbc5.jar,ojdbc6dms.jar, ojdbc6_g.jar, orai18n.jar, orai18n-mapping.jar files - which means the oracle driver is present.. but how do I use it ???

Thanks in Advance!
Jeanne Boyarsky
internet detective

Joined: May 26, 2003
Posts: 30130

That error typically means that Oracle driver isn't in your classpath. Try running the program as

On Windows:
java -cp "/path/to/Oracle/driver/oracleDriver.jar;." Uname

On UNIX/Linux/Mac:
java -cp "/path/to/Oracle/driver/oracleDriver.jar:." Uname

[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
Kerin Kapoor

Joined: Jan 21, 2014
Posts: 14

Thanks for the reply..

I've tried the below -
1. Compile - ./javac /app/utils/Uname.java

2. Run - ./java -cp "/app/wlserver_10.3/server/lib/ojdbc6.jar:." /app/utils/ Uname

But step 2 throws an error. I've tried the step2 without double quotes but no luck. The error is given below -

Exception in thread "Main Thread" java.lang.NoClassDefFoundError: /app/utils
Caused by : java.lang.ClassNotFoundException: .ap01.cap.hyperion.utils
at java.net. URLClassLoader$1.run(URLClassLoader.java.202)
at java.net. URLClassLoader.findClass(URLClassLoader.java.190)
at java.lang.ClassLoader.Loadclass(ClassLoader.java:306)
at sun.misc.Launcher$AppClassLoader.LoadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
Could not find the main class: /app/utils/. Program will exit

Any pointers will be appreciated. I'm using Jrockit jvm installed with weblogic server...

Thanks in advance!!
Jeanne Boyarsky
internet detective

Joined: May 26, 2003
Posts: 30130

You have two choices:

Option 1 - run from directory with code
cd into /app/utils
Run - ./java -cp "/app/wlserver_10.3/server/lib/ojdbc6.jar:." Uname

Option 2 - specify the directory the code is in
Run - ./java -cp "/app/wlserver_10.3/server/lib/ojdbc6.jar:/app/utils" Uname
I agree. Here's the link: http://aspose.com/file-tools
subject: Java program to Connect to database
Similar Threads
Not Suitable Driver Error
Need a help wilh MSAccess
problem in connecting to oracle through jdbc
Why this Program throws SQLException