Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Connecting to Oracle in Eclipse 3.4.2

Posted on 2009-05-08
8
Medium Priority
?
2,597 Views
Last Modified: 2013-12-19
I am new to developing with Eclipse and I need to write a lot of code associated with Oracle databases.  I am using Eclipse 3.4.2 and trying to write Java that will connect to an Oracle 9 database.  I am trying to configure my Eclipse environment so that I can take advantage of the oci connect but I keep getting this error:

java.lang.UnsatisfiedLinkError: no ocijdbc9 in java.library.path
      at java.lang.ClassLoader.loadLibrary(ClassLoader.java:1517)
      at java.lang.Runtime.loadLibrary0(Runtime.java:788)
      at java.lang.System.loadLibrary(System.java:834)
      at oracle.jdbc.oci8.OCIDBAccess.logon(OCIDBAccess.java:267)
      at oracle.jdbc.driver.OracleConnection.<init>(OracleConnection.java:371)
      at oracle.jdbc.driver.OracleDriver.getConnectionInstance(OracleDriver.java:551)
      at oracle.jdbc.driver.OracleDriver.connect(OracleDriver.java:351)
      at oracle.jdbc.pool.OracleDataSource.getConnection(OracleDataSource.java:169)
      at oracle.jdbc.pool.OracleDataSource.getConnection(OracleDataSource.java:105)
      at dbAccess.main(dbAccess.java:34)
Exception in thread "main"

I suspect this is a problem because Eclipse can't find the DLL for the oci driver.  Plus I can compile this code manually from a terminal and it works fine.  I need to know how to get Eclipse to use the DLL for the OCI driver (if it's even possible).  The sample of code below generates the error above.  It is just a small example I am using to test database connectivity.
import java.sql.*;
import java.io.*;
import oracle.jdbc.pool.OracleDataSource;
import oracle.sql.*;
import oracle.jdbc.driver.*;
	
public class dbAccess
{
	public static void main (String args []) throws SQLException, IOException
	{
		String user;
		String password;
		String database;
 
		user = readEntry ("user: ");
		int slash_index = user.indexOf ('/');
		if (slash_index != -1)
		{
			password = user.substring (slash_index + 1);
			user = user.substring (0, slash_index);
		}
		else
			password = readEntry ("password: ");
		database = readEntry ("database (a TNSNAME entry): ");
 
		OracleDataSource ods = new OracleDataSource();
		ods.setURL("jdbc:oracle:oci8:" + user + "/" + password + "@" + database);
 
		System.out.println ("Connecting to the database...");
		System.out.flush ();
 
		try
		{
			Connection conn = ods.getConnection();
			conn.setAutoCommit (false);
			System.out.println ("");
			DatabaseMetaData dbmd = conn.getMetaData();
            		System.out.println("=====  Database info =====");
            		System.out.println("DatabaseProductName: " + dbmd.getDatabaseProductName());
            		System.out.println("DatabaseProductVersion: " + dbmd.getDatabaseProductVersion());
            		System.out.println("DatabaseMajorVersion: " + dbmd.getDatabaseMajorVersion());
            		System.out.println("DatabaseMinorVersion: " + dbmd.getDatabaseMinorVersion());
            		System.out.println("=====  Driver info =====");
            		System.out.println("DriverName: " + dbmd.getDriverName());
            		System.out.println("DriverVersion: " + dbmd.getDriverVersion());   
            		System.out.println("DriverMajorVersion: " + dbmd.getDriverMajorVersion());   
            		System.out.println("DriverMinorVersion: " + dbmd.getDriverMinorVersion());   
            		System.out.println("=====  JDBC/DB attributes =====");   
            		System.out.print("Supports getGeneratedKeys(): ");   
            		if (dbmd.supportsGetGeneratedKeys())   
              			System.out.println("true");   
            		else  
              			System.out.println("false");
			Statement stmt = conn.createStatement ();
			ResultSet rset = stmt.executeQuery ("select sysdate from dual");
			while (rset.next ())
				System.out.println (rset.getString (1));
 
			rset.close();
			stmt.close();
			conn.close();
		}		
		catch (SQLException e)
		{
			System.out.println ("Unable to connect to database " + database + ".");
			switch (e.getErrorCode())
			{
				case 12154:
					System.out.println ("Unable to resolve TNSNAME entry");
					break;
				case 1017:
					System.out.println ("Invalid user / password");
					break;
				default:
					System.out.println ("Unknown error");
					System.out.println (e.getErrorCode());
					System.out.println (e.getMessage());
					break;
			}
			System.exit(0);
		}
	}
 
