javaBean登录注册

package javabean;

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

import util.DBConnection;

public class User {
	private int id;
	private String name;
	private String password;
	private int age;
	private String email;
	private Date birthday;
	private float money;

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public int getAge() {
		return age;
	}

	public void setAge(int age) {
		this.age = age;
	}

	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	public Date getBirthday() {
		return birthday;
	}

	public void setBirthday(Date birthday) {
		this.birthday = birthday;
	}

	public float getMoney() {
		return money;
	}

	public void setMoney(float money) {
		this.money = money;
	}

	// 业务逻辑方法
	// 1。用户登陆,判断用户是否存在
	public User login() throws SQLException {
		Connection conn = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		User user = null;
		try {
			conn = DBConnection.getConnection();
			String sql = "select * from [user] where name=? and password=?";//连接时用and  不能用where name=?,password=?
			ps = conn.prepareStatement(sql);
			ps.setString(1, name);
			ps.setString(2, password);
			rs = ps.executeQuery();
			while (rs.next()) {
				user = new User();
				user.setId(rs.getInt("id"));
				user.setName(rs.getString("name"));
				user.setPassword(rs.getString("password"));
				user.setAge(rs.getInt("age"));
				user.setEmail(rs.getString("email"));
				user.setMoney(rs.getFloat("money"));
   
			}
		} finally {
			DBConnection.close(rs, ps, conn);
		}
		return user;

	}

	// 2。用户注册,插入一条记录
	public boolean register() throws SQLException {
		Connection conn = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		boolean flag = false;
		try {
			conn = DBConnection.getConnection();
			String sql = "insert into [user](name,password,age) values(?,?,?)";
			ps = conn.prepareStatement(sql);
			ps.setString(1, name);
			ps.setString(2, password);
			ps.setInt(3, age);
			ps.executeUpdate();
			flag = true;
		} finally {
			DBConnection.close(rs, ps, conn);
		}
		return flag;
	}

	// 3。用户个人信息修改
	public boolean update() throws SQLException {
		Connection conn = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		boolean flag = false;
		try {
			conn = DBConnection.getConnection();
			String sql = "update [user] set name=?,password=? where id =?";
			ps = conn.prepareStatement(sql);
			ps.setString(1, name);
			ps.setString(2, password);
			ps.setInt(3, id);
			ps.executeUpdate();
			flag = true;
		} finally {
			DBConnection.close(rs, ps, conn);
		}
		return flag;
	}

	// 4。管理员登陆后查看所有用户列表
	public List listUsers() throws SQLException {
		Connection conn = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		User user = null;
		List all = new ArrayList();
		try {
			conn = DBConnection.getConnection();
			String sql = "select * from [user]";
			ps = conn.prepareStatement(sql);
			rs = ps.executeQuery();
			while (rs.next()) {
				user = new User();
				user.setId(rs.getInt("id"));
				user.setName(rs.getString("name"));
				user.setPassword(rs.getString("password"));
				user.setAge(rs.getInt("age"));
				user.setEmail(rs.getString("email"));
				user.setMoney(rs.getFloat("money"));
				all.add(user);

			}
		} finally {
			DBConnection.close(rs, ps, conn);
		}
		return all;
	}

	// 5。管理员进行模糊查询
	@SuppressWarnings("unchecked")
	public List listUsersByName(String queryName) throws SQLException {
		Connection conn = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		User user = null;
		List all = new ArrayList();
		try {
			conn = DBConnection.getConnection();
			String sql = "select * from [user] where name like ?";
			ps = conn.prepareStatement(sql);
			ps.setString(1, "%"+queryName+"%");//开始忘记了写成name
			rs = ps.executeQuery();
			while (rs.next()) {
				user = new User();
				user.setId(rs.getInt("id"));
				user.setName(rs.getString("name"));
				user.setPassword(rs.getString("password"));
				user.setAge(rs.getInt("age"));
				user.setEmail(rs.getString("email"));
				user.setMoney(rs.getFloat("money"));
				all.add(user);

			}
		} finally {
			DBConnection.close(rs, ps, conn);
		}
		return all;
	}

	// 6。管理员根据用户ID删除一个用户
	public boolean delete(int id) throws SQLException {
		Connection conn = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		boolean flag = false;
		try {
			conn = DBConnection.getConnection();
			String sql = "delete from [user] where id=?";
			ps = conn.prepareStatement(sql);
			ps.setInt(1, id);
			ps.executeUpdate();
			flag = true;
		} finally {
			DBConnection.close(rs, ps, conn);
		}
		return flag;
	}

	// 7。根据用户得到USER对象
	public User get(int id) throws SQLException {
		Connection conn = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		User user = null;

		try {
			conn = DBConnection.getConnection();
			String sql = "select * from [user] where id=?";
			ps = conn.prepareStatement(sql);
			ps.setInt(1, id);
			rs = ps.executeQuery();
			while (rs.next()) {
				user = new User();
				user.setId(rs.getInt("id"));
				user.setName(rs.getString("name"));
				user.setPassword(rs.getString("password"));
				user.setAge(rs.getInt("age"));
				user.setEmail(rs.getString("email"));
				user.setMoney(rs.getFloat("money"));

			}
		} finally {
			DBConnection.close(rs, ps, conn);
		}
		return user;
	}

}

  

posted @ 2014-04-15 16:40  Emily1130  阅读(1187)  评论(0编辑  收藏  举报