📜  Java8 JDBC改进

📅  最后修改于: 2020-10-13 05:25:59             🧑  作者: Mango

Java 8 JDBC改进

在Java 8中,Java在JDBC API中进行了两项重大更改。

1)JDBC-ODBC桥已被删除。

Oracle不支持JDBC-ODBC桥。 Oracle建议您使用数据库供应商提供的JDBC驱动程序,而不要使用JDBC-ODBC Bridge。

2)在JDBC 4.2中添加了一些新功能。

Java JDBC 4.2引入了以下功能:

  • 增加了REF_CURSOR支持。
  • 添加java.sql.DriverAction接口
  • 在DriverManager类中的deregisterDriver方法上添加安全检查
  • java.sql.SQLType接口的添加
  • java.sql.JDBCType枚举的添加
  • 添加对大量更新的支持
  • 更改现有接口
  • 行集1.2:列出JDBC行集的增强功能。

Java JDBC驱动程序操作

当要通过DriverManager通知驱动程序时,必须实现此接口。它添加在java.sql包中,仅包含一个抽象方法。

DriverAction方法

Method Description
void deregister() This method called by DriverManager.deregisterDriver(Driver) to notify the JDBC driver that it was de-registered.

取消注册方法仅供JDBC驱动程序使用,而不能由应用程序使用。

建议不要使用JDBC驱动程序在公共类中实现DriverAction。

如果在调用注销方法时存在到数据库的活动连接,则具体取决于连接是关闭还是允许继续。调用此方法后,驱动程序是否会限制创建与数据库的新连接,调用其他Driver方法或引发SQLException的能力,这取决于实现。

Java JDBC4.2 DriverAction示例

import java.sql.*;  
// implementing DriverAction interface
class JdbcExample implements DriverAction{  
// implementing deregister method of DriverAction interface
@Override
public void deregister() {
System.out.println("Driver deregistered");
}
public static void main(String args[]){
try{
// Creating driver instance
Driver driver = new com.mysql.jdbc.Driver();
// Creating Action Driver
DriverAction da = new JdbcExample();
// Registering driver by passing driver and driverAction
DriverManager.registerDriver(driver, da);
// Creating connection
Connection con=DriverManager.getConnection("jdbc:mysql://localhost:3306/student","root","mysql");
//Here student is database name, root is username and password is mysql
Statement stmt=con.createStatement(); 
// Executing SQL query
ResultSet rs=stmt.executeQuery("select * from user");  
while(rs.next()){  
System.out.println(rs.getInt(1)+""+rs.getString(2)+""+rs.getString(3));  
}
// Closing connection
con.close();  
// Calling deregisterDriver method
DriverManager.deregisterDriver(driver);
}catch(Exception e){ System.out.println(e);}  
}  

}  

输出:

1  Arun  25
2  irfan  22
3  Neraj kumar  25
Driver deregistered

Java JDBC SQLType

该接口用于标识通用SQL类型,JDBC类型或特定于供应商的数据类型。

它提供以下方法。

Method Description
String getName() It returns the SQLType name that represents a SQL data type.
String getVendor() It returns the name of the vendor that supports this data type. The value returned typically is the package name for this vendor.
Integer getVendorTypeNumber() It returns the vendor specific type number for the data type.

Java JDBC类型

它是一个枚举,它定义用于标识通用SQL类型(称为JDBC类型)的常量。它扩展了java.lang.Enum并实现了java.sql.SQLType。

JDBCType字段

下表包含JDBCType中定义的常量。

Enum constant Description
public static final JDBCType ARRAY It identifies the generic SQL type ARRAY.
public static final JDBCType BIGINT It identifies the generic SQL type BIGINT.
public static final JDBCType BIT It identifies the generic SQL type BIT.
public static final JDBCType BLOB It identifies the generic SQL type BLOB.
public static final JDBCType BOOLEAN It identifies the generic SQL type BOOLEAN.
public static final JDBCType CHAR It identifies the generic SQL type CHAR.
public static final JDBCType CLOB It identifies the generic SQL type CLOB.
public static final JDBCType DATALINK It identifies the generic SQL type DATALINK.
public static final JDBCType DATE It identifies the generic SQL type DATE.
public static final JDBCType DECIMAL It identifies the generic SQL type DECIMAL.
public static final JDBCType DISTINCT It identifies the generic SQL type DISTINCT.
public static final JDBCType DOUBLE It identifies the generic SQL type DOUBLE.
public static final JDBCType FLOAT It identifies the generic SQL type FLOAT.
public static final JDBCType INTEGER It identifies the generic SQL type INTEGER.
public static final JDBCType JAVA_OBJECT It indicates that the SQL type is database-specific and gets mapped to a Java object that can be accessed via the methods getObject and setObject.
Public static final JDBCType LONGNVARCHAR It identifies the generic SQL type LONGNVARCHAR.
public static final JDBCType NCHAR It identifies the generic SQL type NCHAR.
public static final JDBCType NCLOB It identifies the generic SQL type NCLOB.
public static final JDBCType NULL It identifies the generic SQL value NULL.
public static final JDBCType NUMERIC It identifies the generic SQL type NUMERIC.
public static final JDBCType NVARCHAR It identifies the generic SQL type NVARCHAR.
public static final JDBCType OTHER It indicates that the SQL type is database-specific and gets mapped to a Java object that can be accessed via the methods getObject and setObject.
public static final JDBCType REAL It identifies the generic SQL type REAL.Identifies the generic SQL type VARCHAR.
public static final JDBCType REF It identifies the generic SQL type REF.
public static final JDBCType REF_CURSOR It identifies the generic SQL type REF_CURSOR.
public static final JDBCType ROWID It identifies the SQL type ROWID.
public static final JDBCType SMALLINT It identifies the generic SQL type SMALLINT.
public static final JDBCType SQLXML It identifies the generic SQL type SQLXML.
public static final JDBCType STRUCT It identifies the generic SQL type STRUCT.
public static final JDBCType TIME It identifies the generic SQL type TIME.
public static final JDBCType TIME_WITH_TIMEZONE It identifies the generic SQL type TIME_WITH_TIMEZONE.
public static final JDBCType TIMESTAMP It identifies the generic SQL type TIMESTAMP.
public static final JDBCType TIMESTAMP_WITH_TIMEZONE It identifies the generic SQL type TIMESTAMP_WITH_TIMEZONE.
public static final JDBCType TINYINT It identifies the generic SQL type TINYINT.
public static final JDBCType VARBINARY It identifies the generic SQL type VARBINARY.
public static final JDBCType VARCHAR It identifies the generic SQL type VARCHAR.

JDBCType方法

Method Description
public String getName() It returns the SQLType name that represents a SQL data type.
public String getVendor() It returns the name of the vendor that supports this data type.
public Integer getVendorTypeNumber() It returns the vendor specific type number for the data type.
public static JDBCType valueOf(int type) It returns the JDBCType that corresponds to the specified Types value.
It throws IllegalArgumentException, if this enum type has no constant with the specified Types value.
public static JDBCType valueOf(String name) It returns the enum constant of this type with the specified name. The string must match exactly an identifier used to declare an enum constant in this type.
It throws IllegalArgumentException, if this enum type has no constant with the specified name.
It throws NullPointerException, if the argument is null.
public static JDBCType[] values() It returns an array containing the constants of this enum type, in the order they are declared. This method may be used to iterate over the constants.