首页 > 代码库 > JDBC高级部分

JDBC高级部分

/**
  采用模版类型,封装了基本数据的CRUD操作
  基本属性从外部属性文件读取(如
config.properties
*/
public
class BaseDao<T>{ private InputStream in=this.getClass().getResourceAsStream("/com/tank/comm/config.properties"); private Properties properties=new Properties(); Connection conn=null; PreparedStatement pstat=null;; ResultSet res=null; /*public static void main(String[] args) { System.out.println("----------"); new BaseDao().getConn(); System.out.println("----------"); }*/ public Connection getConn() { try { properties.load(in); //System.out.println("加载文件路径"); Class.forName(properties.getProperty("driver")); conn=DriverManager.getConnection(properties.getProperty("url"), properties.getProperty("username"), properties.getProperty("password")); System.out.println("数据库连接成功!"); return conn; } catch(Exception ex) { ex.printStackTrace(); } return null; } /* * 封装方法,用来执行insert,update,delete 功能 ***/ public int executeUpdate(String sql,Object[]param) { int rows=0; try { this.getConn(); pstat=conn.prepareStatement(sql); if(param!=null&&param.length!=0) { for(int x=0;x<param.length;x++) { pstat.setObject((x+1), param[x]); } } rows=pstat.executeUpdate(); } catch(Exception ex) { ex.printStackTrace(); } finally { this.closeAll(conn, pstat, res); } return rows; } /** *封装执行select语句 * */ public List<T> executeQuery(String sql,Object[]param,Class<T> cls) { List<T> list = new ArrayList<T>(); this.getConn(); try { pstat=conn.prepareStatement(sql); if(param!=null&&param.length!=0) { for(int x=0;x<param.length;x++) { pstat.setObject((x+1), param[x]); } } res=pstat.executeQuery(); while(res.next()) { //1 创建对象 T obj =cls.newInstance(); //对象属性赋值 Field []fs=cls.getDeclaredFields(); for(int x=0;x<fs.length;x++) { Field f=fs[x]; f.setAccessible(true); //属性的类型 String ct = f.getType().getName(); String name=f.getName(); if(ct.equals("java.lang.String")) { f.set(obj, res.getString(name)); } if(ct.equals("int")) { f.set(obj, res.getInt(name)); } if(ct.equals("double")) { f.set(obj, res.getDouble(name)); } if(ct.equals("java.sql.Date")) { f.set(obj, res.getDate(name)); } if(ct.equals("char")) { f.set(obj, res.getString(name)); } } list.add(obj); } System.out.println("列表长度:"+list.size()); return list; } catch(Exception ex) { ex.printStackTrace(); } finally { this.closeAll(conn, pstat, res); } return null; } /** * 重载executeQuery方法 * 增加实体类的指定属性赋值 *
  * @param sql 传入的sql语句
   @param param 传入的参数数组
   @param cls 传入的映射类(用由<T> 指定,避免出现类型转化等警告信息)
   @param   propertyList 属性集合,可以只为为映射类的指定属性赋值 *
*/ public List<T> executeQuery(String sql,Object[]param,Class<T> cls,List<String> propertyList){ List<T> list =new ArrayList<T>(); this.getConn(); try{ pstat=conn.prepareStatement(sql); if(param!=null&&param.length!=0) { for(int x=0;x<param.length;x++) { pstat.setObject((x+1), param[x]); } } res=pstat.executeQuery(); //如果参数集合为空,返回上层默认实体类所有属性赋值 if(propertyList.isEmpty()){ this.executeQuery(sql, param, cls); }else{ //不为空,则为集合内指定属性赋值 //取出实体类的所有属性 while(res.next()){ //创建实例对象 T obj=cls.newInstance(); //获取所有反射属性 Field fields[]=cls.getDeclaredFields(); for(int x=0;x<fields.length;x++) { Field f=fields[x]; f.setAccessible(true); String fname=f.getName(); //判断集合是否包含该属性名,如果包含,为其赋值 if(propertyList.contains(fname)){ String ct=f.getType().getName(); if(ct.equals("java.lang.String")) { f.set(obj, res.getString((fname))); } if(ct.equals("int")) { f.set(obj, res.getInt((fname))); } if(ct.equals("double")) { f.set(obj, res.getDouble((fname))); } if(ct.equals("java.sql.Date")) { f.set(obj, res.getDate((fname))); } if(ct.equals("char")) { f.set(obj, res.getString((fname))); } } } //将对象添加到集合 list.add(obj); } } }catch(Exception e){ e.printStackTrace(); }finally{ this.closeAll(conn, pstat, res); } return list; } //关闭连接,释放资源 public void closeAll(Connection conn,PreparedStatement pstat,ResultSet res) { try { if(res!=null) { res.close(); } if(pstat!=null) { pstat.close(); } if(conn!=null) { conn.close(); } } catch(Exception ex) { ex.printStackTrace(); } } }

 

JDBC高级部分