欢迎来到天天文库
浏览记录
ID:6679806
大小:36.00 KB
页数:11页
时间:2018-01-22
《java底层数据库操作方法》由会员上传分享,免费在线阅读,更多相关内容在行业资料-天天文库。
1、java底层数据库操作方法dao层:对数据库的操作publicclassBaseDao{//数据库连接Connectionconn=null;privatefinalstaticStringDRIVER="com.microsoft.sqlserver.jdbc.SQLserverDriver";privatefinalstaticStringURL="jdbc:sqlserver://localhost:1433;DatabaseName=Students";privatefinalstaticStringDUSER="sa";privatefi
2、nalstaticStringDPASS="ww";//获取连接publicConnectiongetConn(){try{Class.forName(DRIVER);conn=DriverManager.getConnection(URL,DUSER,DPASS);}catch(Exceptione){e.printStackTrace();}returnconn;}//数据更新publicintupdateSQL(Stringsql,ArrayListlist){Connectionconn=this.getConn();PreparedSta
3、tementpstmt=null;intflag=0;try{pstmt=conn.prepareStatement(sql);if(list!=null){for(inti=0;i4、list){Map[]maps=null;PreparedStatementpstmt=null;ResultSetresultset=null;Connectionconn=this.getConn();try{pstmt=conn.prepareStatement(sql);if(list!=null){for(inti=0;i5、.toResult(resultset);maps=result.getRows();}catch(Exceptione){e.printStackTrace();}finally{try{pstmt.close();resultset.close();conn.close();}catch(Exceptione){e.printStackTrace();}}returnmaps;}}使用hibernate中的dao层importjava.io.Serializable;importjava.util.ArrayList;importjava.ut6、il.Iterator;importjava.util.List;importorg.hibernate.Query;importorg.hibernate.Session;importorg.hibernate.SessionFactory;importorg.hibernate.Transaction;importorg.hibernate.cfg.Configuration;importdao.DaoInface;importentity.Users;publicclassDaoInfaceImplimplementsDaoInface{Co7、nfigurationcfg=newConfiguration().configure();SessionFactorysf=cfg.buildSessionFactory();//建立会话工厂Transactiontran=null;//删除publicbooleanDelete(Objectobject){Sessionsession=sf.openSession();booleanflag=false;try{tran=session.beginTransaction();session.delete(object);tran.commit(8、);flag=true;}catch(Exceptione){if(tran!=null){tran.rollback()
4、list){Map[]maps=null;PreparedStatementpstmt=null;ResultSetresultset=null;Connectionconn=this.getConn();try{pstmt=conn.prepareStatement(sql);if(list!=null){for(inti=0;i5、.toResult(resultset);maps=result.getRows();}catch(Exceptione){e.printStackTrace();}finally{try{pstmt.close();resultset.close();conn.close();}catch(Exceptione){e.printStackTrace();}}returnmaps;}}使用hibernate中的dao层importjava.io.Serializable;importjava.util.ArrayList;importjava.ut6、il.Iterator;importjava.util.List;importorg.hibernate.Query;importorg.hibernate.Session;importorg.hibernate.SessionFactory;importorg.hibernate.Transaction;importorg.hibernate.cfg.Configuration;importdao.DaoInface;importentity.Users;publicclassDaoInfaceImplimplementsDaoInface{Co7、nfigurationcfg=newConfiguration().configure();SessionFactorysf=cfg.buildSessionFactory();//建立会话工厂Transactiontran=null;//删除publicbooleanDelete(Objectobject){Sessionsession=sf.openSession();booleanflag=false;try{tran=session.beginTransaction();session.delete(object);tran.commit(8、);flag=true;}catch(Exceptione){if(tran!=null){tran.rollback()
5、.toResult(resultset);maps=result.getRows();}catch(Exceptione){e.printStackTrace();}finally{try{pstmt.close();resultset.close();conn.close();}catch(Exceptione){e.printStackTrace();}}returnmaps;}}使用hibernate中的dao层importjava.io.Serializable;importjava.util.ArrayList;importjava.ut
6、il.Iterator;importjava.util.List;importorg.hibernate.Query;importorg.hibernate.Session;importorg.hibernate.SessionFactory;importorg.hibernate.Transaction;importorg.hibernate.cfg.Configuration;importdao.DaoInface;importentity.Users;publicclassDaoInfaceImplimplementsDaoInface{Co
7、nfigurationcfg=newConfiguration().configure();SessionFactorysf=cfg.buildSessionFactory();//建立会话工厂Transactiontran=null;//删除publicbooleanDelete(Objectobject){Sessionsession=sf.openSession();booleanflag=false;try{tran=session.beginTransaction();session.delete(object);tran.commit(
8、);flag=true;}catch(Exceptione){if(tran!=null){tran.rollback()
此文档下载收益归作者所有