- 浏览: 123522 次
- 性别:
- 来自: 成都
-
文章分类
1.加载驱动程序
Class.forName("oracle.jdbc.driver.OracleDriver");
Class.forName("oracle.jdbc.driver.OracleDriver");
2. 建立连接
Connection conn = DriverMananger.getConnection("jdbc:oracle:thin:@127.0.0.1:1521:xe","用户名","密码");
3. 执行查询
4. 关闭数据库连接
==============实际的代码=================
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class JDBCExample {
public static void main(String[] args) {
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
String url="jdbc:oracle:thin:@127.0.0.1:1521:ora9";
try {
Connection conn=DriverManager.getConnection(url,"scott","tiger");
Statement stmt=conn.createStatement();
ResultSet rs=stmt.executeQuery("select * from dept");
while(rs.next()){
System.out.println("deptno"+rs.getInt(1));
System.out.println("\tDname: "+rs.getString(2));
System.out.println("\tLoc: "+rs.getString(3));
}
rs.close();
stmt.close();
conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class JDBCExample {
public static void main(String[] args) {
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
String url="jdbc:oracle:thin:@127.0.0.1:1521:ora9";
try {
Connection conn=DriverManager.getConnection(url,"scott","tiger");
Statement stmt=conn.createStatement();
ResultSet rs=stmt.executeQuery("select * from dept");
while(rs.next()){
System.out.println("deptno"+rs.getInt(1));
System.out.println("\tDname: "+rs.getString(2));
System.out.println("\tLoc: "+rs.getString(3));
}
rs.close();
stmt.close();
conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
thin VS oci
先看看thin和oci的url写法上的区别: jdbc:oracle:thin:@server ip: service jdbc:oracle:oci:@service 1)从使用上来说,oci必须在客户机上安装oracle客户端或才能连接,而thin就不需要,因此从使用上来讲thin还是更加方便,这也是thin比较常见的原因。 2)原理上来看,thin是纯java实现tcp/ip的c/s通讯;而oci方式,客户端通过native java method调用c library访问服务端,而这个c library就是oci(oracle called interface),因此这个oci总是需要随着oracle客户端安装(从oracle10.1.0开始,单独提供OCI Instant Client,不用再完整的安装client) 3)它们分别是不同的驱动类别,oci是二类驱动, thin是四类驱动,但它们在功能上并无差异。 4)虽然很多人说oci的速度快于thin,但找了半天没有找到相关的测试报告。
接下来再找找oci和thin的其他区别,发现有如下解释:
Oracle provides four different types of JDBC drivers, for use in different deployment scenarios. The 10.1.0 drivers can access Oracle 8.1.7 and higher. While all Oracle JDBC drivers are similar, some features apply only to JDBC OCI drivers and some apply only to the JDBC Thin driver.
JDBC OCI client-side driver: This is a JDBC Type 2 driver that uses Java native methods to call entrypoints in an underlying C library. That C library, called OCI (Oracle Call Interface), interacts with an Oracle database. The JDBC OCI driver requires an Oracle client installation of the same version as the driver.
The use of native methods makes the JDBC OCI driver platform specific. Oracle supports Solaris, Windows, and many other platforms. This means that the Oracle JDBC OCI driver is not appropriate for Java applets, because it depends on a C library.
Starting from 10.1.0, the JDBC OCI driver is available for install with the OCI Instant Client feature, which does not require a complete Oracle client-installation. Please refer to Oracle Call Interface for more information.
JDBC Thin client-side driver: This is a JDBC Type 4 driver that uses Java to connect directly to Oracle. It implements Oracle's SQL*Net Net8 and TTC adapters using its own TCP/IP based Java socket implementation. The JDBC Thin driver does not require Oracle client software to be installed, but does require the server to be configured with a TCP/IP listener.
Because it is written entirely in Java, this driver is platform-independent. The JDBC Thin driver can be downloaded into any browser as part of a Java application. (Note that if running in a client browser, that browser must allow the applet to open a Java socket connection back to the server.)
JDBC Thin server-side driver: This is another JDBC Type 4 driver that uses Java to connect directly to Oracle. This driver is used internally within the Oracle database. This driver offers the same functionality as the client-side JDBC Thin driver (above), but runs inside an Oracle database and is used to access remote databases.
Because it is written entirely in Java, this driver is platform-independent. There is no difference in your code between using the Thin driver from a client application or from inside a server.
连接方式有以下几种:
Oralce provides four types of JDBC driver.
Thin Driver, a 100% Java driver for client-side use without an Oracle installation, particularly with applets. The Thin driver type is thin. To connect user scott with password tiger to a database with SID (system identifier) orcl through port 1521 of host myhost, using the Thin driver, you would write :
Connection conn = DriverManager.getConnection
("jdbc:oracle:thin:@myhost:1521:orcl", "scott", "tiger");
OCI Driver for client-side use with an Oracle client installation. The OCI driver type is oci. To connect user scott with password tiger to a database with SID (system identifier) orcl through port 1521 of host myhost, using the OCI driver, you would write :
Connection conn = DriverManager.getConnection
("jdbc:oracle:oci:@myhost:1521:orcl", "scott", "tiger");
Note that you can also specify the database by a TNSNAMES entry. You can find the available TNSNAMES entries listed in the file tnsnames.ora on the client computer from which you are connecting. For example, if you want to connect to the database on host myhost as user scott with password tiger that has a TNSNAMES entry of MyHostString, enter:
Connection conn = DriverManager.getConnection
("jdbc:oracle:oci8:@MyHostString","scott","tiger");
If your JDBC client and Oracle server are running on the same machine, the OCI driver can use IPC (InterProcess Communication) to connect to the database instead of a network connection. An IPC connection is much faster than a network connection.
Connection conn = DriverManager.getConnection
("jdbc:oracle:oci8:@","scott","tiger");
Server-Side Thin Driver, which is functionally the same as the client-side Thin driver, but is for code that runs inside an Oracle server and needs to access a remote server, including middle-tier scenarios. The Server-Side Thin driver type is thin and there is no difference in your code between using the Thin driver from a client application or from inside a server.
Server-Side Internal Driver for code that runs inside the target server, that is, inside the Oracle server that it must access. The Server-Side Internal driver type is kprb and it actually runs within a default session. You are already "connected". Therefore the connection should never be closed.
To access the default connection, write:
DriverManager.getConnection("jdbc:oracle:kprb:");
or:
DriverManager.getConnection("jdbc:default:connection:");
You can also use the Oracle-specific defaultConnection() method of the OracleDriver class which is generally recommended:
OracleDriver ora = new OracleDriver();
Connection conn = ora.defaultConnection();
Note: You are no longer required to register the OracleDriver class for connecting with the Server-Side Internal driver, although there is no harm in doing so. This is true whether you are using getConnection() or defaultConnection() to make the connection.
Any user name or password you include in the URL string is ignored in connecting to the server default connection. The DriverManager.getConnection() method returns a new Java Connection object every time you call it. Note that although the method is not creating a new physical connection (only a single implicit connection is used), it is returning a new object.
Again, when JDBC code is running inside the target server, the connection is an implicit data channel, not an explicit connection instance as from a client. It should never be closed.
引用
Oracle provides four different types of JDBC drivers, for use in different deployment scenarios. The 10.1.0 drivers can access Oracle 8.1.7 and higher. While all Oracle JDBC drivers are similar, some features apply only to JDBC OCI drivers and some apply only to the JDBC Thin driver.
JDBC OCI client-side driver: This is a JDBC Type 2 driver that uses Java native methods to call entrypoints in an underlying C library. That C library, called OCI (Oracle Call Interface), interacts with an Oracle database. The JDBC OCI driver requires an Oracle client installation of the same version as the driver.
The use of native methods makes the JDBC OCI driver platform specific. Oracle supports Solaris, Windows, and many other platforms. This means that the Oracle JDBC OCI driver is not appropriate for Java applets, because it depends on a C library.
Starting from 10.1.0, the JDBC OCI driver is available for install with the OCI Instant Client feature, which does not require a complete Oracle client-installation. Please refer to Oracle Call Interface for more information.
JDBC Thin client-side driver: This is a JDBC Type 4 driver that uses Java to connect directly to Oracle. It implements Oracle's SQL*Net Net8 and TTC adapters using its own TCP/IP based Java socket implementation. The JDBC Thin driver does not require Oracle client software to be installed, but does require the server to be configured with a TCP/IP listener.
Because it is written entirely in Java, this driver is platform-independent. The JDBC Thin driver can be downloaded into any browser as part of a Java application. (Note that if running in a client browser, that browser must allow the applet to open a Java socket connection back to the server.)
JDBC Thin server-side driver: This is another JDBC Type 4 driver that uses Java to connect directly to Oracle. This driver is used internally within the Oracle database. This driver offers the same functionality as the client-side JDBC Thin driver (above), but runs inside an Oracle database and is used to access remote databases.
Because it is written entirely in Java, this driver is platform-independent. There is no difference in your code between using the Thin driver from a client application or from inside a server.
连接方式有以下几种:
Oralce provides four types of JDBC driver.
Thin Driver, a 100% Java driver for client-side use without an Oracle installation, particularly with applets. The Thin driver type is thin. To connect user scott with password tiger to a database with SID (system identifier) orcl through port 1521 of host myhost, using the Thin driver, you would write :
Connection conn = DriverManager.getConnection
("jdbc:oracle:thin:@myhost:1521:orcl", "scott", "tiger");
OCI Driver for client-side use with an Oracle client installation. The OCI driver type is oci. To connect user scott with password tiger to a database with SID (system identifier) orcl through port 1521 of host myhost, using the OCI driver, you would write :
Connection conn = DriverManager.getConnection
("jdbc:oracle:oci:@myhost:1521:orcl", "scott", "tiger");
Note that you can also specify the database by a TNSNAMES entry. You can find the available TNSNAMES entries listed in the file tnsnames.ora on the client computer from which you are connecting. For example, if you want to connect to the database on host myhost as user scott with password tiger that has a TNSNAMES entry of MyHostString, enter:
Connection conn = DriverManager.getConnection
("jdbc:oracle:oci8:@MyHostString","scott","tiger");
If your JDBC client and Oracle server are running on the same machine, the OCI driver can use IPC (InterProcess Communication) to connect to the database instead of a network connection. An IPC connection is much faster than a network connection.
Connection conn = DriverManager.getConnection
("jdbc:oracle:oci8:@","scott","tiger");
Server-Side Thin Driver, which is functionally the same as the client-side Thin driver, but is for code that runs inside an Oracle server and needs to access a remote server, including middle-tier scenarios. The Server-Side Thin driver type is thin and there is no difference in your code between using the Thin driver from a client application or from inside a server.
Server-Side Internal Driver for code that runs inside the target server, that is, inside the Oracle server that it must access. The Server-Side Internal driver type is kprb and it actually runs within a default session. You are already "connected". Therefore the connection should never be closed.
To access the default connection, write:
DriverManager.getConnection("jdbc:oracle:kprb:");
or:
DriverManager.getConnection("jdbc:default:connection:");
You can also use the Oracle-specific defaultConnection() method of the OracleDriver class which is generally recommended:
OracleDriver ora = new OracleDriver();
Connection conn = ora.defaultConnection();
Note: You are no longer required to register the OracleDriver class for connecting with the Server-Side Internal driver, although there is no harm in doing so. This is true whether you are using getConnection() or defaultConnection() to make the connection.
Any user name or password you include in the URL string is ignored in connecting to the server default connection. The DriverManager.getConnection() method returns a new Java Connection object every time you call it. Note that although the method is not creating a new physical connection (only a single implicit connection is used), it is returning a new object.
Again, when JDBC code is running inside the target server, the connection is an implicit data channel, not an explicit connection instance as from a client. It should never be closed.
发表评论
-
eclipselink-DDL Schema Generation的四种方式
2016-10-12 00:17 590persistence.xml文件配置: < ... -
Java开发中的23种设计模式
2016-09-28 00:40 575设计模式(Design Patterns) ... -
ManyToOne 双向一对多关系
2016-08-05 01:38 528双向一对多关系,一是关系维护端(owner side),多是 ... -
理解RESTful架构
2016-06-22 00:33 580原文:http://www.ruanyifen ... -
foreach循环
2016-05-31 22:23 497这种有冒号的for循环叫做foreach循环,foreach语 ... -
java几种常用设计模式简单示例
2016-05-19 23:02 538PART A:前言 平常我们都在敲代码,为了要实现一些我们 ... -
iText PdfPCell内容水平垂直居中
2016-01-14 00:13 11912先调用Cell.setUseAscender(true);再调 ... -
深入理解 hash 函数、HashMap
2015-12-15 00:52 654http://www.2cto.com/kf/201409/3 ... -
iText的showTextAligned方法
2015-12-06 16:47 5548java使用itext的showTextAligned方法给 ... -
iText PdfTemplate的使用
2015-12-06 02:32 1441在开发系统时,需要在PDF上写入总页数。于是在网上搜索到 ... -
iText表格 分页
2015-11-30 23:31 5257前言 在上一节中,通过listing 4.16产生的表格拥 ... -
iText生成PDF文档部分页面横置
2015-11-27 02:02 5576整个PDF文档页面设置 Rectangle rect ... -
iText生成PDF格式设置
2015-11-27 00:52 2557import java.io.ByteArrayOutputS ... -
Spring中的IOC和AOP
2015-11-19 00:47 522IOC,依赖倒置的意思,所谓依赖,从程序的角度看,就是比如A要 ... -
【转载】纯Java获得本地MAC地址
2015-07-29 21:18 6081 import java.net.*; 2 3 clas ... -
Java笔试题
2014-05-25 21:53 5811. float型float f=3.4是否正 ... -
单例模式的常见应用场景
2014-05-25 21:30 881单例模式(Singleton)也叫单态模式,是设计模式中最 ... -
深入Java单例模式
2014-05-25 21:29 590在GoF的23种设计模式中,单例模式是比较简单的一种。然而 ... -
java 异常捕捉 ( try catch finally )
2014-05-25 21:15 589前言:java 中的异常处理机制你真的理解了吗?掌握了吗?c ... -
多线程的实现
2014-05-15 02:11 530http://www.cnblogs.com/rollenho ...
相关推荐
### JDBC连接MySQL数据库关键的四个步骤 在现代软件开发中,Java作为一种广泛使用的编程语言,其与数据库的交互是必不可少的一部分。JDBC(Java Database Connectivity)作为Java平台上的标准数据库访问接口,允许...
### JDBC连接数据库步骤详解 #### 一、简介 在Java开发中,经常需要与数据库进行交互,例如查询数据、更新数据等操作。为了方便开发者进行这些操作,Sun公司推出了一套标准的Java访问数据库的API——Java Database...
### JDBC连接数据库的详细步骤与解析 #### 一、加载JDBC驱动 ...以上六个步骤构成了使用JDBC连接和操作数据库的完整流程。理解并熟练掌握这些步骤,将有助于开发者有效地在Java应用程序中集成数据库功能。
本文将详细介绍如何使用JDBC连接数据库,并按照以下七个步骤进行: 1. 加载JDBC驱动程序 2. 提供JDBC连接的URL 3. 创建数据库的连接 4. 创建一个Statement 5. 执行SQL语句 6. 处理结果集 7. 关闭连接 #### 二、...
### JDBC连接所有数据库步骤详解 #### 一、概述 Java Database Connectivity (JDBC) 是 Java 平台上的标准 SQL 数据库访问接口。它允许 Java 应用程序与各种类型的 SQL 数据库进行交互,包括 Oracle、MySQL、SQL ...
JDBC 连接数据库方法...JDBC 连接数据库的方法可以分为四个步骤:加载 JDBC 驱动程序、提供 JDBC 连接的 URL、创建数据库的连接、创建一个 Statement。通过这四个步骤,开发者可以轻松地连接数据库并执行 SQL 语句。
综上所述,JDBC连接数据库主要包括加载驱动、建立连接、执行SQL和关闭连接四个步骤。通过封装这些操作在类中,可以方便地在应用程序中复用。在实际开发中,还需要根据具体需求对异常处理、事务管理等方面进行优化和...
本文将详细介绍如何使用JDBC连接Oracle、SqlServer、MySql和Access这四种常见的数据库。 一、Oracle数据库连接 Oracle数据库是企业级的大型关系型数据库管理系统。在Java中,我们通常会使用`ojdbc`驱动来连接Oracle...
配置Tomcat服务器以支持JDBC连接,主要涉及以下步骤: 1. **确认Tomcat已安装**:检查是否已正确安装Tomcat服务器。 2. **配置Classpath**:打开Tomcat的监控界面,找到Java选项卡下的Java Classpath设置,添加`sql...
3. **建立数据库连接**:使用以下步骤建立JDBC连接: - 加载驱动:`Class.forName("oracle.jdbc.driver.OracleDriver");` - 创建连接:`Connection conn = DriverManager.getConnection(url, username, password);...
Sql Server 2008 连接 JDBC 步骤 一、Sql Server 2008 的身份验证方式 Sql Server 2008 的默认身份验证方式是“Windows 身份验证”,但是 JDBC 不支持这种方式。JDBC 支持 SQL Server 和 Windows 身份验证方式,也...
一、Java中Jdbc连接Oracle数据库的基本步骤 1. 导入Jdbc驱动包:在Java程序中需要引入Oracle的Jdbc驱动包,通常是oracle.jdbc.driver.OracleDriver。 2. 注册Jdbc驱动:使用Class.forName()方法注册Jdbc驱动。 3. ...
#### 二、JDBC连接数据库步骤详解 ##### 1. 加载JDBC驱动 加载JDBC驱动是与数据库建立连接的第一步。这一步通常通过调用`Class.forName()`方法来完成,例如: ```java Class.forName("com.mysql.jdbc.Driver"); ```...
### JDBC连接驱动大全 #### 一、MySQL连接配置 **MySQL** 是一款广泛使用的开源关系型数据库管理系统。要通过 Java 应用程序连接 MySQL 数据库,可以使用 **MySQL JDBC 驱动**。 - **驱动文件**: `mm.mysql-2.0.2...
这里我们有ojdbc6_g.jar、ojdbc5_g.jar、ojdbc6.jar和ojdbc5.jar四个文件。其中,“_g”后缀通常表示包含了调试信息,对于开发和问题排查有所帮助,但在生产环境中,一般使用不带“_g”的版本以减少应用大小。 连接...
本文将详细介绍如何使用 JDBC 连接 SQL Server 数据库,并创建表、插入数据以及查询数据的具体步骤。 #### 二、JDBC 连接 SQL Server 数据库的关键步骤 ##### 2.1 加载 JDBC 驱动 在 Java 应用程序中使用 JDBC ...
这通常涉及以下四个步骤: 1. **加载驱动**:在Java程序中,我们需要加载对应的数据库驱动。这可以通过`Class.forName()`方法完成,例如,对于SQL Server 2000或2005,我们可以使用`Class.forName(...
### 使用JDBC连接Oracle数据库 #### 一、简介与背景 Java Database Connectivity (JDBC) 是 Java 开发语言中的一项关键技术,它允许开发者通过标准 API 与多种类型的数据库进行交互。JDBC 提供了一种机制,使 Java...
`jconn4`是Sybase JDBC驱动的第四版本,提供了更高效、更稳定的数据库连接功能,并且支持更多的数据库特性。 首先,了解JDBC是理解`jconn4`的关键。JDBC是Java平台上的一个标准API,允许Java应用程序与各种不同类型...
### PostgreSql 连接任意数据库使用 JDBC_FDW 插件详解 ...以上就是使用 JDBC_FDW 连接任意数据库的详细步骤。通过这种方式,可以方便地在 PostgreSQL 中操作来自其他数据库的数据,为跨数据库操作提供了极大的灵活性。