Java实现Dbhelper支持大数据增删改
在做项目的时候,技术选型很重要,在底层的方法直接影响了我们对大数据访问以及修改的速度,在Java中有很多优秀的ORM框架,比如说:JPA,Hibernate等等,正如我们所说的,框架有框架的好处,当然也存在一些可以改进的地方,这个时候,就需要我们针对于不同的业务不同的需求,不同的访问量,对底层的架构重新封装,来支持大数据增删改。
代码:
importjava.io.*; importjava.sql.*; importjava.util.*; importjava.util.logging.Level; importjava.util.logging.Logger; importjavax.servlet.jsp.jstl.sql.*; /** *DbHelper *@authorqmx * */ publicclassDbhelper{ privateStringsql;//要传入的sql语句 publicvoidsetSql(Stringsql){ this.sql=sql; } privateListsqlValues;//sql语句的参数 publicvoidsetSqlValues(ListsqlValues){ this.sqlValues=sqlValues; } privateList<List>sqlValue;//sql语句的参数 publicvoidsetSqlValue(List<List>sqlValues){ this.sqlValue=sqlValues; } privateConnectioncon;//连接对象 publicvoidsetCon(Connectioncon){ this.con=con; } publicDbhelper(){ this.con=getConnection();//给Connection的对象赋初值 } /** *获取数据库连接 *@return */ privateConnectiongetConnection(){ Stringdriver_class=null; Stringdriver_url=null; Stringdatabase_user=null; Stringdatabase_password=null; try{ InputStreamfis=this.getClass().getResourceAsStream("/db.properties");//加载数据库配置文件到内存中 Propertiesp=newProperties(); p.load(fis); driver_class=p.getProperty("driver_class");//获取数据库配置文件 driver_url=p.getProperty("driver_url"); database_user=p.getProperty("database_user"); database_password=p.getProperty("database_password"); Class.forName(driver_class); con=DriverManager.getConnection(driver_url,database_user,database_password); }catch(ClassNotFoundExceptione){ //TODOAuto-generatedcatchblock e.printStackTrace(); }catch(SQLExceptione){ //TODOAuto-generatedcatchblock e.printStackTrace(); }catch(FileNotFoundExceptione){ //TODOAuto-generatedcatchblock e.printStackTrace(); }catch(IOExceptione){ //TODOAuto-generatedcatchblock e.printStackTrace(); } returncon; } /** *关闭数据库 *@paramcon *@parampst *@paramrst */ privatevoidcloseAll(Connectioncon,PreparedStatementpst,ResultSetrst){ if(rst!=null){ try{ rst.close(); }catch(SQLExceptione){ //TODOAuto-generatedcatchblock e.printStackTrace(); } } if(pst!=null){ try{ pst.close(); }catch(SQLExceptione){ //TODOAuto-generatedcatchblock e.printStackTrace(); } } if(con!=null){ try{ con.close(); }catch(SQLExceptione){ //TODOAuto-generatedcatchblock e.printStackTrace(); } } } /** *关闭数据库 *@paramcon *@parampst *@paramrst */ privatevoidcloseAll(Connectioncon,Statementpst,ResultSetrst){ if(rst!=null){ try{ rst.close(); }catch(SQLExceptione){ //TODOAuto-generatedcatchblock e.printStackTrace(); } } if(pst!=null){ try{ pst.close(); }catch(SQLExceptione){ //TODOAuto-generatedcatchblock e.printStackTrace(); } } if(con!=null){ try{ con.close(); }catch(SQLExceptione){ //TODOAuto-generatedcatchblock e.printStackTrace(); } } } /** *查找 *@paramsql *@paramsqlValues *@return */ publicResultexecuteQuery(){ Resultresult=null; ResultSetrst=null; PreparedStatementpst=null; try{ pst=con.prepareStatement(sql); if(sqlValues!=null&&sqlValues.size()>0){//当sql语句中存在占位符时 setSqlValues(pst,sqlValues); } rst=pst.executeQuery(); result=ResultSupport.toResult(rst);//一定要在关闭数据库之前完成转换 }catch(SQLExceptione){ //TODOAuto-generatedcatchblock e.printStackTrace(); }finally{ this.closeAll(con,pst,rst); } returnresult; } /** *增删改 *@return */ publicintexecuteUpdate(){ intresult=-1; PreparedStatementpst=null; try{ pst=con.prepareStatement(sql); if(sqlValues!=null&&sqlValues.size()>0){//当sql语句中存在占位符时 setSqlValues(pst,sqlValues); } result=pst.executeUpdate(); }catch(SQLExceptione){ //TODOAuto-generatedcatchblock e.printStackTrace(); }finally{ this.closeAll(con,pst,null); } returnresult; } /** *使用PreparedStatement加批量的方法 *@return */ publicint[]executeUpdateMore(){ int[]result=null; try{ PreparedStatementprest=con.prepareStatement(sql,ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_READ_ONLY); for(ListsqlValueString:sqlValue){ for(inti=0;i<sqlValueString.size();i++){ try{ prest.setObject(i+1,sqlValueString.get(i)); }catch(SQLExceptione){ //TODOAuto-generatedcatchblock e.printStackTrace(); } } prest.addBatch(); } prest.executeBatch(); /*con.commit();*/ this.closeAll(con,prest,null); }catch(SQLExceptionex){ Logger.getLogger(Dbhelper.class.getName()).log(Level.SEVERE,null,ex); } returnresult; } /** *使用PreparedStatement加批量的方法,strvalue: *"INSERTINTOadlogs(ip,website,yyyymmdd,hour,object_id)VALUES('192.168.1.3','localhost','20081009',8,'23123')" *@return *@throwsSQLException */ publicint[]executeUpdateMoreNotAuto()throwsSQLException{ int[]result=null; con.setAutoCommit(false); Statementstmt=con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_READ_ONLY); String[]SqlString=null; for(Stringstrvalue:SqlString){ stmt.execute(strvalue); } con.commit(); returnresult; } /** *使用PreparedStatement加批量的方法,strvalue: *"INSERTINTOadlogs(ip,website,yyyymmdd,hour,object_id)VALUES('192.168.1.3','localhost','20081009',8,'23123')" *@return *@throwsSQLException */ publicint[]executeMoreNotAuto()throwsSQLException{ //保存当前自动提交模式 BooleanbooleanautoCommit=false; String[]SqlString=null; int[]result=null; try { booleanautoCommit=con.getAutoCommit(); //关闭自动提交 con.setAutoCommit(false); Statementstmt=con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_READ_ONLY); //使用Statement同时收集多条sql语句 /*stmt.addBatch(insert_sql1); stmt.addBatch(insert_sql2); stmt.addBatch(update_sql3);*/ for(Stringstrvalue:SqlString){ stmt.addBatch(strvalue); } //同时提交所有的sql语句 stmt.executeBatch(); //提交修改 con.commit(); con.setAutoCommit(booleanautoCommit); this.closeAll(con,stmt,null); } catch(Exceptione) { e.printStackTrace(); con.rollback();//设定setAutoCommit(false)没有在catch中进行Connection的rollBack操作,操作的表就会被锁住,造成数据库死锁 } returnresult; } /** *给sql语句中的占位符赋值 *@parampst *@paramsqlValues */ privatevoidsetSqlValues(PreparedStatementpst,ListsqlValues){ for(inti=0;i<sqlValues.size();i++){ try{ pst.setObject(i+1,sqlValues.get(i)); }catch(SQLExceptione){ //TODOAuto-generatedcatchblock e.printStackTrace(); } } } }
我们的在db.properties中写入访问数据库的信息:
driver_class=com.mysql.jdbc.Driver driver_url=jdbc:mysql://192.168.22.246:3306/importexceltest database_user=basic database_password=basic
测试:
importjava.util.*; publicclassImportExcelTest{ publicstaticvoidmain(String[]args){ /*Dbhelperdb=newDbhelper(); Stringsql="insertintotb_coursetype(id,courseTypeName)values('2012003','qmx3')"; db.setSql(sql); db.executeUpdate();*/ /*Dbhelperdb1=newDbhelper(); Stringsql1="insertintotb_coursetype(id,courseTypeName)values(?,?)"; ListsqlValues=newArrayList(); sqlValues.add("2012004"); sqlValues.add("qmx4"); db1.setSqlValues(sqlValues); db1.setSql(sql1); db1.executeUpdate();*/ Dbhelperdb=newDbhelper(); Stringsql="insertintotb_coursetype(id,courseTypeName)values(?,?)"; List<List>sqlValues=newArrayList(); ListsqlValueString=newArrayList(); sqlValueString.add("2012010"); sqlValueString.add("qmx10"); sqlValues.add(sqlValueString); ListsqlValueString1=newArrayList(); sqlValueString1.add("2012011"); sqlValueString1.add("qmx11"); sqlValues.add(sqlValueString1); ListsqlValueString2=newArrayList(); sqlValueString2.add("2012012"); sqlValueString2.add("qmx12"); sqlValues.add(sqlValueString2); ListsqlValueString3=newArrayList(); sqlValueString3.add("2012013"); sqlValueString3.add("qmx13"); sqlValues.add(sqlValueString3); db.setSqlValue(sqlValues); db.setSql(sql); db.executeUpdateMore(); } }