package com.bbs.service.impl;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.List;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;
import com.bbs.model.User;
import com.bbs.service.UserService;
public class UserServiceImpl implements UserService {
public User userSelectByUserName(Connection con, String userName)
throws Exception {
String sql = "select * from user where userName=?";
PreparedStatement pstmt = con.prepareStatement(sql);
pstmt.setString(1, userName);
ResultSet rs = pstmt.executeQuery();
int id = 0;
String name = "";
int nameFlag = 0;
String email = "";
int emailFlag = 0;
String password = "";
String comeFrom = "";
String job = "";
String description = "";
String sign = "";
String qq = "";
String msn = "";
String image = "";
int score = 0;
int flag = 0;
String regDate;
User user = new User();
if(rs.next()){
id = rs.getInt("id");
name = rs.getString("name");
nameFlag = rs.getInt("nameFlag");
email = rs.getString("email");
emailFlag = rs.getInt("emailFlag");
password = rs.getString("password");
comeFrom = rs.getString("comeFrom");
job = rs.getString("job");
description = rs.getString("description");
sign = rs.getString("sign");
qq = rs.getString("qq");
msn = rs.getString("msn");
image = rs.getString("image");
score = rs.getInt("score");
flag = rs.getInt("flag");
regDate = rs.getString("regDate");
user.setId(id);
user.setUserName(userName);
user.setName(name);
user.setNameFlag(nameFlag);
user.setEmail(email);
user.setEmailFlag(emailFlag);
user.setPassword(password);
user.setComeFrom(comeFrom);
user.setJob(job);
user.setDescription(description);
user.setSign(sign);
user.setQq(qq);
user.setMsn(msn);
user.setImage(image);
user.setScore(score);
user.setFlag(flag);
user.setRegDate(regDate);
}
return user;
}
/*
* 用户注册
*/
public void userAdd(Connection con, String userName, String name,
String nameFlag, String email, String emailFlag, String password,
String comeFrom, String job, String description,
String sign, String qq, String msn, String image) throws Exception {
String sql = "insert into user values(null,?,?,?,?,?,?,?,?,?,?,?,?,?,?,1000,0,now())";
PreparedStatement pstmt = con.prepareStatement(sql);
pstmt.setString(1, userName);
pstmt.setString(2, name);
pstmt.setString(3, nameFlag);
pstmt.setString(4, email);
pstmt.setString(5, emailFlag);
pstmt.setString(6, password);
pstmt.setString(7, comeFrom);
pstmt.setString(8, job);
pstmt.setString(10, description);
pstmt.setString(11, sign);
pstmt.setString(12, qq);
pstmt.setString(13, msn);
pstmt.setString(14, image);
pstmt.execute();
}
/*
* 验证用户名是否存在
*/
public boolean userGetByUserName(Connection con, String userName)
throws Exception {
String sql = "select userName from user where userName=?";
PreparedStatement pstmt = con.prepareStatement(sql);
pstmt.setString(1, userName);
ResultSet rs = pstmt.executeQuery();
if(rs.next()){
return true;
}else{
return false;
}
}
/*
* 添加Cookie
*/
public void addCookie(String name,String value,int maxAge,HttpServletResponse response){
Cookie cookie = new Cookie(name,value);
//设置cookie的有效时间(以秒为单位)
cookie.setMaxAge(maxAge);
//将Cookie添加到响应中,带回客户端
response.addCookie(cookie);
}
/*
* 验证用户名和密码是否正确
*/
public boolean userLogin(Connection con, String userName,String password)
throws Exception {
String sql = "select userName from user where userName=? and flag=0 and password=? ";
PreparedStatement pstmt = con.prepareStatement(sql);
pstmt.setString(1, userName);
pstmt.setString(2, password);
ResultSet rs = pstmt.executeQuery();
if(rs.next()){
return true;
}else{
return false;
}
}
/*
* 修改用户信息
*/
public void userUpdate(Connection con, String userName, String name,
String nameFlag, String email, String emailFlag, String password,
String comeFrom, String job, String description,
String sign, String qq, String msn) throws Exception {
String sql = "update user set name=?,nameFlag=?,email=?,emailFlag=?,password=?,comeFrom=?,job=?,mainPage=?,description=?,sign=?,qq=?,msn=? where userName=?";
PreparedStatement pstmt = con.prepareStatement(sql);
pstmt.setString(1, name);
pstmt.setString(2, nameFlag);
pstmt.setString(3, email);
pstmt.setString(4, emailFlag);
pstmt.setString(5, password);
pstmt.setString(6, comeFrom);
pstmt.setString(7, job);
pstmt.setString(9, description);
pstmt.setString(10, sign);
pstmt.setString(11, qq);
pstmt.setString(12, msn);
pstmt.setString(13, userName);
pstmt.execute();
}
public User userGetById(Connection con, String id) throws Exception {
String sql="select * from user where id=?";
PreparedStatement pstmt = con.prepareStatement(sql);
pstmt.setString(1, id);
ResultSet rs = pstmt.executeQuery();
User user = new User();
if(rs.next()){
String userName = rs.getString("userName");
String name = rs.getString("name");
int nameFlag = rs.getInt("nameFlag");
String email = rs.getString("email");
int emailFlag = rs.getInt("emailFlag");
String password = rs.getString("password");
String comeFrom = rs.getString("comeFrom");
String job = rs.getString("job");
String description = rs.getString("description");
String sign = rs.getString("sign");
String qq = rs.getString("qq");
String msn = rs.getString("msn");
String image = rs.getString("image");
int score = rs.getInt("score");
int flag = rs.getInt("flag");
String regDate = rs.getString("regDate");
user.setUserName(userName);
user.setName(name);
user.setNameFlag(nameFlag);
user.setEmail(email);
user.setEmailFlag(emailFlag);
user.setPassword(password);
user.setComeFrom(comeFrom);
user.setJob(job);
user.setDescription(description);
user.setSign(sign);
user.setQq(qq);
user.setMsn(msn);
user.setImage(image);
user.setScore(score);
user.setFlag(flag);
user.setRegDate(regDate);
}
return user;
}
/*
* 修改user表的image字段
*/
public void imageUpdate(Connection con,String userName,String path) throws Exception{
String sql = "update user set image=? where userName=?";
PreparedStatement pstmt = con.prepareStatement(sql);
pstmt.setString(1, path);
pstmt.setString(2, userName);
pstmt.execute();
}
/*
* 管理员查询user表的username字段
*/
public List selectUserName(Connection con,List list,String flag)throws Exception{
String sql = "select username,id from user where flag=?";
PreparedStatement pstmt = con.prepareStatement(sql);
pstmt.setString(1, flag);
ResultSet rs = pstmt.executeQuery();
String userName = "";
String id ="";
while(rs.next()){
User user = new User();
userName = rs.getString("username");
id = rs.getString("id");
user.setUserName(userName);
user.setId(Integer.parseInt(id));
list.add(user);
}
return list;
}
/*
* 管理员根据id修改user表的flag字段
*/
public void updateFlagByid(Connection con,String flag,String id)throws Exception{
String sql = "update user set flag=? where id=?";
PreparedStatement pstmt = con.prepareStatement(sql);
pstmt.setString(1, flag);
pstmt.setString(2, id);
pstmt.execute();
}
}