【问题标题】:Connecting to oracle database located remotely连接到远程的 oracle 数据库
【发布时间】:2012-01-20 16:39:36
【问题描述】:

我正在尝试运行以下连接远程数据库并检索记录的代码:

   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 运行此代码后,我收到一个错误:

线程“main”java.sql.SQLException 中的异常:找不到适合 jdbc:oracle:thin:@ourcompany.com:1521:course 的驱动程序 在 java.sql.DriverManager.getConnection(DriverManager.java:604) 在 java.sql.DriverManager.getConnection(DriverManager.java:221) 在 Employee.main(Emplyoee.java:23) Java 结果:1 构建成功(总时间:2 秒)

我已经下载了 ojdbc14.jar 并保存在 C:\Program Files\Java\jdk1.7.0\jre\lib 路径中。 我不知道我哪里出错了?...请在这里帮助我。

【问题讨论】:

    标签: oracle netbeans jdbc database-connection


    【解决方案1】:

    试试这个驱动:

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

    在 Netbeans 中检查您的类路径:

    如何在 NetBeans 中设置类路径:

    在 NetBeans 项目属性窗口中,单击左侧面板中的库,右侧面板中有 4 类可以配置的类路径:

    1. 编译:默认为空。编译时库是自动的 传播到其他类别的类路径,所以你不需要 在所有 4 个类别中重复相同的 jar 文件集。
    2. 运行:默认情况下包括编译时类路径中的所有内容,并且 编译的类(例如,build/classes)。
    3. 编译测试:默认情况下包括编译时的所有内容 类路径、编译的类(例如 build/classes)和 JUnit。
    4. 运行测试:默认包含用于编译测试的类路径,以及 编译测试。

    【讨论】:

    • 你确定 jar 文件在你的类路径中吗?
    • 我是否必须使用命令行设置 CLASSPATH:C:> java -classpath C:\java\MyClasses\myclasses.jar
    • 那么我必须在哪里添加 ojdbc6.jar 文件?运行测试选项?
    • 我在编译窗格中添加了 ojdbc6.jar 文件并运行代码...它已连接,我得到的输出如下:运行:尝试连接...已连接! SQLException:Io 异常:大小数据单元 (SDU) 不匹配
    【解决方案2】:

    您使用的是非常旧版本的 Oracle JDBC 驱动程序。您应该使用 ojdbc6.jar。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-12-14
      • 2012-07-16
      • 2014-06-03
      • 1970-01-01
      • 2012-01-24
      • 1970-01-01
      相关资源
      最近更新 更多