package dao;


import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;


import cn.jdbc.JDBCUtils;
import entity.User;
/**
 * DAO类
 *  负责访问数据库
 * @author soft01
 *
 */
public class UserDao{
 /**
  * 用于将所有用户查询出来,每一个用户的信息对应一个user对象,返回一个由这些user对象组成集和对象
  * @return
  * @throws Exception 
  */
 public List<User> findAll() throws Exception{
 List<User> listuser = new ArrayList<User>();
 Connection conn = null;
 PreparedStatement ps = null;
 ResultSet rs = null;
 String sql = "select * from t_user";
 try {
 conn = JDBCUtils.getconn();
 ps = conn.prepareStatement(sql);
 rs = ps.executeQuery();
 while(rs.next()) {
 int id = rs.getInt("id");
 String username = rs.getString("username");
 String password = rs.getString("password"); 
 String email = rs.getString("email");
 User user = new User();
 //给user对象敷值
 user.setId(id);
 user.setUsername(username);
 user.setPassword(password);
 user.setEmail(email);
 listuser.add(user);//把user对象放到集和当中
 }
 } catch (Exception e) {
 e.printStackTrace();
 throw e;
 }finally {
 JDBCUtils.close(rs, ps, conn);
 }
 return listuser;
 }
 /**
  * 添加用户
  * @param user
  * @return
  * @throws Exception 
  */
 public int save(User user) throws Exception {
 int rows = 0;
 Connection conn = null;
 PreparedStatement ps = null;
 ResultSet rs = null;
 String sql = "insert into t_user values(null,?,?,?)";
 try {
 conn = JDBCUtils.getconn();
 ps = conn.prepareStatement(sql);
 ps.setString(1, user.getUsername());
 ps.setString(2, user.getPassword());
 ps.setString(3, user.getEmail());
 rows = ps.executeUpdate();
 } catch (Exception e) {
 throw e;
 }finally {
 JDBCUtils.close(rs, ps, conn);
 }
 return rows;
 }
 /**
  * 删除用户
  * @return
  * @throws Exception 
  */
 public int del(int id) throws Exception {
 int rows = 0;
 Connection conn = null;
 PreparedStatement ps = null;
 ResultSet rs = null;
 String sql = "delete from t_user where id=?";
 try {
 conn = JDBCUtils.getconn();
 ps = conn.prepareStatement(sql);
 ps.setInt(1, id);
 rows = ps.executeUpdate();
 } catch (Exception e) {
 e.printStackTrace();
 throw e;//将异常抛出,让调用着去解决
 }finally {
 JDBCUtils.close(rs, ps, conn);
 }
 return rows;
 }
 /**
  * 修改用户名
  * @return
  */
 public int updateuname(User user) throws Exception {
 int rows = 0;
 Connection conn = null;
 PreparedStatement ps = null;
 ResultSet rs = null;
 String sql = "update t_user set username=? where email=?";
 //update t_user set username='admin1' where email='67890@qq.com';
 try {
 conn = JDBCUtils.getconn();
 ps = conn.prepareStatement(sql);
 ps.setString(1, user.getUsername());
 ps.setString(2, user.getEmail());
 rows = ps.executeUpdate();
 } catch (Exception e) {
 e.printStackTrace();
 throw e;
 }finally {
 JDBCUtils.close(rs, ps, conn);
 }
 return rows;
 }
 /**
  * 修改用户密码
  * @return
  * @throws Exception 
  */
 public int updatepwd(User user) throws Exception {
 int rows = 0;
 Connection conn = null;
 PreparedStatement ps = null;
 ResultSet rs= null;
 String sql= "update t_user set password=? where id=?";
 try {
 conn  = JDBCUtils.getconn();
 ps = conn.prepareStatement(sql);
 ps.setString(1, user.getPassword());
 ps.setInt(2, user.getId());
 rows = ps.executeUpdate();
 } catch (Exception e) {
 e.printStackTrace();
 throw e;
 }finally {
 JDBCUtils.close(rs, ps, conn);
 }
 return rows;
 }
 /**
  * 查询用户名的方法
  * @param name
  * @return
  * @throws Exception
  */
 //这种方法性能会比较好一点
   public User findl(String name) throws Exception{//根据用户名查询指定的信息,如果找不到,返回null
   User user = null;
   Connection conn = null;
 PreparedStatement ps = null;
 ResultSet rs= null;
 String sql= "select * from t_user where username=?";
 try{
 conn  = JDBCUtils.getconn();
 ps = conn.prepareStatement(sql);
 ps.setString(1,name);
 rs = ps.executeQuery();
 if(rs.next()) {
 user = new User();
 user.setId(rs.getInt("id"));
 user.setUsername(name);
 user.setPassword(rs.getString("password"));
 user.setEmail(rs.getString("email"));
 }
 }catch(Exception e) {
 e.printStackTrace();
 throw e;
 }finally {
 JDBCUtils.close(rs, ps, conn);
 }
 return user;
 } 
  
   
   


 /**
  * 用户登陆方法
  * @param username
  * @param password
  * @return
  * @throws Exception
  */

 public boolean login(String username,String password) throws Exception {
 boolean flag = false;
 Connection conn = null;
 PreparedStatement ps = null;
 ResultSet rs= null;
 String sql = "select username,password from t_user where username=? and password=?";
 try {
 conn  = JDBCUtils.getconn();
 ps = conn.prepareStatement(sql);
 ps.setString(1, username);
 ps.setString(2, password);
 rs = ps.executeQuery();
 while(rs.next()) {
 if(rs != null) {
 flag=true;
 }
 }
 } catch (Exception e) {
 e.printStackTrace();
 throw e;
 }finally {
 JDBCUtils.close(rs, ps, conn);
 }
 return flag;
 }
 public List<User> limit(int page) throws Exception {//page是当前页数
 ArrayList<User> userlist =new ArrayList<User>();
 Connection conn = null;
 PreparedStatement ps = null;
 ResultSet rs= null;
 String sql = "select * from t_user limit ?,?";//页数-1*条数,条数
 try {
 conn = JDBCUtils.getconn();
 ps = conn.prepareStatement(sql);
 ps.setInt(1, (page-1)*5);
 ps.setInt(1, 5);
 rs = ps.executeQuery();
 while(rs.next()) {
 int id = rs.getInt("id");
 String username = rs.getString("username");
 String password = rs.getString("password"); 
 String email = rs.getString("email");
 User user = new User();
 //给user对象敷值
 user.setId(id);
 user.setUsername(username);
 user.setPassword(password);
 user.setEmail(email);
 userlist.add(user);//把user对象放到集和当中
 }
 } catch (Exception e) {
 e.printStackTrace();
 throw e;
 }
 return userlist;
 }

}