我正在尝试使用Oracle瘦驱动程序连接到Oracle 10 XE数据库,但无法实现这一点。甚至在编译过程中将驱动程序添加到了我的类路径中。

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Properties;

public class Connectivity {
    public static void main(String args[]) throws SQLException {
        String url = "jdbc:oracle:thin:@localhost:1521:XE";
        Properties props = new Properties();

        props.setProperty("user", "SYSTEM");
        props.setProperty("password", "iiita");

        try {
            Class.forName ("oracle.jdbc.driver.OracleDriver");
        } catch (ClassNotFoundException e) {
            System.out.println(e.getMessage());
        }
        Connection conn = DriverManager.getConnection(url,props);
        String sql ="select sysdate as current_day from dual";

        PreparedStatement preStatement = conn.prepareStatement(sql);

        ResultSet result = preStatement.executeQuery();

        while(result.next()){
            System.out.println("Current Date from Oracle : " +  result.getString("current_day"));
        }

        System.out.println("done");

    }
}

该代码在运行期间给出以下错误
Exception in thread "main" java.sql.SQLException: No suitable driver found for jdbc:oracle:thin:@localhost:1521/XE
    at java.sql.DriverManager.getConnection(DriverManager.java:644)
    at java.sql.DriverManager.getConnection(DriverManager.java:202)
    at Connectivity.main(Connectivity.java:16)

最佳答案

您在运行主类时忘记指定ojdbc14类路径:

对于Windows:java -cp .;<path>/ojdbc14.jar Connectivity
在Linux上:java -cp .:<path>/ojdbc14.jar Connectivity
附言在类路径上不需要ojdbc14.jar即可编译主类。这是运行时依赖项。

关于java - 无法使用jdbc瘦驱动程序连接到oracle数据库,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/14941936/

10-10 14:30