1_基础版JDBC-DBHelper扩展(事务及大数据操作)
2015-06-03 12:47
253 查看
JDBC - 基础版DBHelper
/MyProperties.java/
public class MyProperties extends Properties{
private static MyProperties myproperties;
private MyProperties(){
InputStream iis = MyProperties.class.getClassLoader().getResourceAsStream(“db.properties”);
try{
super.load( iis );
}catch( IOException e ){
e.printStackTrace();
}
}
public static MyProperties getInstance(){ if( myproperties == null ){ myproperties = new MyProperties(); } return myproperties; }
}
/[b]****************************************************************[/b]/
/DBHelper.java/
public class DBHelper{
// 静态块 , 读取一次数据库驱动, static{ try{ Class.forName( MyProperties.getInstance().getProperty("driver") ); }catch( ClassNotFoundException e ){ e.printStackTrace(); } } //获取数据库联接 public Connection getCon() throws SQLException { MyProperties mp = MyProperties.getInstance(); Connection con = DriverManager.getConnection( mp.getProperty("url") , mp.getProperty ("username") , mp.getProperty("password") ); return con; } //查询 public List<Map<String , String >> findAll( String sql ,List<Object> params )throws SQLException{ Connection con = getCon(); PreparedStatement pstmt = con.prepareStatement( sql ); setParams( pstmt , params ); ResultSet rs = pstmt.executeQuery(); ResultSetMetaData rsmd = rs.getMetaData(); //获取关于 ResultSet 对象中列的类型和属性信息的对象 List<String> cnlist = new ArrayList<String>(); for( int i = 1; i <= rsmd.getColumnCount() ; i++ ){ cnlist.add( rsmd.getColumnName( i ) ); } List<Map<String , String >> list = new ArrayList<Map<String ,String >>(); while( rs.next() ){ Map<String , String > map = new HashMap<String , String >(); for( String cn :cnlist ){ map.put( cn , rs.getString(cn) ); } list.add( map ); } closeAll(pstmt ,con ,rs ); return list; } //查询聚合函数 public double findDouble( String sql , List<Object> params ) throws SQLException{ double result = 0; Connection con = getCon(); PreparedStatement pstmt = con.prepareStatement( sql ); setParams( pstmt ,params ); ResultSet rs = pstmt.executeQuery(); while( rs.next() ){ rs.getDouble( 1 ); } closeAll( pstmt ,con ,rs ); return result; } //封装增删改 public int doUpdate( String sql , List<Object> params ) throws SQLException{ Connection con = getCon(); PreparedStatement pstmt = con.prepareStatement( sql ); setParams( pstmt ,params ); int result = pstmt.executeUpdate(); closeAll( pstmt ,con ,null ); return result; } //封装带事务的增删改 public void doUpdateTran( List<String> sqls ,List< List<Object> > listparams ){ Connection con = getCon(); PreparedStatement pstmt = null; con.setAutoCommit( false ); //关闭隐式事务( 一句SQL语句提交一次 ) try{ if( sqls != null && sqls.size() > 0 ){ for( int i = 0; i < sqls.size() ; i++ ){ String sql = sqls.get( i ); // 取出每一条sql语句. pstmt = con.prepareStatement( sql ); setParams( pstmt , listparams( i ) ); pstmt.executeUpdate(); } } con.commit(); }catch( Exception e ){ if( con != null ){ con.rollback; // 回滚 } }finally{ con.setAutoCommit( true ); //恢复隐式事务 closeAll( pstmt ,con ,null ); } } //大数据的操作 , 批处理 // 集合嵌套集合. public void doBigDataUpdate( String sql , List<List<Object>> params ) throws SQLException{ Connection con = getCon(); con.setAutoCommit( false ); PreparedStatement pstmt = null; try{ pstmt = con.prepareStatement( sql ); for( int i = 0; i < params.size() ; i++ ){ List<Object> paramsList = params.get( i ); setParams( pstmt , paramsList ); pstmt.addBatch(); //添加一次预定义参数 if( i % 1000 == 0 ){ pstmt.executeBatch(); //批量执行预定义sql ,这个返回值里面存的是每条语句执行的结果 } } pstmt.executeBatch(); // 多余的余数在执行. con.commit(); }catch( Exception e){ e.printStackTrace(); con.rollback(); // 异常 ====>>>> 回滚。 }finally{ con.setAutoCommit( true ); closeAll( pstmt ,con ,null ); } } //设置参数 占位符 private void setParams( PreparedStatement pstmt , List<Object> params ) throws SQLException{ if( params != null && params.size() > 0 ){ for( int i = 1; i <= params.size() ; i++ ){ pstmt.setObject( i , params.get( i-1 )); } } } // 释放资源 private sttaic void clossAll( PreparedStatement pstmt , Connection con , ResultSet rs ){ if( pstmt != null ){ pstmt.close(); } if( con !=null ){ con.close(); } if( rs != null ){ rs.close(); } }
}
相关文章推荐
- jdbc中的Statement和PreparedStatement接口对象
- SQL Server误区30日谈 第1天 正在运行的事务在服务器故障转移后继续执行
- 浅析SQL Server中包含事务的存储过程
- Mysql中的事务是什么如何使用
- MySql的事务使用与示例详解
- C#分布式事务的超时处理实例分析
- SQL Server的事务操作隔离模式介绍
- JDBC 数据库常用连接 链接字符串
- oracle 合并查询 事务 sql函数小知识学习
- JDBC连接Access数据库的几种方式介绍
- sql不常用函数总结以及事务,增加,删除触发器
- mysql的XA事务恢复过程详解
- 在Mysql存储过程中使用事务实例
- mysql存储过程事务管理简析
- php+mysql事务rollback&commit示例
- JDBC 程序的常见错误及调试方法
- PHP中的事务使用实例
- Java加载JDBC驱动程序实例详解
- PHP使用Mysql事务实例解析
- MySql事务无法回滚的原因有哪些