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

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);
}
}

}



你可能感兴趣的:(dataSource)