2011-12-14 69 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。