博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
jdbc连接池工作原理_JDBC连接实际上如何工作?
阅读量:2530 次
发布时间:2019-05-11

本文共 5102 字,大约阅读时间需要 17 分钟。

jdbc连接池工作原理

The JDBC Connection interface is a part of java.sql package. The java.sql.Connection interface defines the contract for relational database vendors. The vendor connection classes must implement these methods. An instance of Connection is used to communicate with the Database.

JDBC Connection接口是java.sql包的一部分。 java.sql.Connection接口定义关系数据库供应商的合同。 供应商连接类必须实现这些方法。 Connection的一个实例用于与数据库进行通信。

The implementation of the Connection interface depends on the database vendor. So we don’t need to worry about implementation details as long as the vendor follows the contract.

Connection接口的实现取决于数据库供应商。 因此,只要供应商遵守合同,我们就不必担心实施细节。

如何建立JDBC连接 (How to Establish JDBC Connection)

Let’s have a look at the below steps to establish JDBC Connection.

让我们看一下以下建立JDBC连接的步骤。

1.注册JDBC驱动程序 (1. Register JDBC Driver)

JDBC driver is registered using below method.

使用以下方法注册JDBC驱动程序。

Class.forName(String className): This method is used to load the JDBC driver class into JVM memory. The string argument is the fully classified name of the Connection implementation class.

Class.forName(String className) :此方法用于将JDBC驱动程序类加载到JVM内存中。 字符串参数是Connection实现类的完全分类的名称。

// for MySQLClass.forName("com.mysql.jdbc.Driver");// for PostgreSQLClass.forName("org.postgresql.Driver");

Let’s understand what is happening behind the scene. How JDBC Driver gets registered by using this syntax.

让我们了解幕后发生的事情。 使用此语法如何注册JDBC驱动程序。

First of all, the JDBC driver’s jar file is included in the classpath of the java application. The DriverManager class uses service provider mechanism to search for jar files having a file named java.sql.Driver. This file must be present in the META-INF/services folder of the driver’s jar.

首先,JDBC驱动程序的jar文件包含在java应用程序的类路径中。 DriverManager类使用服务提供者机制来搜索具有名为java.sql.Driver的文件的jar文件。 该文件必须存在于驱动程序jar的META-INF / services文件夹中。

This is a simple text file that contains the full name of the class that implements the java.sql.Driver interface. For example, full name for MySQL Driver is com.mysql.jdbc.Driver. For PostgreSQL Driver, it is org.postgresql.Driver.

这是一个简单的文本文件,其中包含实现java.sql.Driver接口的类的全名。 例如,MySQL驱动程序的全名是com.mysql.jdbc.Driver。 对于PostgreSQL驱动程序,它是org.postgresql.Driver。

Jdbc Connection Implementation Jar

Jdbc Connection Implementation Jar

Jdbc连接实现Jar

When the Driver class is loaded into memory, its initialization block is executed. The static method of the DriverManager class is called to register the driver.

将Driver类加载到内存时,将执行其初始化块。 调用DriverManager类的静态方法来注册驱动程序。

public class VendorDriver implements java.sql.Driver{ static {	VendorDriver driver = new VendorDriver();	DriverManager.registerDriver(driver);          }}
Jdbc Connection Register Driver

Jdbc Connection Register Driver

Jdbc连接寄存器驱动程序

This will register an instance of Driver class into the DriverManager.

这会将Driver类的实例注册到DriverManager中。

2.创建JDBC连接 (2. Create JDBC Connection)

We can get the JDBC connection using DriverManager.getConnection() method.

我们可以使用DriverManager.getConnection()方法获得JDBC连接。

Connection conn = DriverManager.getConnection(url, user, password);

Let’s understand the parameters that we have passed to get the JDBC connection.

让我们了解为获取JDBC连接而传递的参数。

  • url: The JDBC URL which is used to determine which driver implementation to use for a given connection.
    jdbc:postgresql://hostname:port/databasenamejdbc:postgresql://127.0.0.1:5432/javadb

    The first part indicates that this is the JDBC URL.

    The second part indicates the driver vendor which is PostgreSQL in our case.

    The third part indicates the hostname or IP address with the port of the database.

    The last part indicates the name of the database.

    url :JDBC URL,用于确定用于给定连接的驱动程序实现。

    第一部分指示这是JDBC URL。

    第二部分说明驱动程序供应商,在本例中为PostgreSQL。

    第三部分用数据库端口指示主机名或IP地址。

    最后一部分指示数据库的名称。

  • user: user of the database for which we get the connection.

    user :我们为其连接的数据库的用户。
  • password: password to access the given database.

    password :访问给定数据库的密码。
Creating Jdbc Connection

Creating Jdbc Connection

创建Jdbc连接

JDBC连接示例 (JDBC Connection Example)

Let’s have a look at the below example program. In below program, I have used PostgreSQL database so I have used the respective jar for PostgreSQL.

让我们看一下下面的示例程序。 在下面的程序中,我使用了PostgreSQL数据库,所以我在PostgreSQL中使用了相应的jar。

package com.journaldev.examples;import java.sql.Connection;import java.sql.DriverManager;/** * JDBC Connection Example *  * @author pankaj * */public class JDBCConnection {	public static void main(String[] args) {		Connection connection = null;		try {			System.out.println("------JDBC Connection Example--------");			Class.forName("org.postgresql.Driver");			connection = DriverManager.getConnection("jdbc:postgresql://localhost:5432/journaldev", "postgres", "admin");			System.out.println("----JDBC Connection Established------");			System.out.println("Catalog: "+connection.getCatalog());			System.out.println("Schema: "+connection.getSchema());			System.out.println("Info: "+connection.getClientInfo());		} catch (Exception e) {			e.printStackTrace();		}finally {			try {				if (connection!=null) {					connection.close();					System.out.println("Connection is Closed.");				}			} catch (Exception e2) {				e2.printStackTrace();			}		}			}}

Output:

输出

Establish Jdbc Connection Program Output

Establish Jdbc Connection Program Output

建立Jdbc连接程序输出

翻译自:

jdbc连接池工作原理

转载地址:http://cuqzd.baihongyu.com/

你可能感兴趣的文章
AtCoder Grand Contest 031 B - Reversi
查看>>
完整成功配置wamp server小记
查看>>
build.gradle添加Oracle jdbc6 链接
查看>>
影响系统性能的20个瓶颈--转自开源中国
查看>>
根据ISBN获取豆瓣API提供的图书信息
查看>>
【转】Python中*args和**kwargs的区别
查看>>
git命令简单使用
查看>>
CODEFORCES 125E MST Company 巧用Kruskal算法
查看>>
C++标准库分析总结(三)——<迭代器设计原则>
查看>>
Ibatis 配置问题
查看>>
Notability 3.1 for Mac 中文共享版 – 好用的文本笔记工具
查看>>
HDU 2089 数位dp入门
查看>>
How do I resolve the CodeSign error: CSSMERR_TP_NOT_TRUSTED?
查看>>
linux下添加定时任务
查看>>
python的第三篇--安装Ubuntu、pycharm
查看>>
LeetCode 1092. Shortest Common Supersequence
查看>>
《区块链技术与应用》北京大学肖臻老师公开课 笔记
查看>>
139句
查看>>
购买《哈利波特》书籍
查看>>
谈谈一些网页游戏失败的原因到底有哪些?(转)
查看>>