对JDBC的封装
连接的公共方法
package com.zhj.dao.common;
import java.lang.reflect.Method;
import java.math.BigDecimal;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.util.ArrayList;
import java.util.List;
import com.zhj.util.CommonValue;
import com.zhj.util.PropertyUtil;
/**
* 公用类
* @author admin
*
*/
public class CommonDao {
private static PropertyUtil pu=new PropertyUtil(CommonValue.propertyFileName);
private static PropertyUtil puu=new PropertyUtil(pu.getProperty("dbtype")+".properties");
private static String driverClass=puu.getProperty("driverClass");
private static String url=puu.getProperty("url");
private static String name=puu.getProperty("name");
private static String password=puu.getProperty("password");
private static ThreadLocal<Connection> th;
public static Connection getConnection()throws Exception{
Connection con=th.get();
if (con == null) {
Class.forName(driverClass);
con=DriverManager.getConnection(url,name,password);
th.set(con);
}
return con;
}
public static void closeAll(ResultSet rs,PreparedStatement pstmt,Connection con)throws Exception{
if(rs!=null){
rs.close();
}
if(pstmt!=null){
pstmt.close();
}
if(con!=null){
con.close();
}
}
//通用更新
public static int executeUpdate(String sql,Object...params)throws Exception{
int rowAffect=0;
Connection con=getConnection();
PreparedStatement pstmt=con.prepareStatement(sql);
if(params!=null){
for(int i=0;i<params.length;i++){
pstmt.setObject((i+1), params[i]);
}
}
rowAffect=pstmt.executeUpdate();
closeAll(null,pstmt,con);
return rowAffect;
}
// 自己设置连接,关闭,可以便于事物 设置关闭自动提交 -----改进方案 通过线程 共享变量可以直接拿到connection
public static int executeUpdate(Connection con,String sql,Object...params)throws Exception{
int rowAffect=0;
PreparedStatement pstmt=con.prepareStatement(sql);
if(params!=null){
for(int i=0;i<params.length;i++){
pstmt.setObject((i+1), params[i]);
}
}
rowAffect=pstmt.executeUpdate();
closeAll(null,pstmt,null);
return rowAffect;
}
//通用查询的策略版
public static <T> List<T> executeQuery_strategy(RowMapper<T> rm,String sql,Object...params)throws Exception{
List<T> list=new ArrayList<T>();
Connection con=getConnection();
PreparedStatement pstmt=con.prepareStatement(sql);
if(params!=null){
for(int i=0;i<params.length;i++){
pstmt.setObject((i+1), params[i]);
}
}
ResultSet rs=pstmt.executeQuery();
while(rs.next()){
T t=rm.mapRow(rs);
list.add(t);
}
closeAll(rs,pstmt,con);
return list;
}
//通用查询反射版本
public static <T> List<T> executeQuery_reflect(Class<T> clazz,String sql,Object...params)throws Exception{
List<T> list=new ArrayList<T>();
Connection con=getConnection();
PreparedStatement pstmt=con.prepareStatement(sql);
if(params!=null){
for(int i=0;i<params.length;i++){
pstmt.setObject((i+1), params[i]);
}
}
ResultSet rs=pstmt.executeQuery();
//获取结果集的元数据
ResultSetMetaData rsmd=rs.getMetaData();
//放置结果集的列名字
List<String> columnNames=new ArrayList<String>();
for(int i=0;i<rsmd.getColumnCount();i++){
String columnName=rsmd.getColumnLabel((i+1));
columnNames.add(columnName);
}
while(rs.next()){
T t=clazz.newInstance();
for(String columnName : columnNames){
String setterMethodName="set"+columnName.substring(0, 1).toUpperCase()+columnName.substring(1);
Method[] methods=clazz.getDeclaredMethods();
for(Method method: methods){
if(setterMethodName.equals(method.getName())){
Object value=rs.getObject(columnName);
//将聚合函数返回结果Long、oralce的number类型的数据进入结果集编程BigDecimal 转成 int (如果需要高精度类型余姚去掉该处理)
//处理有关于聚合函数返回结果是Long
if(value instanceof Long){
Long l=(Long)value;
value=l.intValue();
}
//处理oralce的number类型的数据进入结果集编程BigDecimal
if(value instanceof BigDecimal){
BigDecimal bd=(BigDecimal)value;
value=bd.intValue();
}
method.invoke(t, value);
}
}
}
list.add(t);
}
closeAll(rs,pstmt,con);
return list;
}
}
策略实现
package com.zhj.dao.common;
import java.sql.ResultSet;
/**
* 不同的策略实现,就有不同的处理方式
* @author admin
*
* @param <T>
*/
public interface RowMapper<T> {
public T mapRow(ResultSet rs)throws Exception;
}
设置公共值
package com.zhj.util;
public class CommonValue {
public static String encoding="UTF-8";
public static String propertyFileName="db.properties";
}
获取配置文件
package com.zhj.util;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
public class PropertyUtil {
private Properties p=new Properties();
public PropertyUtil(String fileName){
try {
InputStream is=PropertyUtil.class.getClassLoader().getResourceAsStream(fileName);
p.load(is);//把属性文件的内容加载到p对象中
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
//从p对象中获取key所对应的值
public String getProperty(String key){
return p.getProperty(key);
}
}