📜  Java7 JDBC改进

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

Java 7 JDBC改进

JDBC(Java数据库连接)提供了Java编程语言的通用数据访问。您可以使用JDBC访问数据库,电子表格或平面文件中的任何数据。

在Java 7中,Java引入了以下功能:

1)它提供了使用try-with-resources语句自动关闭Connection,ResultSet和Statement类型的资源的功能。

2)RowSet 1.1:引入RowSetFactory接口和RowSetProvider类,使您可以创建JDBC驱动程序支持的所有类型的行集。

RowSetFactory接口

它定义了工厂的实现,该工厂用于获取不同类型的RowSet。

RowSetFactory接口方法

Methods Description
CachedRowSet createCachedRowSet()
throws SQLException
It creates a new instance of a FilteredRowSet.
It throws SQLException, if a CachedRowSet cannot be created.
FilteredRowSet createFilteredRowSet()
throws SQLException
It creates a new instance of a FilteredRowSet.
It throws SQLException, if a FilteredRowSet cannot be created.
JdbcRowSet createJdbcRowSet()
throws SQLException
It creates a new instance of a JdbcRowSet.
It throws SQLException, if a JdbcRowSet cannot be created.
JoinRowSet createJoinRowSet()
throws SQLException
It creates a new instance of a JoinRowSet.
It throws SQLException, if a JoinRowSet cannot be created.
WebRowSet createWebRowSet()
throws SQLException
It creates a new instance of a WebRowSet.
It throws SQLException, if a WebRowSet cannot be created.

Java RowSetProvider类

它是一种工厂API,可帮助应用程序获取RowSetFactory实现,该实现可用于创建不同类型的RowSet。

Methods Description
public static RowSetFactory newFactory()
throws SQLException
It creates a new instance of a RowSetFactory implementation.
It throws SQLException, if the default factory class cannot be loaded or instantiated.
public static RowSetFactory newFactory(String factoryClassName,
ClassLoader cl)
throws SQLException
It creates a new instance of a RowSetFactory from the specified factory class name. This function is useful when there are multiple providers in the classpath. It gives more control to the application as it can specify which provider should be loaded.
It throws SQLException, if factoryClassName is null, or the factory class cannot be loaded.

JDBC示例:使用Try-With-Resources的Mysql连接

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;  
class JdbcExample{  
public static void main(String args[]){
try(// --------------try-with-resources begin-------------//
// Creating connection
Connection con = DriverManager.getConnection( "jdbc:mysql://localhost:3306/student","root","mysql");
// Creating statement
Statement stmt=con.createStatement();
// Executing Sql query
ResultSet rs=stmt.executeQuery("select * from user");
   )// --------------try-with-resources end--------------//
{ // ----------------try block begin---------------------//
// Iterating ResultSet elements
while(rs.next()){  
System.out.println(rs.getInt(1)+""+rs.getString(2)+""+rs.getString(3));  
}
} // ----------------try block end----------------------//
catch(Exception e){ // Exception handler 
System.out.println(e.getMessage());
}  
}
}

在上面的示例中,我们使用了try-with-resources。完成try块后,它用于关闭资源。现在,您无需显式关闭数据库连接。

确保使用的是JDBC 4.0或更高版本以及Java 1.6或更高版本。

行集1.1

在Java的早期版本中,您已经使用JdbcRowSetImpl类创建了JdbcRowSet,CachedRowSet,FilteredRowSet等的实例。

现在,Java 7添加了新的RowSet 1.1。因此,您可以使用RowSetFactory接口创建JdbcRowSet的实例。

Java CachedRowSet

它将数据存储(缓存)到内存中,以便可以对自己的数据而不是存储在数据库中的数据执行操作。它可以在不连接其数据源的情况下运行,这也就是为什么它也被称为disconnectedRowSet。

Java JDBC示例:CachedRowSet

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import javax.sql.rowset.CachedRowSet;
import javax.sql.rowset.RowSetProvider;  
class JdbcExample{  
public static void main(String args[]) throws Exception{
try(// --------------try-with-resources begin-------------//
// Creating connection
Connection con = DriverManager.getConnection( "jdbc:mysql://localhost:3306/student","root","mysql");
// Creating statement
Statement stmt=con.createStatement();
// Executing query
ResultSet rs=stmt.executeQuery("select * from user");
   )// --------------try-with-resources end--------------//
{ // ----------------try block begin---------------------//
// Creating CachedRowSet
CachedRowSet cRS = RowSetProvider.newFactory().createCachedRowSet();
// Populating ResultSet data into CachedRowSet
cRS.populate(rs);
while(cRS.next()){  
System.out.println(cRS.getInt(1)+""+cRS.getString(2)+""+cRS.getString(3));  
}
} // ----------------try block end----------------------//
catch(Exception e){ // Exception handler 
System.out.println(e);
}  
}
}

Java JdbcRowSet

它是一个改良的ResultSet对象,用于维护与数据源的连接。它与ResultSet相似,但是最大的不同是它提供了一组属性和侦听器,就像JavaBeans。JdbcRowSet的主要目的是使ResultSet可滚动和可更新。

在下面的示例中,我们正在使用新方法创建JdbcRowSet的实例。

Java JdbcRowSet示例1

import javax.sql.rowset.JdbcRowSet;
import javax.sql.rowset.RowSetProvider;  
class JdbcExample{  
public static void main(String args[]) throws Exception{
try(// --------------try-with-resources begin-------------//
// Creating connection
JdbcRowSet jRS = RowSetProvider.newFactory().createJdbcRowSet();
   )// --------------try-with-resources end--------------//
{ // ----------------try block begin---------------------//
// Set database connection
jRS.setUrl("jdbc:mysql://localhost:3306/student");
// Set database username
jRS.setUsername("root");
// Set database password
jRS.setPassword("mysql");
// Set sql query to execute 
jRS.setCommand("select * from user");
// Execute query
jRS.execute();
while(jRS.next()){  
System.out.println(jRS.getInt(1)+""+jRS.getString(2)+""+jRS.getString(3));  
}
} // ----------------try block end----------------------//
catch(Exception e){ // Exception handler 
System.out.println(e);
}  
}
}

Java JdbcRowSet示例:更新行

import javax.sql.rowset.JdbcRowSet;
import javax.sql.rowset.RowSetProvider;  
class JdbcExample{  
public staticvoid main(String args[]) throws Exception{
try(// --------------try-with-resources begin-------------//
// Creating connection
JdbcRowSet jRS = RowSetProvider.newFactory().createJdbcRowSet();
   )// --------------try-with-resources end--------------//
{ // ----------------try block begin---------------------//
// Set database connection
jRS.setUrl("jdbc:mysql://localhost:3306/student");
// Set database username
jRS.setUsername("root");
// Set database password
jRS.setPassword("mysql");
// Set sql query to execute 
jRS.setCommand("select * from user");
// Execute query
jRS.execute();
// Getting 3rd row because it is scrollable by default
jRS.absolute(3);  
System.out.println(jRS.getInt(1)+""+jRS.getString(2)+""+jRS.getString(3));  
// Updating 3rd row
jRS.updateString("name", "Neraj Kumar Singh");
jRS.updateRow();
// Fetching 3rd row again
System.out.println(jRS.getInt(1)+""+jRS.getString(2)+""+jRS.getString(3));
} // ----------------try block end----------------------//
catch(Exception e){ // Exception handler 
System.out.println(e);
}  
}
}

输出:

3  Neraj kumar  8562697858
3  Neraj Kumar Singh  8562697858