tags:

views:

104

answers:

3

Hi friends i am facing the following error while trying to connect to a database through java code:

Exception in thread "Main Thread" java.lang.ClassNotFoundException: oracle.jdbc.OracleDriver
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:169)
at com.example.model.Driver.main(Driver.java:13)

My java code is:

package com.example.model;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class Driver {
    public static void main(String args[]) throws ClassNotFoundException,
    SQLException {

        Class.forName("oracle.jdbc.OracleDriver");
        // or you can use:
        // DriverManager.registerDriver(
        // new oracle.jdbc.driver.OracleDriver());
        Connection conn = DriverManager.getConnection(
        "jdbc:oracle:thin:@127.0.0.1:1521:ORCL", "scott", "tiger");

        Statement stmt = conn.createStatement();
        ResultSet rset = stmt.executeQuery("select * from customer");
        while (rset.next())
            System.out.println(rset.getString(1));
        rset.close();
        stmt.close();
        conn.close();
    }
}

As per i came to know it could be due to class path issue but don't know how to resolve it. I set my class path to

C:\bea\user_project\workspace\wlserver_10.3\server\ext\jdbc\oracle\11g\ojdbc5.jar;

I am using weblogic 10.3 workspace and weblogic 10.3 server.

+1  A: 

I will try to put it in the weblogic's lib folder... and restart the server, so you will be sure that it is a classpath problem...

You should download the oracle drivers from oracle and put the .jar file in the project's CLASSPATH...

You can download it at Oracle's drivers download page

Garis Suero
It seems the OP is not running inside WebLogic (and actually, WebLogic already has an oracle driver in `$WL_HOME/server/lib`)
Pascal Thivent
+2  A: 

The java.lang.ClassNotFoundException: oracle.jdbc.OracleDriver definitely means that the mentioned class is not on the class path. And because ojdbc5.jar has it, the whole question is: "how did you set your classpath"?

Here is what I get on my machine using your code (using the default package):

$ cat > Driver.java
...
$ javac Driver.java
$ java -cp /home/pascal/opt/Oracle/Middleware/wlserver_10.3/server/ext/jdbc/oracle/11g/ojdbc5.jar:. Driver 
Exception in thread "main" java.sql.SQLException: The Network Adapter could not establish the connection
    at oracle.jdbc.driver.SQLStateMapping.newSQLException(SQLStateMapping.java:70)
    at oracle.jdbc.driver.DatabaseError.newSQLException(DatabaseError.java:131)
    at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:197)
    at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:525)
    at oracle.jdbc.driver.T4CConnection.logon(T4CConnection.java:413)
    at oracle.jdbc.driver.PhysicalConnection.(PhysicalConnection.java:508)
    at oracle.jdbc.driver.T4CConnection.(T4CConnection.java:203)
    at oracle.jdbc.driver.T4CDriverExtension.getConnection(T4CDriverExtension.java:33)
    at oracle.jdbc.driver.OracleDriver.connect(OracleDriver.java:510)
    at java.sql.DriverManager.getConnection(DriverManager.java:582)
    at java.sql.DriverManager.getConnection(DriverManager.java:185)
    at Driver.main(Driver.java:15)
Caused by: oracle.net.ns.NetException: The Network Adapter could not establish the connection
    at oracle.net.nt.ConnStrategy.execute(ConnStrategy.java:328)
    at oracle.net.resolver.AddrResolution.resolveAndExecute(AddrResolution.java:421)
    at oracle.net.ns.NSProtocol.establishConnection(NSProtocol.java:634)
    at oracle.net.ns.NSProtocol.connect(NSProtocol.java:208)
    at oracle.jdbc.driver.T4CConnection.connect(T4CConnection.java:966)
    at oracle.jdbc.driver.T4CConnection.logon(T4CConnection.java:292)
    ... 7 more
Caused by: java.net.ConnectException: Connection refused
    at java.net.PlainSocketImpl.socketConnect(Native Method)
    at java.net.PlainSocketImpl.doConnect(PlainSocketImpl.java:333)
    at java.net.PlainSocketImpl.connectToAddress(PlainSocketImpl.java:195)
    at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:182)
    at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:366)
    at java.net.Socket.connect(Socket.java:529)
    at java.net.Socket.connect(Socket.java:478)
    at java.net.Socket.(Socket.java:375)
    at java.net.Socket.(Socket.java:189)
    at oracle.net.nt.TcpNTAdapter.connect(TcpNTAdapter.java:127)
    at oracle.net.nt.ConnOption.connect(ConnOption.java:126)
    at oracle.net.nt.ConnStrategy.execute(ConnStrategy.java:306)
    ... 12 more

The driver is found (I get an exception because I'm not running any Oracle server but this is another story).

Pascal Thivent
A: 

You are missing driver in the name..

Class.forName("oracle.jdbc.driver.OracleDriver").newInstance()

(running java -verbose:class is generally useful debugging in classpath problem)

Jayan
He is definitely **NOT** (the `ojdbc.jar` is providing both packages) and the package `oracle.jdbc.driver` is actually deprecated since v9.0.1, users should use `oracle.jdbc`. This is a pure incorrect classpath problem.
Pascal Thivent