*
The moose likes JDBC and the fly likes Connecting to an Oracle DB Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » JDBC
Bookmark "Connecting to an Oracle DB" Watch "Connecting to an Oracle DB" New topic
Author

Connecting to an Oracle DB

James Hambrick
Ranch Hand

Joined: Sep 04, 2004
Posts: 282

I have tried this in Netbeans and was able to connect and write data to a table, but not query(kept getting an error on the SQL statement). Anyways I am working on this from scratch not using NetBeand and would like to know how I connect to a DB and get data from it. NetBeans seems to use an entity manager, but I think there's another way too. Anyone have any useful links. I was reading through Head First Java but I did not see anywhere they talk about connecting to Oracle.


Visit my blog! http://jameshambrick.com
David Barry
Ranch Hand

Joined: Jan 13, 2009
Posts: 83
Is this sort of what you are talking about?

http://w2.syronex.com/jmr/edu/db/oracle-and-java
James Hambrick
Ranch Hand

Joined: Sep 04, 2004
Posts: 282

C:\Java>java OraThin > oraThin.log
java.lang.ClassNotFoundException: oracle.jdbc.driver.OracleDriver
at java.net.URLClassLoader$1.run(URLClassLoader.java:200)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:188)
at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:252)
at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:320)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:169)
at OraThin.main(OraThin.java:8)

C:\Java>


This is what I get when I copy the code from that example, edit the details to connect to my DB , compile and run. Seems to not be able to load the drivers. I'm using Oracle 10G and it's loaded directly onto my computer so the drivers should be there I would assume.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37991
    
  22
We usually discuss this sort of thing in the JDBC forum . . . moving.
James Hambrick
Ranch Hand

Joined: Sep 04, 2004
Posts: 282

I have found the path to be correct. oracle.jdbc.driver.OracleDriver
when I run java c:\oracle\jdbc\driver\OracleDriver I get errors. "Exception in thread "main" ".

It may be that I have Java 1.6 and the latest Oracle driver I saw was for Java 1.4-1.5. Should I uninstall and reinstall 1.5?
Tualha Khan
Ranch Hand

Joined: Nov 22, 2000
Posts: 287
I am guessing you are using JDBC driver to connect to the oracle database here. So, the driver resides in a jar file which needs to be in your CLASSPATH. so, look for something like classes.jar or ojdbc.jar etc in your oracle home's JDBC directory.

The way to configure your classpath in windows is:

CMD> set CLASSPATH=%CLASSPATH%;<path_to_oracle_jar_file.jar>

Also, the actual jar file needs to be in the classpath (absolute path with the extension included), not just the directory path.

hth,
tualha


SCJP2, BEA WLS 6.0, DB2 UDB 7.1
James Hambrick
Ranch Hand

Joined: Sep 04, 2004
Posts: 282

I don't have a .jar file, just .class files. I downloaded a .jar file and used the jar command to install/extract it. Althougth I'm not sure where it went to. But it was for a java version 1.5. I found a ojdbc14.jar inside c:\oracle\product\10.2.0\db_1\jdbc\lib I was going to place that path into the program, but not sure how the 10.2.0 would work since it uses dots to separate.

Okay I've redownloaded the .jar file and placed it into the c:\oracle\jdbc\driver folder. Added it to CLASSPATH. In DOS when to do java ojdbc14.jar I get errors. I read that it should give the version of the driver. seems like Java and the driver are not liking each other.
Tualha Khan
Ranch Hand

Joined: Nov 22, 2000
Posts: 287
I found a ojdbc14.jar inside c:\oracle\product\10.2.0\db_1\jdbc\lib I was going to place that path into the program, but not sure how the 10.2.0 would work since it uses dots to separate.

This should be OK, but to be safe, just enclose the whole path in double quotes eg.



What version of java are you using?



hth,
tualha
James Hambrick
Ranch Hand

Joined: Sep 04, 2004
Posts: 282

java version 1.6.0_10

I have moved the jar file to c:\oracle\jdbc and changed the classPath

My concern is that I cannot even run the jar file. Shouldn't I just have to run java <name of jar file> and get something like a version number as the output.
Balu Sadhasivam
Ranch Hand

Joined: Jan 01, 2009
Posts: 874


My concern is that I cannot even run the jar file. Shouldn't I just have to run java <name of jar file> and get something like a version number as the output.


