结果集处理器
装饰类的使用:
wrapper是一个接口类:
The wrapper pattern is employed by many JDBC driver
多被应用于JDBC驱动器的加载
The wrapper pattern is employed by many JDBC driver implementations to provide extensions beyond the traditional JDBC API that are specific to a data source. Developers may wish to gain access to these resources that are wrapped (the delegates) as proxy class instances representing the the actual resources. This interface describes a standard mechanism to access these wrapped resources represented by their proxy, to permit direct access to the resource delegates.
接口和接口之间通过继承来增强父类的接口,而不需重写父类的方法
就如同
ResultSetMetaData继承Wrapper一样
ResultSetMetaData接口:
An object that can be used to get information about the types and properties of the columns in a ResultSet object. The following code fragment creates the ResultSet object rs, creates the ResultSetMetaData object rsmd, and uses rsmd to find out how many columns rs has and whether the first column in rs can be used in a WHERE clause.
BeanInfo接口:
用来处理javaBean对象的属性和参数
Use the BeanInfo interface to create a BeanInfo class and provide explicit information about the methods, properties, events, and other features of your beans.
Introspector类:
是一个工具类用来获取一个javaBean对象的具体属性和参数
The Introspector class provides a standard way for tools to learn about the properties, events, and methods supported by a target Java Bean.
结果处理器的实现:
步骤一:创建结果集处理器接口
package JDBCTemp ;
import java. sql. ResultSet ;
public interface ResultHandler < T > {
T Handler ( ResultSet rs) ;
}
步骤二:实现结果集处理器;(用于javaBean对象的封装)
方式一:集合数据的封装
package JDBCTemp. Imp ;
import JDBCTemp . ResultHandler ;
import java. beans. * ;
import java. lang. reflect. InvocationTargetException ;
import java. lang. reflect. Method ;
import java. sql. ResultSet ;
import java. sql. ResultSetMetaData ;
import java. sql. SQLException ;
import java. util. ArrayList ;
import java. util. List ;
public class ResultHandlerImpl < T > implements ResultHandler {
Class cl;
public ResultHandlerImpl ( Class cl) {
this . cl = cl;
}
@Override
public T Handler ( ResultSet rs) {
List < T > list = new ArrayList < T > ( ) ;
try {
ResultSetMetaData resultSetMetaData = rs. getMetaData ( ) ;
int columnCount = resultSetMetaData. getColumnCount ( ) ;
BeanInfo beanInfo = Introspector . getBeanInfo ( cl) ;
PropertyDescriptor [ ] propertyDescriptors = beanInfo. getPropertyDescriptors ( ) ;
while ( rs. next ( ) ) {
Object bean = cl. newInstance ( ) ;
for ( int i = 1 ; i <= columnCount; i++ ) {
String columnName = resultSetMetaData. getColumnName ( i) ;
String columnClassNames = resultSetMetaData. getColumnClassName ( i) ;
String [ ] cmts = columnClassNames. split ( "\\." ) ;
String columnClassName = cmts[ 2 ] ;
String cmtss = columnClassNames. substring ( columnClassNames. lastIndexOf ( "." ) + 1 ) ;
for ( PropertyDescriptor propertyDescriptor : propertyDescriptors) {
if ( propertyDescriptor. getPropertyType ( ) . getSimpleName ( ) . equals ( cmtss) && propertyDescriptor. getName ( ) . equals ( columnName) ) {
Object rsMetaData = rs. getObject ( columnName) ;
Method writeMethod = propertyDescriptor. getWriteMethod ( ) ;
writeMethod. invoke ( bean, rsMetaData) ;
}
}
}
list. add ( ( T ) bean) ;
}
} catch ( SQLException e) {
throw new RuntimeException ( e) ;
} catch ( IntrospectionException e) {
throw new RuntimeException ( e) ;
} catch ( InstantiationException e) {
throw new RuntimeException ( e) ;
} catch ( IllegalAccessException e) {
throw new RuntimeException ( e) ;
} catch ( InvocationTargetException e) {
throw new RuntimeException ( e) ;
}
return ( T ) list;
}
}
方式二:一个参数的封装
public class HandlerOne < T > implements ResultHandler {
Class cl;
public HandlerOne ( Class cl) {
this . cl = cl;
}
public T Handler ( ResultSet rs) {
Object bean = null ;
try {
ResultSetMetaData metaData = rs. getMetaData ( ) ;
int columnCount = metaData. getColumnCount ( ) ;
BeanInfo beanInfo = Introspector . getBeanInfo ( cl) ;
PropertyDescriptor [ ] pds = beanInfo. getPropertyDescriptors ( ) ;
while ( rs. next ( ) ) {
bean = cl. newInstance ( ) ;
for ( int i = 1 ; i <= columnCount; i++ ) {
String columnName = metaData. getColumnName ( i) ;
String columnClassName = metaData. getColumnClassName ( i) ;
String [ ] split = columnClassName. split ( "\\." ) ;
String name = split[ 2 ] ;
for ( PropertyDescriptor pd : pds) {
if ( pd. getName ( ) . equals ( columnName) && pd. getPropertyType ( ) . getSimpleName ( ) . equals ( name) ) {
Object data = rs. getObject ( columnName) ;
Method writeMethod = pd. getWriteMethod ( ) ;
writeMethod. invoke ( bean, data) ;
}
}
}
}
} catch ( SQLException e) {
e. printStackTrace ( ) ;
} catch ( IntrospectionException e) {
e. printStackTrace ( ) ;
} catch ( InstantiationException e) {
e. printStackTrace ( ) ;
} catch ( IllegalAccessException e) {
e. printStackTrace ( ) ;
} catch ( InvocationTargetException e) {
e. printStackTrace ( ) ;
}
return ( T ) bean;
}
}
步骤三:sql语句执行工具,(进行sql语句的参数封装)
package JDBCTemp ;
import JDBCTemp . Utils . JDBCUtils ;
import java. sql. Connection ;
import java. sql. PreparedStatement ;
import java. sql. ResultSet ;
import java. sql. SQLException ;
import java. util. ArrayList ;
import java. util. List ;
public class Temp < T > {
Connection conn = null ;
PreparedStatement ps = null ;
ResultSet rs = null ;
public T selectOne ( String sql, ResultHandler handler, Object . . . parames) {
Object bean = null ;
try {
conn = JDBCUtils . getCon ( ) ;
ps = conn. prepareStatement ( sql) ;
int parameterCount = ps. getParameterMetaData ( ) . getParameterCount ( ) ;
if ( parameterCount != parames. length) {
throw new RuntimeException ( "参数不匹配" ) ;
}
for ( int i = 1 ; i <= parameterCount; i++ ) {
ps. setObject ( i, parames[ i - 1 ] ) ;
}
rs = ps. executeQuery ( ) ;
bean = handler. Handler( rs) ;
} catch ( SQLException e) {
e. printStackTrace ( ) ;
}
return ( T ) bean;
}
public T selectList ( String sql, ResultHandler handler, Object . . . parames) {
List < T > list = new ArrayList < > ( ) ;
try {
conn = JDBCUtils . getCon ( ) ;
ps = conn. prepareStatement ( sql) ;
int parameterCount = ps. getParameterMetaData ( ) . getParameterCount ( ) ;
if ( parameterCount != parames. length) {
throw new RuntimeException ( "参数不匹配" ) ;
}
for ( int i = 1 ; i <= parameterCount; i++ ) {
ps. setObject ( i, parames[ i - 1 ] ) ;
}
rs = ps. executeQuery ( ) ;
list = ( List < T > ) handler. Handler( rs) ;
} catch ( SQLException e) {
e. printStackTrace ( ) ;
}
return ( T ) list;
}
public int updates ( String sql, Object . . . parames) {
int count = 0 ;
try {
conn = JDBCUtils . getCon ( ) ;
ps = conn. prepareStatement ( sql) ;
int parameterCount = ps. getParameterMetaData ( ) . getParameterCount ( ) ;
if ( parameterCount != parames. length) {
throw new RuntimeException ( "参数不匹配" ) ;
}
for ( int i = 1 ; i <= parameterCount; i++ ) {
ps. setObject ( i, parames[ i - 1 ] ) ;
}
count = ps. executeUpdate ( ) ;
} catch ( SQLException e) {
e. printStackTrace ( ) ;
}
return count;
}
}
步骤四:进行sql语句的定义返回封装的JavaBean对象
package JDBCTemp ;
import JDBCTemp . Imp . HandlerOne ;
import JDBCTemp . entity. Student;
public class Test {
public static void main ( String [ ] args) {
String sql = "select * from user_table where user_id = ?" ;
Temp < Student > temp = new Temp < > ( ) ;
Student student = temp. selectOne ( sql, new HandlerOne ( Student . class ) , 1 ) ;
System . out. println ( student) ;
}
}
package JDBCTemp ;
import JDBCTemp . Imp . ResultHandlerImpl ;
import JDBCTemp . entity. Student;
import java. util. List ;
public class Test2 {
public static void main ( String [ ] args) {
int i = 1 ;
String sql = "select * from user_table " ;
Temp < Student > temp = new Temp < > ( ) ;
List < Student > students = ( List < Student > ) temp. selectList ( sql, new ResultHandlerImpl ( Student . class ) ) ;
for ( Student student : students) {
System . out. println ( i++ ) ;
System . out. println ( student) ;
}
}
}