什么 jdbc jar 与 oracle 11g & jdk 1.6 一起使用以及如何连接到数据库本身
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8007174/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
what jdbc jar to use with oracle 11g & jdk 1.6 and how to connect to the db itself
提问by Matthew Quiros
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.
我正在用 Java 编写一个数据库访问器。数据库是Oracle 11g,我绝对不熟悉,我有JDK 1.6。
- Will ojdbc4.jar do for my program? We're not allowed to connect to the Internet in the office and I can't download ojdbc6.jar, which I've read is more compatible with my setup.
- What strings should I put in the Class.forName(String driver) and DriverManager.getConnection(String connectionURL)? I don't know the driver string and the connection URL since they (naturally) look very different from the ones for MS SQL Server.
- ojdbc4.jar 对我的程序有用吗?我们不允许在办公室连接到 Internet,我也无法下载 ojdbc6.jar,我读过它与我的设置更兼容。
- 我应该在 Class.forName(String driver) 和 DriverManager.getConnection(String connectionURL) 中放入哪些字符串?我不知道驱动程序字符串和连接 URL,因为它们(自然地)看起来与 MS SQL Server 的非常不同。
回答by trojanfoe
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.The Driver classname is
oracle.jdbc.OracleDriver
and the URL isjdbc:oracle:thin:@//[HOST][:PORT]/SERVICE
.
Oracle 将 Jar 与 Oracle 客户端或服务器安装捆绑在一起,可以在
$ORACLE_HOME/jdbc/lib/ojdbc6.jar
. 我总是用那个。驱动程序类名是
oracle.jdbc.OracleDriver
,URL 是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) {}
}
}
}
回答by Codo
The offical JAR file in combination with JDK 1.6 is ojdbc6.jar
. But ojdbc4.jar
should work for most applications.
结合 JDK 1.6 的官方 JAR 文件是ojdbc6.jar
. 但ojdbc4.jar
应该适用于大多数应用程序。
Typicall connection strings are:
典型的连接字符串是:
jdbc:oracle:thin:user/xxxx@server:port:SID
jdbc:oracle:thin:user/xxxx@//server:port/XE
jdbc:oracle:thin:user/xxxx@:SID