	static String readEntry (String prompt)
	{
		try
		{
			StringBuffer buffer = new StringBuffer ();
			System.out.print (prompt);
			System.out.flush ();
			int c = System.in.read ();
			while (c != '\n' && c != -1)
			{
				buffer.append ((char)c);
				c = System.in.read ();
			}
			return buffer.toString ().trim ();
		}
		catch (IOException e)
		{
			return "";
		}
	}
}

Open in new window

0
Comment
Question by:ccovell
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 2
8 Comments
 

Assisted Solution

by:vinnakota
vinnakota earned 600 total points
ID: 24340494
Add <<oraclehomedirectory>>/bin to the containers Libary path and also make sure the oracle jdbc driver jar is in classpath.
0
 

Author Comment

by:ccovell
ID: 24340558
Thank you vinnakota,
I apologize for my n00b-ness.  I understand what you are saying, but I do not know how to add the ORACLE_HOME/bin directory to the container Library path.  Respectfully, can you provide more detailed steps.  I know the resource locations, I just need to know how to do that in Eclipse 3.4.2.  Thanks.
0
 

Author Comment

by:ccovell
ID: 24340663
After a little searching in the IDE I've added the ORACLE_HOME/bin path to the native library for the src folder and for the JRE system library and an auxillary library I am using that has the jdbc driver jar.  I'm still getting the same error.
0
Build and deliver software with DevOps

A digital transformation requires faster time to market, shorter software development lifecycles, and the ability to adapt rapidly to changing customer demands. DevOps provides the solution.

 

Author Comment

by:ccovell
ID: 24340888
Even if I use:

System.load("c:\\Program Files\\Oracle\\Ora102\\BIN\\ocijdbc10.dll");

I still get the same error.  Somehow, I'm beginning to think this is not an issue with Eclipse.
0
 

Assisted Solution

by:vinnakota
vinnakota earned 600 total points
ID: 24341005
Assuming that you are getting the error at runtime, try adding the path in the Run configuration (Run --> Run Configurations --> Classpath tab)
0
 

Accepted Solution

by:
RMarla earned 1400 total points
ID: 24341284
In Eclipse, right click your project....and choose "properties"
Click on "Java Build Path"
Choose the libraries tab
Click on "Add External Jar..."
(Now, I assume you have an Oracle Client installed on your computer....and the necessary client libraries.     I do not remember the exact path, but search for classes12.zip in the Oracle client installation path)
Navigate and choose the file:  classes12.zip from the directory
    {Oracle_Install_directory}\jdbc\lib

Then click on RUN menu in Eclipse
     Open Run options...
      Click on the java application being tested
      Click on the "Classpath" tab
     Click on "Add External Jars...."
     Add the classes12.zip just as you did earlier.

Then, go ahead and run your program.     I tested your code and it worked for me....although I used a different jar file since I used the 10g client/database

Hope this helps..

0
 

Author Comment

by:ccovell
ID: 24342142
with all due respect, it's the weekend here and I got too frustrated to keep trying.  Your solution is something that i have not tried and will do so on Monday.  Update then (since the site is secured by guys with guns till then).  Thanks to all.  Update to follow...
0
 

Author Comment

by:ccovell
ID: 24355298
Outstanding.  Worked perfectly.  Thank you.
0

Featured Post

Veeam Task Manager for Hyper-V

Task Manager for Hyper-V provides critical information that allows you to monitor Hyper-V performance by displaying real-time views of CPU and memory at the individual VM-level, so you can quickly identify which VMs are using host resources.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This post first appeared at Oracleinaction  (http://oracleinaction.com/undo-and-redo-in-oracle/)by Anju Garg (Myself). I  will demonstrate that undo for DML’s is stored both in undo tablespace and online redo logs. Then, we will analyze the reaso…
Shell script to create broker configuration file using current broker Configuration, solely for purpose of backup on Linux. Script may need to be modified depending on OS-installation. Please deploy and verify the script in a test environment.
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…
This video shows syntax for various backup options while discussing how the different basic backup types work.  It explains how to take full backups, incremental level 0 backups, incremental level 1 backups in both differential and cumulative mode a…

730 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question