2011-12-14 70 views
2

我試圖運行下面的代碼被連接到遠程數據庫和檢索記錄Oracle數據庫:連接到位於遠程

import java.sql.*; 

    class Employee 
    { 
    public static void main (String args []) 
     throws SQLException, ClassNotFoundException { 
    // Load the Oracle JDBC driver 
    Class.forName ("sun.jdbc.odbc.JdbcOdbcDriver"); 

    // Connect to the database 
    // You must put a database name after the @ sign in the connection URL. 
    // You can use either the fully specified SQL*net syntax or a short cut 
    // syntax as <host>:<port>:<sid>. The example uses the short cut syntax. 
    Connection conn = DriverManager.getConnection ("jdbc:oracle:thin:@ourcompany.com:1521:course", "username", "password"); 

// Create a Statement 
Statement stmt = conn.createStatement(); 

// Select the ENAME column from the EMP table 
ResultSet rset = stmt.executeQuery ("select * from test"); 

// Iterate through the result and print the employee names 
/* 
while (rset.next()) 
    System.out.println (rset.getString ("name")); 
    System.out.println (rset.getString ("id")); 
    */ 
    rset.next(); 
    System.out.println(rset.getString("name")); 

} }

從NetBeans中運行此代碼我得到一個後錯誤:

Exception in thread "main" java.sql.SQLException: No suitable driver found for jdbc:oracle:thin:@ourcompany.com:1521:course at java.sql.DriverManager.getConnection(DriverManager.java:604) at java.sql.DriverManager.getConnection(DriverManager.java:221) at Employee.main(Emplyoee.java:23) Java Result: 1 BUILD SUCCESSFUL (total time: 2 seconds)

我已經下載ojdbc14.jar的,並保存在C:\ Program Files文件\的Java \ jdk1.7.0 \ JRE \ lib文件路徑。 我不知道我哪裏去錯了... ... plz幫助我在這裏。

回答

1

嘗試用該驅動程序:

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

檢查您的類路徑中的Netbeans:

如何設置CLASSPATH在NetBeans:

在NetBeans項目屬性窗口,單擊庫在左側面板,在右側面板中可以配置4種類路徑:

  1. 編譯:默認爲空。編譯時庫自動傳播到其他類路徑,因此您不需要 在所有4個類別中重複使用同一組jar文件。
  2. 運行:默認情況下包括編譯時類路徑中的所有內容,以及編譯類(例如,構建/類)。
  3. 編譯測試:默認包含編譯時的所有內容 classpath,編譯類(例如build/classes)和JUnit。
  4. 運行測試:默認情況下包括用於編譯測試的類路徑,以及 已編譯的測試。
+0

我試過這個...得到了同樣的錯誤。 – kpc72 2011-12-14 22:44:13

+0

你確定jar文件在你的類路徑中嗎? – chance 2011-12-14 22:45:34

1

您正在使用舊版本的Oracle JDBC驅動程序。你應該使用ojdbc6.jar。