连接到远程的oracle数据库

发布于 2024-12-21 09:47:14 字数 1580 浏览 2 评论 0原文

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

   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(Empyoee.java:23) Java 结果:1 构建成功(总时间:2秒)

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

I am trying to run following code which is connecting to remote database and retrieving records:

   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"));

}
}

after running this code from Netbeans i am getting an error:

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)

I have downloaded ojdbc14.jar and kept in C:\Program Files\Java\jdk1.7.0\jre\lib path.
I do not know where i am going wrong?...plz help me here.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

扛起拖把扫天下 2024-12-28 09:47:14

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

You are using awfully old version of Oracle JDBC driver. You should be using ojdbc6.jar.

老旧海报 2024-12-28 09:47:14

尝试使用此驱动程序:

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

检查 Netbeans 中的类路径:

如何在 NetBeans 中设置类路径:

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

  1. 编译:清空默认。编译时库会自动生成
    传播到类路径的其他类别,因此您不需要
    在所有 4 个类别中重复同一组 jar 文件。
  2. 运行:默认情况下包含编译时类路径中的所有内容,并且
    已编译的类(例如,构建/类)。
  3. 编译测试:默认情况下包括编译时的所有内容
    类路径、编译类(例如,构建/类)和 JUnit。
  4. 运行测试:默认情况下包括用于编译测试的类路径,以及
    编译的测试。

Try with this driver:

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

Check your classpath in Netbeans:

How to set classpath in NetBeans:

In NetBeans Project Properties Window, you click Libraries in the left panel, and in the right panel are 4 categories of classpath you can configure:

  1. Compile: Empty by default. Compile-time libraries are automatically
    propagated to other categories of classpath, so you don't need to
    repeat the same set of jar files in all 4 categories.
  2. Run: By default includes everything in compile-time classpath, and
    compiled classes (e.g., build/classes).
  3. Compile Tests: By default includes everything in compile-time
    classpath, compiled classes (e.g., build/classes), and JUnit.
  4. Run Tests: By default includes classpath for compiling tests, and
    compiled tests.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文