zoukankan      html  css  js  c++  java
  • 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(); } } }
  • 相关阅读:
    [C#][Newtonsoft.Json] Newtonsoft.Json 序列化时的一些其它用法
    [C#] 获取计算机内部信息
    [Bug] 解决 Sql Server 数据库死锁问题
    Visual Studio提示“无法启动IIS Express Web服务器”的解决方法
    [svn] TortoiseSVN 图文操作
    [js] 如何 在 jQuery 中的 $.each 循环中使用 break 和 continue
    Redis 小白指南(三)- 事务、过期、消息通知、管道和优化内存空间
    Redis 小白指南(二)- 聊聊五大类型:字符串、散列、列表、集合和有序集合
    Redis 小白指南(一)- 简介、安装、GUI 和 C# 驱动介绍
    Redis 小白指南(四)- 数据的持久化保存(草稿)
  • 原文地址:https://www.cnblogs.com/TankRuning/p/3959333.html
Copyright © 2011-2022 走看看