您好,欢迎访问三七文档
当前位置:首页 > 商业/管理/HR > 项目/工程管理 > 使用JDBC做的增删改查
业务类publicclassUserDaoImplimplementsUserDao{//定义一个JdbcprivateJdbcTemplatejt;//必须有set方法,因为是通过set方法装配的publicvoidsetJt(JdbcTemplatejt){this.jt=jt;}@Overridepublicvoiddelete(intid){//TODOAuto-generatedmethodstubStringsql=deletefromuserwhereid=+id;inttemp=this.jt.update(sql);if(temp0){System.out.println(删除成功!);}else{System.out.println(删除失败!);}}@Overridepublicvoidinsert(Userentity){//TODOAuto-generatedmethodstubStringsql=insertintouser(name,age,sex)values(?,?,?);Objectobj[]={entity.getName(),entity.getAge(),entity.getSex()};inttemp=this.jt.update(sql,obj);if(temp0){System.out.println(插入成功!);}else{System.out.println(插入失败!);}}@OverridepublicListUserselectAll(){Stringsql=select*fromuser;Listlist=this.jt.query(sql,newRowMapper(){@OverridepublicObjectmapRow(ResultSetrs,introw)throwsSQLException{//TODOAuto-generatedmethodstubUseruser=newUser();user.setId(rs.getInt(id));user.setName(rs.getString(name));user.setAge(rs.getInt(age));user.setSex(rs.getString(sex));returnuser;}});System.out.println(list.size());//TODOAuto-generatedmethodstubreturnlist;}@OverridepublicUserselectById(intid){//TODOAuto-generatedmethodstubStringsql=selectid,name,age,sexfromuserwhereid=+id;Useruser=(User)jt.queryForObject(sql,newRowMapper(){@OverridepublicObjectmapRow(ResultSetrs,intarg1)throwsSQLException{//TODOAuto-generatedmethodstubUseruser=newUser();user.setId(rs.getInt(id));user.setName(rs.getString(name));user.setAge(rs.getInt(age));user.setSex(rs.getString(sex));returnuser;}});returnuser;}@Overridepublicvoidupdate(Userentity){//TODOAuto-generatedmethodstubStringsql=updateusersetname=?,age=?,sex=?whereid=?;Objectobj[]={entity.getName(),entity.getAge(),entity.getSex(),entity.getId()};inttemp=jt.update(sql,obj);if(temp0){System.out.println(更新成功!);}else{System.out.println(更新失败!);}}}Xml中的部分代码1.!--分散配置--2.context:property-placeholderlocation=jdbc.properties/3.!--c3p0的数据源,设置它是为了配置jbdcTemplate--4.beanid=comboPooledDataSourceclass=com.mchange.v2.c3p0.ComboPooledDataSource5.!--驱动程序--6.propertyname=driverClass7.value${driverClass}/value8./property9.!--用户名--10.propertyname=user11.value${username}/value12./property13.!--密码--14.propertyname=password15.value${password}/value16./property17.!--url--18.propertyname=jdbcUrl19.value${url}/value20./property21.!--最大池数--22.propertyname=maxPoolSize23.value${c3p0.pool.max}/value24./property25.!--最小池数--26.propertyname=minPoolSize27.value${c3p0.pool.min}/value28./property29.!--初始化的池数--30.propertyname=initialPoolSize31.value${c3p0.pool.init}/value32./property33./bean34.!--配置jdbcTemplate的bean,设置它是为了设置业务bean中的jdbcTemplate参数--35.spanstyle=background-color:#ffff00;spanstyle=background-color:#f9b3ec;spanstyle=background-color:#f9b3ec;spanstyle=background-color:#ff0000;beanid=jdbcTemplateclass=org.springframework.jdbc.core.JdbcTemplate36.constructor-argref=comboPooledDataSource/37./bean/span/span/span/span38.!--设置业务bean--39.beanid=userDaoImplclass=cn.csdn.dao.UserDaoImpl40.propertyname=jtref=jdbcTemplate/41./bean使用NamedParameterJdbcTemplate类,实现JDBC,参数是用Map集合传递的业务类:1.publicclassUserDaoImpl2implementsUserDao{2.//定义一个Jdbc3.privateNamedParameterJdbcTemplatenpjt;4.//必须有set方法,因为NamedParameterJdbcTemplate类是用set方法加载的5.publicvoidsetNpjt(NamedParameterJdbcTemplatenpjt){6.this.npjt=npjt;7.}8.@Override9.publicvoiddelete(intid){10.//TODOAuto-generatedmethodstub11.Stringsql=deletefromuserwhereid=:id;12.MapString,Objectmap=newHashMapString,Object();13.map.put(id,id);14.inttemp=this.npjt.update(sql,map);15.if(temp0){16.System.out.println(删除成功!);17.}else{18.System.out.println(删除失败!);19.}20.}21.22.@Override23.publicvoidinsert(Userentity){24.//TODOAuto-generatedmethodstub25.Stringsql=insertintouser(name,age,sex)values(:name,:age,:sex);26.MapString,Objectmap=newHashMapString,Object();27.map.put(name,entity.getName());28.map.put(age,entity.getAge());29.map.put(sex,entity.getSex());30.inttemp=npjt.update(sql,map);31.if(temp0){32.System.out.println(插入成功!);33.}else{34.System.out.println(插入失败!);35.}36.}37.//38.//39.//40.@Override41.publicListUserselectAll(){42.Stringsql=select*fromuser;43.//Listlist=this.npjt.queryForList(sql,newHashMap());44.Listlist=npjt.query(sql,newHashMap(),newRowMapper(){45.46.@Override47.publicObjectmapRow(ResultSetrs,intarg1)throwsSQLException{48.//TODOAuto-generatedmethodstub49.Useruser=newUser();50.user.setId(rs.getInt(id));51.user.setName(rs.getString(name));52.user.setAge(rs.getInt(age));53.user.setSex(rs.getString(sex));54.returnuser;55.}56.57.});58.System.out.println(list.size());59.returnlist;60.}61.62.@Override63.publicUserselectById(intid){64.//TODOAuto-generatedmethodstub65.Stringsql=selectid,name,age,sexfromuserwhereid=1;66.67.MapString,Objectmap=newHashMapString,Object();68.map.put(id,id);69.Useruser=(User)npjt.queryForObject(sql,map,newRowMapper(){70.71.@Override72.publicObjectmapRow(ResultSetrs,intarg1)throwsSQLException{73.Useruser=newUser();74.user.setId(rs.getInt(id));75.user
本文标题:使用JDBC做的增删改查
链接地址:https://www.777doc.com/doc-2687048 .html