本文由西安白癜风医院收集,转载请注明出处本文由西安白癜风医院收集,转载请注明出处java学习笔记——使用JDBC,对数据库进行增删改查(方案一)工具类BaseDao.java[java]viewplaincopy1.packagecom.accp.jdbc;2.3.importjava.sql.Connection;4.importjava.sql.DriverManager;5.importjava.sql.PreparedStatement;6.importjava.sql.ResultSet;7.importjava.sql.SQLException;8.9.importorg.apache.log4j.Logger;10.11.publicclassBaseDao{12.//使用log4j记录日志13.privatestaticLoggerlogger=Logger.getLogger(BaseDao.class);14.//连接驱动15.privatestaticfinalStringDRIVER=com.microsoft.sqlserver.jdbc.SQLServerDriver;16.//连接路径17.privatestaticfinalStringURL=jdbc:sqlserver://localhost:1433;databaseName=test;18.//用户名19.privatestaticfinalStringUSERNAME=sa;20.//密码21.privatestaticfinalStringPASSWORD=sssaaa;22.23.//静态代码块24.static{25.try{26.//加载驱动27.Class.forName(DRIVER);28.}catch(ClassNotFoundExceptione){29.e.printStackTrace();30.logger.error(加载驱动失败,e);31.}32.}33.34./*本文由西安白癜风医院收集,转载请注明出处本文由西安白癜风医院收集,转载请注明出处35.*获取数据库连接36.*/37.publicConnectiongetConnection(){38.Connectionconn=null;39.logger.debug(开始连接数据库);40.try{41.//与数据库建立连接42.conn=DriverManager.getConnection(URL,USERNAME,PASSWORD);43.}catch(SQLExceptione){44.e.printStackTrace();45.logger.error(数据库连接失败!,e);46.}47.logger.debug(数据库连接成功);48.returnconn;49.}50.51./*52.*关闭数据库连接,注意关闭的顺序53.*/54.publicvoidclose(ResultSetrs,PreparedStatementps,Connectionconn){55.//注意:最后打开的最先关闭56.if(rs!=null){57.try{58.rs.close();59.rs=null;60.}catch(SQLExceptione){61.e.printStackTrace();62.logger.error(关闭ResultSet失败,e);63.}64.}65.if(ps!=null){66.try{67.ps.close();68.ps=null;69.}catch(SQLExceptione){70.e.printStackTrace();71.logger.error(关闭PreparedStatement失败,e);72.}73.}74.if(conn!=null){75.try{76.conn.close();77.conn=null;本文由西安白癜风医院收集,转载请注明出处本文由西安白癜风医院收集,转载请注明出处78.}catch(SQLExceptione){79.e.printStackTrace();80.logger.error(关闭Connection失败,e);81.}82.}83.}84.85./*86.*查询方法87.*88.*sql:要执行的sql语句89.*handler:自定义接口90.*obj:可变参数列表91.*/92.publicTTquery(Stringsql,ResultSetHandlerThandler,Object...obj){93.Connectionconn=getConnection();//获得连接94.PreparedStatementps=null;95.ResultSetrs=null;96.try{97.//创建PreparedStatement对象98.ps=conn.prepareStatement(sql);99.//为查询语句设置参数100.setParameter(ps,obj);101.//获得ResultSet结果集102.rs=ps.executeQuery();103.//返回对象104.returnhandler.callback(rs);105.}catch(SQLExceptione){106.e.printStackTrace();107.logger.error(数据库操作异常,e);108.}finally{109.//关闭连接110.close(rs,ps,conn);111.logger.debug(释放资源成功);112.}113.returnnull;114.}115.116./*117.*增加、修改、删除,的方法118.*119.*obj:可变参数列表120.*/本文由西安白癜风医院收集,转载请注明出处本文由西安白癜风医院收集,转载请注明出处121.publicintupdate(Stringsql,Object...obj){122.Connectionconn=getConnection();//获得连接123.PreparedStatementps=null;124.introws=0;125.try{126.//创建PreparedStatement对象127.ps=conn.prepareStatement(sql);128.//为查询语句设置参数129.setParameter(ps,obj);130.//获得受影响的行数131.rows=ps.executeUpdate();132.}catch(SQLExceptione){133.e.printStackTrace();134.logger.error(数据库操作异常,e);135.}finally{136.//关闭连接137.close(null,ps,conn);138.logger.debug(释放资源成功);139.}140.returnrows;141.}142.143./*144.*为预编译对象设置参数145.*/146.publicvoidsetParameter(PreparedStatementps,Object...obj)147.throwsSQLException{148.if(obj!=null&&obj.length0){149.//循环设置参数150.for(inti=0;iobj.length;i++){151.ps.setObject(i+1,obj[i]);152.}153.}154.}155.}156.157.//自定义接口158.interfaceResultSetHandlerT{159.publicTcallback(ResultSetrs);160.}操作类,进行增删改查UserDao.java[java]viewplaincopy本文由西安白癜风医院收集,转载请注明出处本文由西安白癜风医院收集,转载请注明出处1.packagecom.accp.jdbc;2.3.importjava.sql.Connection;4.importjava.sql.PreparedStatement;5.importjava.sql.ResultSet;6.importjava.sql.SQLException;7.importjava.sql.Statement;8.importjava.util.ArrayList;9.importjava.util.List;10.11.importcom.accp.entity.User;12.13.publicclassUserDaoextendsBaseDao{14.publicstaticvoidmain(String[]args){15.UserDaouser=newUserDao();16.ListUserlist=newArrayListUser();17.//查询id10的数据18.list=user.queryUser();19.//遍历结果集20.for(Useru:list){21.System.out.println(u);22.}23.24.//查询单条数据25.System.out.println(user.queryUserById(10));26.27.//更新数据28.Useru=newUser();29.u.setId(2);30.u.setName(张龙);31.u.setAge(22);32.System.out.println(更新+user.updateUser(u)+条数据);33.34.//删除数据35.System.out.println(删除+user.deleteUser(15)+条数据);36.37.//插入数据38.Useru1=newUser();39.u1.setName(张三);40.u1.setAge(18);41.user.insertUser(u1);42.}43.44.//查询多条信息本文由西安白癜风医院收集,转载请注明出处本文由西安白癜风医院收集,转载请注明出处45.publicListUserqueryUser(){46.Stringsql=select*fromuserswhereid10;47.//匿名实现自定义接口48.ResultSetHandlerListUserhandler=newResultSetHandlerListUser(){49.@Override50.publicListUsercallback(ResultSetrs){51.ListUserlist=newArrayListUser();//用于存放结果的集合,User类型52.Useruser=null;53.try{54.while(rs.next()){55.user=newUser();//实例化一个User对象56.user.setId(rs.getInt(id));57.user.setAge(rs.getInt(age));58.user.setName(rs.getString(name));59.list.add(user);//添加到list集合60.}61.}catch(SQLExceptione)