什么 jdbc jar 与 oracle 11g & 一起使用jdk 1.6 以及如何连接到数据库本身

2021-12-30 00:00:00 oracle11g oracle jdbc

我正在用 Java 编写一个数据库访问器.数据库是Oracle 11g,完全不熟悉,JDK 1.6.

I'm writing a database accessor in Java. The database is in Oracle 11g, of which I am absolutely not familiar, and I have JDK 1.6.

  1. ojdbc4.jar 对我的程序有用吗?我们在办公室里不允许连接到 Internet,我也无法下载 ojdbc6.jar,我读过它与我的设置更兼容.
  2. 我应该在 Class.forName(String driver) 和 DriverManager.getConnection(String connectionURL) 中放入哪些字符串?我不知道驱动程序字符串和连接 URL,因为它们(自然地)看起来与 MS SQL Server 的非常不同.

推荐答案

  1. Oracle 将 Jar 与 Oracle 客户端或服务器安装捆绑在一起,可以在 $ORACLE_HOME/jdbc/lib/ojdbc6.jar 中找到.我总是用那个.

  1. Oracle bundle the Jar with the Oracle client or server installation and can be found in $ORACLE_HOME/jdbc/lib/ojdbc6.jar. I always use that one.

Driver 类名是 oracle.jdbc.OracleDriver,URL 是 jdbc:oracle:thin:@//[HOST][:PORT]/SERVICE>.

The Driver classname is oracle.jdbc.OracleDriver and the URL is jdbc:oracle:thin:@//[HOST][:PORT]/SERVICE.

这是一个示例(取自此处):

Here's an example (taken from here):

import java.sql.*;
class Conn {
  public static void main (String[] args) throws Exception
  {
   Class.forName ("oracle.jdbc.OracleDriver");

   Connection conn = DriverManager.getConnection
     ("jdbc:oracle:thin:@//localhost:1521/orcl", "scott", "tiger");
                        // @//machineName:port/SID,   userid,  password
   try {
     Statement stmt = conn.createStatement();
     try {
       ResultSet rset = stmt.executeQuery("select BANNER from SYS.V_$VERSION");
       try {
         while (rset.next())
           System.out.println (rset.getString(1));   // Print col 1
       } 
       finally {
          try { rset.close(); } catch (Exception ignore) {}
       }
     } 
     finally {
       try { stmt.close(); } catch (Exception ignore) {}
     }
   } 
   finally {
     try { conn.close(); } catch (Exception ignore) {}
   }
  }
}

相关文章