得到类未找到异常

时间:2010-08-07 16:08:29

标签: java oracle jdbc

朋友们,我在尝试通过java代码连接到数据库时遇到以下错误:

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)

我的java代码是:

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();
    }
}

据我所知,这可能是由于类路径问题,但不知道如何解决它。 我将课程路径设置为

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

我使用的是weblogic 10.3工作区和weblogic 10.3服务器。

2 个答案:

答案 0 :(得分:2)

java.lang.ClassNotFoundException: oracle.jdbc.OracleDriver肯定意味着提到的类不在类路径上。因为ojdbc5.jar拥有它,所以整个问题是:“你是如何设置类路径的?”

以下是我使用您的代码(使用默认包)在我的机器上获得的内容:

$ 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

找到驱动程序(我得到一个例外,因为我没有运行任何Oracle服务器,但这是另一个故事)。

答案 1 :(得分:1)

我会尝试将它放在weblogic的lib文件夹中......并重新启动服务器,这样你就可以确定它是一个类路径问题......

你应该从oracle下载oracle驱动程序并将.jar文件放在项目的CLASSPATH中......

您可以在Oracle's drivers download page

下载