I am not sure what you try to do with runnning the ojdbc14.jar file ?

You can directly point jar file in the CLASSPATH , and no need to extract it.
If say your jar is in c:\oracle\jdbc path , then
set CLASSPATH=%CLASSPATH%;"c:\oracle\jdbc\ojdbc14.jar"; shd do and can be called from your java application.

BTW , oracle.jdbc.driver.OracleDriver Class is deprecated and use instead oracle.jdbc.OracleDriver.

Four your additional information , the oracle.jdbc.driver.OracleDriver cannnot be even accessed in Oracle 11g with ojdbc5.jar as the Class is changed to default access.

James Hambrick
Ranch Hand

Joined: Sep 04, 2004
Posts: 282



Here's the errors I get when I try to run it.

C:\Java>java OraThin
Exception in thread "main" java.lang.NoClassDefFoundError: OraThin
Caused by: java.lang.ClassNotFoundException: OraThin
at java.net.URLClassLoader$1.run(URLClassLoader.java:200)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:188)
at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:252)
at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:320)
Could not find the main class: OraThin. Program will exit.

C:\Java>


And yes the file is named OraThin.java and yes I compiled it and there's a OraThin.class file.
Balu Sadhasivam
Ranch Hand

Joined: Jan 01, 2009
Posts: 874


C:\Java>java OraThin
Exception in thread "main" java.lang.NoClassDefFoundError: OraThin
Caused by: java.lang.ClassNotFoundException: OraThin


Is your class file OraThin is inside C:/Java ? try to run from the same directory as you have the class file

Also add the "." at the end of the CLASSPATH


Edited : for proper understanding
James Hambrick
Ranch Hand

Joined: Sep 04, 2004
Posts: 282

The class file is located in c:\java

I added the semi-colon at the end of the classpath and now got a different error.


C:\Java>java OraThin
java.sql.SQLException: Io exception: The Network Adapter could not establish the
connection
at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java
:112)
at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java
:146)
at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java
:255)
at oracle.jdbc.driver.T4CConnection.logon(T4CConnection.java:387)
at oracle.jdbc.driver.PhysicalConnection.<init>(PhysicalConnection.java:
441)
at oracle.jdbc.driver.T4CConnection.<init>(T4CConnection.java:165)
at oracle.jdbc.driver.T4CDriverExtension.getConnection(T4CDriverExtensio
n.java:35)
at oracle.jdbc.driver.OracleDriver.connect(OracleDriver.java:801)
at java.sql.DriverManager.getConnection(DriverManager.java:582)
at java.sql.DriverManager.getConnection(DriverManager.java:185)
at OraThin.main(OraThin.java:9)

C:\Java>



The Oracle DB is located on my computer but I put in the IP address. can I just put localhost? Looks like it's not connecting now.
Balu Sadhasivam
Ranch Hand

Joined: Jan 01, 2009
Posts: 874



Is yours static IP ? Yes use localhost instead , also make sure you "test" SID available and Oracle server is running.
James Hambrick
Ranch Hand

Joined: Sep 04, 2004
Posts: 282

Okay I changed it to @localhost and it ran with no errors. now I can get it to write data and query data I will be golden.
James Hambrick
Ranch Hand

Joined: Sep 04, 2004
Posts: 282

Okay I can write to the table, and query that table. But how do I query using a where statment and a variable?

I can have
("select * from Login where uname = 'james3302'")

But of course I need to be able to use a variable there.
Balu Sadhasivam
Ranch Hand

Joined: Jan 01, 2009
Posts: 874


Are you using Statement or PreparedStatement ?

If Statement then
userName = "james3302"
"select * from Login where name ='"+ userName + "'"

If Prepared Statement then
userName = "james3302"
query = "select * from Login where uname = ? " ;
ps = con.prepareStatement(query);
ps.setString(1,userName);


Anyway regardless of it, If you dont understand any of the above , Please learn the basics of JDBC and come back with queries.

James Hambrick
Ranch Hand

Joined: Sep 04, 2004
Posts: 282

Thanks for the link, but I have already read through that. I just cannot seem to get the syntax right.

I copied and pasted your example code and it works. I must have typed your code wrong.
 
wood burning stoves
 
subject: Connecting to an Oracle DB
 
Similar Threads
Connecting to Oracle data source from EJB
netbeans + oracle
Struts: DB Connection
java DB2 program
Hibernate Configuration help!