📄 iuserdaojdbcimpl.java
字号:
package com.lideedu.huang.addressBook.dao.user;
import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import com.lideedu.huang.addressBook.dao.DBUtils;
import com.lideedu.huang.addressBook.exceptions.DataAccessException;
import com.lideedu.huang.addressBook.pojos.User;
import com.lideedu.huang.addressBook.pojos.ValueObject;
import com.lideedu.huang.addressBook.utils.PageBean;
public class IUserDaoJDBCImpl implements IUserDao {
private static final String INSERT_STR = "insert into users values(?,?,?,?,?,?,?)";
private static final String FIND_BY_OBJ = "select * from users where username=? and password=?";
private static final String FIND_BY_USERNAME= "select * from users where username=?";
private static final String UPDAE_STR="update users set username=?,sex=?,age=?,birthday=?,password=?,hobbies=? where username=?";
Connection con = null;
PreparedStatement psta = null;
ResultSet rs = null;
//检测用户是否存在
public boolean findByObj(ValueObject findRecord)throws DataAccessException {
User user = (User) findRecord;
try {
con = DBUtils.getConnection();
psta = con.prepareStatement(FIND_BY_OBJ);
psta.setString(1, user.getUsername());
psta.setString(2, user.getPassword());
rs = psta.executeQuery();
if (rs.next())
return true;
else
return false;
} catch (SQLException e) {
throw new DataAccessException("检测用户信息失败!,对象为:" + user, e);
} finally {
DBUtils.releaseDBRes(con, psta, rs);
}
}
//检测用户名是否已存在
public boolean findByUserName(String userName) throws DataAccessException {
try {
con = DBUtils.getConnection();
psta = con.prepareStatement(FIND_BY_USERNAME);
psta.setString(1,userName);
rs = psta.executeQuery();
if (rs.next())
return true;
else
return false;
} catch (SQLException e) {
throw new DataAccessException("检测用户名失败!,e");
} finally {
DBUtils.releaseDBRes(con, psta, rs);
}
}
//用户注册
public void insert(ValueObject insertRecord) throws DataAccessException {
User user = (User) insertRecord;
try {
con = DBUtils.getConnection();
psta = con.prepareStatement(INSERT_STR);
psta.setString(1, user.getUsername());
psta.setInt(2, user.getSex());
psta.setInt(3,user.getAge());
psta.setDate(4, new Date(user.getBirthDay().getTime()));
psta.setString(5, user.getPassword());
psta.setString(6, user.getHobbies());
psta.setDate(7, new Date(user.getRegDate().getTime()));
psta.executeUpdate();
} catch (SQLException e) {
throw new DataAccessException("用户注册失败!对象为:" + user, e);
} finally {
DBUtils.releaseDBRes(con, psta, null);
}
}
//修改用户信息
public void updateUserInfo(String oldUsername, ValueObject updateRecord) throws DataAccessException {
User user=(User)updateRecord;
try{
con=DBUtils.getConnection();
psta=con.prepareStatement(UPDAE_STR);
psta.setString(1, user.getUsername());
psta.setInt(2, user.getSex());
psta.setInt(3, user.getAge());
psta.setDate(4, new Date(user.getBirthDay().getTime()));
psta.setString(5, user.getPassword());
psta.setString(6, user.getHobbies());
psta.setString(7, oldUsername);
psta.executeUpdate();
}catch(SQLException e){
throw new DataAccessException("修改用户信息失败! 对象为:"+user,e);
}finally{
DBUtils.releaseDBRes(con, psta, null);
}
}
//按主键查询
public ValueObject findByPK(String primaryKey) throws DataAccessException {
String username=primaryKey;
User user=new User();
try{
con=DBUtils.getConnection();
psta=con.prepareStatement(FIND_BY_USERNAME);
psta.setString(1, username);
rs=psta.executeQuery();
if(rs.next()){
user.setUsername(rs.getString(1));
user.setSex(rs.getShort(2));
user.setAge(rs.getShort(3));
user.setBirthDay(rs.getDate(4));
user.setPassword(rs.getString(5));
user.setHobbies(rs.getString(6));
user.setRegDate(rs.getDate(7));
return user;
}else
return null;
}catch(SQLException e){
throw new DataAccessException("查找用户失败!用户名为:"+username,e);
}finally{
DBUtils.releaseDBRes(con, psta, rs);
}
}
public void update(ValueObject updateRecord) throws DataAccessException {
// TODO Auto-generated method stub
}
public void delete(ValueObject deleteRecord) throws DataAccessException {
// TODO Auto-generated method stub
}
public PageBean findAll(String queryCondition,String queryKeyword,PageBean pageBean) throws DataAccessException {
// TODO Auto-generated method stub
return null;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -