1

我正在尝试通过以下程序连接到 Oracle 数据库。但抛出错误 java.lang.ClassNotFoundException: oracle.jdbc.driver.OracleDriver "

我的机器上只安装了 SQL 开发者客户端,而实际数据库存储在服务器上。请帮助解决问题

package test;

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

public class DBConnection {

    public static void main(String[] argv) {

        System.out.println("-------- Oracle JDBC Connection Testing ------");

        try {

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

        } catch (ClassNotFoundException e) {

            System.out.println("Where is your Oracle JDBC Driver?");
            e.printStackTrace();
            return;

        }

        System.out.println("Oracle JDBC Driver Registered!");

        Connection connection = null;


    enter code here
        try {

            connection = DriverManager.getConnection(
                    "jdbc:oracle:thin:@hostname:5800:SID", "user","password");

        } catch (SQLException e) {

            System.out.println("Connection Failed! Check output console");
            e.printStackTrace();
            return;

        }

        if (connection != null) {
            System.out.println("You made it, take control your database now!");
        } else {
            System.out.println("Failed to make connection!");
        }
    }

}
4

1 回答 1

1

将您的 Oracle JDBC 驱动程序 jar 添加到类路径

可以从这里下载适用于您的 Oracle 数据库版本的驱动程序

于 2013-06-11T09:08:58.777 回答