jdbc简介
Java Database Connectivity(JDBC) is an Application Programming Interface(API) used to connect Java application with Database. JDBC is used to interact with various type of Database such as Oracle, MS Access, My SQL and SQL Server. JDBC can also be defined as the platform-independent interface between a relational database and Java programming. It allows java program to execute SQL statement and retrieve result from database.
Java数据库连接(JDBC)是用于将Java应用程序与数据库连接的应用程序编程接口(API) 。 JDBC用于与各种类型的数据库进行交互,例如Oracle,MS Access,My SQL和SQL Server。 JDBC也可以定义为关系数据库和Java编程之间与平台无关的接口。 它允许Java程序执行SQL语句并从数据库检索结果。
The JDBC API consists of classes and methods that are used to perform various operations like: connect, read, write and store data in the database. In this tutorial we will learn the JDBC with examples.
JDBC API由用于执行各种操作的类和方法组成,这些类和方法包括:在数据库中连接,读取,写入和存储数据。 在本教程中,我们将通过示例学习JDBC。
You can get idea of how JDBC connect Java Application to the database by following image.
您可以通过下图了解JDBC如何将Java应用程序连接到数据库。
JDBC 4.0的新增功能 (What's new in JDBC 4.0)
Java introduced JDBC 4.0, a new version which is advance specification of JDBC. It provides the following advance features
Java引入了JDBC 4.0 ,这是JDBC的高级规范的新版本。 它提供以下高级功能
Connection Management
连接管理
Auto loading of Driver Interface.
自动加载驱动程序接口。
Better exception handling
更好的异常处理
Support for large object
支持大物件
Annotation in SQL query.
SQL查询中的注释。
JDBC 4.1 (JDBC 4.1)
The JDBC 4.1 version was introduced with Java SE 7 and includes the following features:
JDBC 4.1版本是Java SE 7引入的,具有以下功能:
Allows to use a try-with-resources statement to automatically close resources of type Connection, ResultSet etc.
允许使用try-with-resources语句自动关闭Connection,ResultSet等类型的资源。
Introduced the RowSetFactory interface and the RowSetProviderclass to create all types of row sets supported by your JDBC driver.
引入了RowSetFactory接口和RowSetProvider类,以创建JDBC驱动程序支持的所有类型的行集。
JDBC 4.2 (JDBC 4.2 )
version is introduced with Java SE 8 and includes the following features:
该版本随Java SE 8一起推出,并具有以下功能:
The REF_CURSOR support.
REF_CURSOR支持。
Added an interface java.sql.DriverAction
添加了接口java.sql.DriverAction
Added an interface java.sql.SQLType
添加了接口java.sql.SQLType
Added an Enum java.sql.JDBCType
添加了一个枚举java.sql.JDBCType
Add Support for large update counts
添加对大量更新的支持
Improved the existing interfaces: Driver, DriverManage, DatabaseMetaData.
改进了现有接口: Driver,DriverManage,DatabaseMetaData 。
Interfaces and classes enhanced for RowSet1.2:
RowSet1.2增强的接口和类:
JDBC 4.3 (JDBC 4.3)
Added support to Statement for enquoting literals and simple identifiers
增加了对对单引号和简单标识符加引号的声明的支持
Added Sharding support
添加了分片支持
Enhanced Connection to be able to provide hints to the driver that a request, an independent unit of work, is beginning or ending
增强的连接能够向驱动程序提供提示,即一个独立工作单元的请求正在开始或结束
Enhanced DatabaseMetaData to determine if Sharding is supported
增强的DatabaseMetaData以确定是否支持分片
Added the method drivers to DriverManager to return a Stream of the currently loaded and available JDBC drivers
向DriverManager添加了方法驱动程序,以返回当前已加载且可用的JDBC驱动程序的流
JDBC驱动程序 (JDBC Driver)
JDBC Driver is required to establish connection between application and database. It also helps to process SQL requests and generating result. The following are the different types of driver available in JDBC which are used by the application based on the scenario and type of application.
需要JDBC驱动程序才能在应用程序和数据库之间建立连接。 它还有助于处理SQL请求并生成结果。 以下是JDBC中可用的不同类型的驱动程序,这些驱动程序由应用程序根据场景和应用程序类型使用。
Type-1 Driver or JDBC-ODBC bridge
Type-1驱动程序或JDBC-ODBC桥
Type-2 Driver or Native API Partly Java Driver
Type-2驱动程序或本机API部分为Java驱动程序
Type-3 Driver or Network Protocol Driver
Type-3驱动程序或网络协议驱动程序
Type-4 Driver or Thin Driver
Type-4驱动程序或瘦驱动程序
JDBC-ODBC桥 (JDBC-ODBC bridge)
Type-1 Driver act as a bridge between JDBC and other database connectivity mechanism(ODBC). This driver converts JDBC calls into ODBC calls and redirects the request to the ODBC driver.
Type-1驱动程序充当JDBC与其他数据库连接机制(ODBC)之间的桥梁。 该驱动程序将JDBC调用转换为ODBC调用,并将请求重定向到ODBC驱动程序。
Note: In Java 8, the JDBC-ODBC Bridge has been removed.
注意 :在Java 8中,已删除JDBC-ODBC Bridge。
Advantage
优点
Easy to use
易于使用
Allow easy connectivity to all database supported by the ODBC Driver.
允许轻松连接到ODBC驱动程序支持的所有数据库。
Disadvantage
坏处
Slow execution time
执行时间慢
Dependent on ODBC Driver.
取决于ODBC驱动程序。
Uses Java Native Interface(JNI) to make ODBC call.
使用Java本机接口(JNI)进行ODBC调用。
本机API驱动程序 (Native API Driver)
This type of driver make use of Java Native Interface(JNI) call on database specific native client API. These native client API are usually written in C and C++.
这种类型的驱动程序利用对数据库特定的本机客户端API的Java本机接口(JNI)调用。 这些本机客户端API通常用C和C ++编写。
Advantage
优点
faster as compared to Type-1 Driver
与Type-1驱动程序相比更快
Contains additional features.
包含其他功能。
Disadvantage
坏处
Requires native library
需要本地库
Increased cost of Application
申请费用增加
网络协议驱动程序 (Network Protocol Driver)
This driver translate the JDBC calls into a database server independent and Middleware server-specific calls. Middleware server further translate JDBC calls into database specific calls.
该驱动程序将JDBC调用转换为独立于数据库服务器和特定于中间件服务器的调用。 中间件服务器将JDBC调用进一步转换为数据库特定的调用。
Advantage
优点
Does not require any native library to be installed.
不需要安装任何本机库。
Database Independency.
数据库独立性。
Provide facility to switch over from one database to another database.
提供从一个数据库切换到另一个数据库的功能。
Disadvantage
坏处
Slow due to increase number of network call.
由于网络通话数量增加而变慢。
薄驱动器 (Thin Driver)
This is Driver called Pure Java Driver because. This driver interact directly with database. It does not require any native database library, that is why it is also known as Thin Driver.
这是因为驱动程序称为Pure Java Driver。 该驱动程序直接与数据库交互。 它不需要任何本机数据库库,因此也被称为Thin Driver。
Advantage
优点
Does not require any native library.
不需要任何本机库。
Does not require any Middleware server.
不需要任何中间件服务器。
Better Performance than other driver.
比其他驱动程序更好的性能。
Disadvantage
坏处
Slow due to increase number of network call.
由于网络通话数量增加而变慢。
DriverManager类 (DriverManager class)
在Java中,DriverManager将其分类为用户和驱动程序之间的接口。 此类用于监视驱动程序,该驱动程序用于建立数据库和驱动程序之间的连接。 DriverManager类具有已注册的Driver类列表,并称为DriverManager.registerDriver()
. DriverManager.registerDriver()
。S.No. | Method | Description |
---|---|---|
1 | public static void registerDriver(Driver driver) | It is used for Registering the Driver with the Driver Manager. |
2 | public static void deregisterDriver(Driver driver) | It is used for Deregistering the Driver with the Driver Manager. |
3 | public static Connection getConnection(String Url) | It is used for establishing a connection with the given URL. |
4 | public static Connection getConnection(String Url, String username, String password) | It is used for establishing the connection with the given URL, username and password. |
序号 | 方法 | 描述 |
---|---|---|
1个 | 公共静态无效registerDriver(驱动程序驱动程序) | 它用于在驱动程序管理器中注册驱动程序。 |
2 | 公共静态无效deregisterDriver(驱动程序驱动程序) | 它用于通过驱动程序管理器注销驱动程序。 |
3 | 公共静态连接getConnection(String Url) | 它用于与给定URL建立连接。 |
4 | 公共静态连接getConnection(String Url,String username,String password) | 它用于使用给定的URL,用户名和密码建立连接。 |
连接界面 (Connection interface)
In Java, The Connection interface is used for creating the session between the application and the database. This interface contains Statement, PreparedStatement and DatabaseMetaData. The connection objects are used in Statement and the DatabaseMetaData. commit(), rollback() etc.. are some of the methods of Connection Interface.
在Java中,Connection接口用于在应用程序和数据库之间创建会话。 该接口包含Statement,PreparedStatement和DatabaseMetaData。 连接对象用于Statement和DatabaseMetaData中。 commit(),rollback()等是Connection Interface的一些方法。
S.No. | Method | Description |
---|---|---|
1 | public Statement createStatement() | It is used for creating an object of statement for executing the SQL queries. |
2 | public Statement createStatement(intresultSetType,intresultSetConcurrency) | It is used for creating objects for the ResultSet from the given type and concurrency. |
3 | public void setAutoCommit(boolean status) | It is used for setting the commit status. By default, it is always true. |
4 | public void commit() | It is used to save the changes which have been commit or rollback permanent |
5 | public void rollback() | It is used to delete the changes which have been commit or rollback permanent |
6 | public void close() | It is used to delete the changes which have been commit or rollback permanent |
序号 | 方法 | 描述 |
---|---|---|
1个 | 公共声明createStatement() | 它用于创建执行SQL查询的语句对象。 |
2 | 公共声明createStatement(intresultSetType,intresultSetConcurrency) | 它用于根据给定的类型和并发性为ResultSet创建对象。 |
3 | public void setAutoCommit(布尔状态) | 用于设置提交状态。 默认情况下,它始终为true。 |
4 | 公共无效commit() | 它用于保存已被永久提交或回滚的更改 |
5 | 公共无效rollback() | 用于删除已提交或永久回滚的更改 |
6 | 公共无效close() | 用于删除已提交或永久回滚的更改 |
报表界面 (Statement interface)
In Java, The Statement interface is used for executing queries using the database. This interface is a factory of ResultSet. It is used to get the Object of ResultSet. Methods of this interface is given below.
在Java中,Statement接口用于使用数据库执行查询。 此接口是ResultSet的工厂。 它用于获取ResultSet的对象。 该接口的方法在下面给出。
S.No. | Method | Description |
---|---|---|
1 | public ResultSetexecuteQuery(String sql) | It is used for executing the SELECT query |
2 | public intexecuteUpdate(String sql) | It is used for executing any specified query |
3 | public boolean execute(String sql) | It is used when multiple results are required. |
4 | public int[] executeBatch() | It is used for executing the batch of commands. |
序号 | 方法 | 描述 |
---|---|---|
1个 | 公共ResultSetexecuteQuery(String sql) | 用于执行SELECT查询 |
2 | 公共intexecuteUpdate(String sql) | 用于执行任何指定的查询 |
3 | 公共布尔执行(String sql) | 当需要多个结果时使用。 |
4 | 公共int [] executeBatch() | 用于执行一批命令。 |
ResultSet界面 (ResultSet interface)
In Java, the ResultSet Interface is used for maintaining the pointer to a row of a table. In starting the pointer is before the first row. The object can be moved forward as well as backward direction using TYPE_SCROLL_INSENSITIVE or TYPE_SCROLL_SENSITIVE in createStatement(int,int). Methods of this interface is given below.
在Java中,ResultSet接口用于维护指向表行的指针。 在开始时,指针在第一行之前。 使用createStatement(int,int)中的TYPE_SCROLL_INSENSITIVE或TYPE_SCROLL_SENSITIVE可以向前和向后移动对象。 该接口的方法在下面给出。
S.No. | Method | Description |
---|---|---|
1 | public boolean next() | It is used for moving the cursor to the next position from the current position. |
2 | public boolean previous() | It is used for moving the cursor to the previous position from the current position. |
3 | public boolean first() | It is used for moving the cursor to the first position from the current position. |
4 | public booleanlast() | It is used for moving the cursor to the Last position from the current position. |
5 | public booleanabsolute(int row) | It is used for moving the cursor to the specified position from the current position. |
6 | public booleanrelative(int row) | It is used for moving the cursor to the relative row number from the current position. |
7 | public intgetInt(intcolumnIndex) | It is used to get the data from the specified position. |
8 | public intgetInt(String columnName) | It is used to get the data from the specified column name of the current row. |
9 | public StringgetString(intcolumnIndex) | It is used to get the data from the specified column name of the current row in form of an integer. |
10 | public StringgetString(StringcolumnIndex) | It is used to get the data from the specified column name of the current row in form of string. |
序号 | 方法 | 描述 |
---|---|---|
1个 | 布尔布尔值next() | 用于将光标从当前位置移动到下一个位置。 |
2 | 公共布尔的previous() | 用于将光标从当前位置移至上一个位置。 |
3 | public boolean first() | 用于将光标从当前位置移动到第一位置。 |
4 | 公共booleanlast() | 用于将光标从当前位置移动到最后一个位置。 |
5 | 公共booleanabsolute(int行) | 用于将光标从当前位置移动到指定位置。 |
6 | public booleanrelative(int row) | 它用于将光标从当前位置移动到相对行号。 |
7 | 公共intgetInt(intcolumnIndex) | 它用于从指定位置获取数据。 |
8 | 公共intgetInt(字符串columnName) | 它用于从当前行的指定列名获取数据。 |
9 | 公共StringgetString(intcolumnIndex) | 它用于以整数形式从当前行的指定列名称中获取数据。 |
10 | 公共StringgetString(StringcolumnIndex) | 它用于以字符串形式从当前行的指定列名称中获取数据。 |
PreparedStatement接口 (PreparedStatement interface)
In Java, The PreparedStatement interface is a subinterface of Statement. It is mainly used for the parameterized queries. A question mark (?) is passed for the values. The values to this question marks will be set by the PreparedStatement. Methods of this interface is given below.
在Java中,PreparedStatement接口是Statement的子接口。 它主要用于参数化查询。 将为值传递一个问号(?)。 问号的值将由PreparedStatement设置。 该接口的方法在下面给出。
S.No. | Method | Description |
---|---|---|
1 | public void setInt(intparamIndex, int value) | It is used for setting the integer value for the given parameter index. |
2 | public void setString(intparamIndex, String value) | It is used for setting the String value for the given parameter index. |
3 | public void setFloat(intparamIndex, float value) | It is used for setting the Float value for the given parameter index. |
4 | public void setDouble(intparamIndex, double value) | It is used for setting the Double value for the given parameter index. |
5 | public intexecuteUpdate() | It is used for executing a query. |
6 | public ResultSetexecuteQuery() | It is used for executing the select query. |
序号 | 方法 | 描述 |
---|---|---|
1个 | 公共无效setInt(intparamIndex,int value) | 它用于设置给定参数索引的整数值。 |
2 | 公共无效setString(intparamIndex,字符串值) | 它用于设置给定参数索引的字符串值。 |
3 | 公共无效setFloat(intparamIndex,浮点值) | 它用于设置给定参数索引的浮点值。 |
4 | 公共无效setDouble(intparamIndex,双精度值) | 它用于设置给定参数索引的Double值。 |
5 | 公共intexecuteUpdate() | 它用于执行查询。 |
6 | 公共ResultSetexecuteQuery() | 它用于执行选择查询。 |
ResultSetMetaData接口 (ResultSetMetaData Interface)
The ResultSetMetaData interface is used to get metadata from the ResultSet object. Metadata are the data about data. Methods of this interface is given below.
ResultSetMetaData接口用于从ResultSet对象获取元数据。 元数据是关于数据的数据。 该接口的方法在下面给出。
S.No. | Method | Description |
---|---|---|
1 | public intgetColumnCount()throws SQLException | It is used to get the total number of columns. |
2 | public String getColumnName(int index)throws SQLException | It is used to get the name of the column of a specified column index. |
3 | public StringgetColumnTypeName(int index)throws SQLException | It is used to get the name of the column of a specified index. |
4 | public StringgetTableName(int index)throws SQLException | It is used to get the name of a table from the specified column index |
序号 | 方法 | 描述 |
---|---|---|
1个 | 公共intgetColumnCount()引发SQLException | 它用于获取列总数。 |
2 | 公共字符串getColumnName(int索引)引发SQLException | 它用于获取指定列索引的列名。 |
3 | 公共StringgetColumnTypeName(int index)引发SQLException | 它用于获取指定索引的列的名称。 |
4 | 公共StringgetTableName(int索引)引发SQLException | 它用于从指定的列索引获取表的名称 |
翻译自: https://www.studytonight.com/java/introduction-to-jdbc.php
jdbc简介