package dao;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import javabean.User_Info;
import uity.ConnectionFactory;
public class User_InfoDAO {
private Connection conn;
private PreparedStatement pstmt;
private ResultSet rs;
/**
* 根据用户名和密码,查找相关用户信息
*
* @param userName
* @param password
* @return 用户对象
*/
public User_Info findUsers(String userName, String password) {
User_Info user = null;
String sql = "select * from user_info where userid=? and password=? ";
try {
conn = ConnectionFactory.getConn();
pstmt = conn.prepareStatement(sql);
pstmt.setString(1, userName);
pstmt.setString(2, password);
rs = pstmt.executeQuery();
if (rs.next()) {
user = new User_Info();
user.setUserid(rs.getString("userid"));
user.setName(rs.getString("name"));
user.setAddress(rs.getString("address"));
user.setEmail(rs.getString("email"));
user.setSex(rs.getString("sex"));
user.setPhone(rs.getString("phone"));
user.setPic(rs.getString("pic"));
user.setPassword(rs.getString("password"));
}
} catch (Exception e) {
e.printStackTrace();
} finally {
ConnectionFactory.closeAll(conn, pstmt, rs);
}
return user;
}
/**
* 根据用户名查找头像
*
* @param userName
* @return 用户头象
*/
public String findUsersPic(String author) {
String pic = null;
String sql = "select pic from user_info where userid=?";
try {
conn = ConnectionFactory.getConn();
pstmt = conn.prepareStatement(sql);
pstmt.setString(1, author);
rs = pstmt.executeQuery();
if (rs.next()) {
pic=rs.getString("pic");
}
} catch (Exception e) {
e.printStackTrace();
} finally {
ConnectionFactory.closeAll(conn, pstmt, rs);
}
return pic;
}
/**
* 判断数据库中当前用户名是否存在
* @param userName 用户名
* @return 存在返回true,否则返回false
*/
public boolean findUsers(String userName){
String sql = "select * from user_info where userid=? ";
try {
conn = ConnectionFactory.getConn();
pstmt = conn.prepareStatement(sql);
pstmt.setString(1, userName);
rs = pstmt.executeQuery();
if (rs.next()) {
return true;
}
} catch (Exception e) {
e.printStackTrace();
} finally {
ConnectionFactory.closeAll(conn, pstmt, rs);
}
return false;
}
/**
* 添加用户信息
* @param userName
* @param password
* @return 操作数据库影响行数
*/
public int insertUser(User_Info user) {
int i=0;
String sql = "insert into user_info values(?,?,?,?,?,?,?,?) ";
try {
conn=ConnectionFactory.getConn();
pstmt=conn.prepareStatement(sql);
pstmt.setString(1, user.getUserid());
pstmt.setString(2, user.getPassword());
pstmt.setString(3, user.getName());
pstmt.setString(4, user.getSex());
pstmt.setString(5, user.getPhone());
pstmt.setString(6, user.getAddress());
pstmt.setString(7, user.getEmail());
pstmt.setString(8, user.getPic());
i=pstmt.executeUpdate();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return i;
}
/**
* 查找所有的注册用户信息
* @return 用户列表
*/
public List<User_Info> selectAllUser(){
List<User_Info> list=new ArrayList<User_Info>();
String sql = "select * from user_info ";
try {
conn = ConnectionFactory.getConn();
pstmt = conn.prepareStatement(sql);
rs = pstmt.executeQuery();
while(rs.next()) {
User_Info user = new User_Info();
user.setUserid(rs.getString("userid"));
user.setPassword(rs.getString("password"));
user.setName(rs.getString("name"));
user.setSex(rs.getString("sex"));
user.setPhone(rs.getString("phone"));
user.setAddress(rs.getString("address"));
user.setEmail(rs.getString("email"));
user.setPic(rs.getString("pic"));
list.add(user);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
ConnectionFactory.closeAll(conn, pstmt, rs);
}
return list;
}
/**
* 根据用户ID,进行删除操作
* @param userID
* @return 执行SQL语句所影响的数据库行数
*/
public int deleteUserByID(String userid){
int i=0;
String sql="delete from user_info where userid = ? ";
try {
conn = ConnectionFactory.getConn();
pstmt = conn.prepareStatement(sql);
pstmt.setString(1, userid);
i=pstmt.executeUpdate();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return i;
}
}