private QueryRunner qr=new QueryRunner(JdbcUtil.getDataSource());

本文介绍了一个用户管理模块的实现细节,包括用户添加、查询、更新和删除等功能。使用了Java和Apache Commons DbUtils库来操作数据库,实现了高效的用户数据管理。
package com.tfy.dao.impl;


import java.util.List;


import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;


import com.tfy.dao.UserDao;
import com.tfy.domain.User;
import com.tfy.util.JdbcUtil;


public class UserDaoImpl implements UserDao {


private QueryRunner qr=new QueryRunner(JdbcUtil.getDataSource());

/* (non-Javadoc)
* @see com.tfy.dao.impl.UserDao#addUser(com.tfy.domain.User)
*/
public void addUser(User user){
try {
String sql="insert into s_user(userID,userName,logonName,logonPwd,sex,birthday,education,telephone,interest,path,filename,remark) values(?,?,?,?,?,?,?,?,?,?,?,?)";
Object []params={user.getUserID(),user.getUserName(),user.getLogonName(),user.getLogonPwd(),user.getSex(),
user.getBirthday(),user.getEducation(),user.getTelephone(),user.getInterest(),user.getPath(),user.getFilename(),user.getRemark()};


qr.update(sql, params);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
/* (non-Javadoc)
* @see com.tfy.dao.impl.UserDao#findAllUser(int, int)
*/
public List<User> findAllUser(int startIndex,int pageSize){
try {
String sql="select * from s_user limit ?,?";
Object []params={startIndex,pageSize};
return qr.query(sql, new BeanListHandler<User>(User.class),params);

} catch (Exception e) {
throw new RuntimeException(e);
}
}
/* (non-Javadoc)
* @see com.tfy.dao.impl.UserDao#findAllUserByUserID(int, int, java.lang.String)
*/
public List<User> findAllUserByUserID(int startIndex,int pageSize,String userID){
try {
if(userID==null){
String sql="select * from s_user limit ?,?";
Object []params={startIndex,pageSize};
return qr.query(sql, new BeanListHandler<User>(User.class),params);
}else{
String sql="select * from s_user where userID=? limit ?,?";
Object []params={userID,startIndex,pageSize};
return qr.query(sql, new BeanListHandler<User>(User.class),params);
}

} catch (Exception e) {
throw new RuntimeException(e);
}
}
/* (non-Javadoc)
* @see com.tfy.dao.impl.UserDao#getTotalUsers(java.lang.String)
*/
public int getTotalUsers(String userID){
try {
if(userID==null){
String sql="select count(*) from s_user";
Long count= (Long) qr.query(sql,new ScalarHandler(1));
return count.intValue();
}else{
String sql="select count(*) from s_user where userID=?";
Long count= (Long) qr.query(sql,new ScalarHandler(1),userID);
return count.intValue();
}

} catch (Exception e) {
throw new RuntimeException(e);
}
}
/* (non-Javadoc)
* @see com.tfy.dao.impl.UserDao#findUserByuserID(java.lang.String)
*/
public User findUserByuserID(Integer userID) {
try {
String sql="select * from s_user where userID=?";
return qr.query(sql, new BeanHandler<User>(User.class),userID);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
public User findUserByuserName(String userName) {
try {
String sql="select * from s_user where userName=? ";
return qr.query(sql, new BeanHandler<User>(User.class),userName);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
public User findUser(String logonName, String logonPwd) {
try {
String sql="select * from s_user where logonName=? and logonPwd=?";
Object []params={logonName,logonPwd};
return qr.query(sql, new BeanHandler<User>(User.class),params);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
public List<User> findAllUser() {
try {
String sql="select * from s_user ";

return qr.query(sql, new BeanListHandler<User>(User.class));

} catch (Exception e) {
throw new RuntimeException(e);
}
}

public void updateUser(User user) {
try {
if(user.getUpload()!=null){
String sql="update s_user set userName=?,logonName=?,logonPwd=?,sex=?," +
"birthday=?,education=?,telephone=?,interest=?,remark=? where userID=? ";
Object []params={user.getUserName(),user.getLogonName(),user.getLogonPwd(),user.getSex(),
user.getBirthday(),user.getEducation(),user.getTelephone(),user.getInterest(),
user.getRemark(),user.getUserID()};


qr.update(sql, params);
}else{


String sql="update s_user set userName=?,logonName=?,logonPwd=?,sex=?," +
"birthday=?,education=?,telephone=?,interest=?,path=?,filename=?,remark=? where userID=? ";
Object []params={user.getUserName(),user.getLogonName(),user.getLogonPwd(),user.getSex(),
user.getBirthday(),user.getEducation(),user.getTelephone(),user.getInterest(),user.getPath(),
user.getFilename(),user.getRemark(),user.getUserID()};


qr.update(sql, params);
}
} catch (Exception e) {
throw new RuntimeException(e);
}
}
public void deleteUserByID(Integer userID) {
try {
String sql="delete from s_user where userID=? ";
qr.update(sql,userID);
} catch (Exception e) {
throw new RuntimeException(e);
}
}

}



第5关:购物车操作 400 学习内容 参考答案 记录 评论 本关任务 JDBC查询方法封装 购物车相关信息介绍 相关知识(略) 编程要求 测试说明 本关任务 为了能进行购物车的修改、添加、删除操作,本关需要借助JDBC,对数据库表t_cart、t_goods表进行操作。 为了完成本关任务,需要了解: JDBC更新方法封装; 购物车相关信息介绍。 JDBC查询方法封装 为了方便JDBC操作,平台进行的JDBC更新方法进行了封装,封装类com.educoder.dao.impl.BaseDao。 public static boolean operUpdate(String sql, List<Object> p) { Connection conn = null; PreparedStatement pste = null; int res = 0; conn = getConn(); try { pste = conn.prepareStatement(sql); if (p != null) { for (int i = 0; i < p.size(); i++) { pste.setObject(i + 1, p.get(i)); } } res = pste.executeUpdate(); } catch (SQLException e) { e.printStackTrace(); } finally { releaseAll(null, pste, conn); } return res > 0;// } 购物车相关信息介绍 MYSQL用户名 MYSQL密码 驱动 URL root 123123 com.mysql.jdbc.Driver jdbc:mysql://127.0.0.1:3306/online_shop?useUnicode=true&characterEncoding=UTF-8&autoReconnect=true com.educoder.entity.Goods字段及方法; 字段 描述 类型 get方法 set方法 goodsId 商品Id String getGoodsId() setGoodsId(String goodsId) goodsName 商品名 String getGoodsName() setGoodsName(String goodsName) goodsImg 展示于主页的图 String getGoodsImg() setGoodsImg(String goodsImg) goodsPrice 价格 BigDecimal getGoodsPrice() setGoodsPrice(BigDecimal goodsPrice) goodsNum 库存数量 Integer getGoodsNum() setGoodsNum(Integer goodsNum) salesNum 销售数量 Integer getSalesNum() setSalesNum(Integer salesNum) goodsSize 商品规格 String getGoodsSize() setGoodsSize(String goodsSize) goodsFrom 商品产地 String getGoodsFrom() setGoodsFrom(String goodsFrom) goodsTime 保质期 String getGoodsTime() setGoodsTime(String goodsTime) goodsSaveCondition 存储条件 String getGoodsSaveCondition() setGoodsSaveCondition(String goodsSaveCondition) goodsDescribe 商品描述介绍 String getGoodsDescribe() setGoodsDescribe(String goodsDescribe) goodsExplain 对商品简短说明 String getGoodsExplain() setGoodsExplain(String goodsExplain) goodsClass 所属类别 String
最新发布
09-22
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值