⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 userregisterbean.java

📁 泽风留言本,非常小巧好用的东西啊.请多多照顾.
💻 JAVA
字号:
/**
 *  作者: 佟劲纬   创建日期: 2006-1-10
 *	
 *	QQ: 532443423 Email: TJW_7@163.com
 */
package com.tjw.guestbook.model;

import java.io.*;
import java.sql.*;

import com.tjw.guestbook.database.*;

/**
 * 管理用户Bean
 */
public class UserRegisterBean implements Serializable {

	private ConDatabase dataSource;

	public UserRegisterBean() {
	}

	/**
	 * 使用构造方法设置数据源
	 */
	public UserRegisterBean(ConDatabase dataSource) {
		this.dataSource = dataSource;
	}

	/**
	 * 设置数据源.
	 */
	public void setDataSource(ConDatabase dataSource) {
		this.dataSource = dataSource;
	}

	/**
	 * 如果用户名和密码和数据库中的用户信息匹配,则返回UserInfoBean, 否则返回null.
	 */
	public UserInfoBean authenticate(String userName, String password)
			throws SQLException {

		UserInfoBean userInfo = getUserInfo(userName);

		if (userInfo != null && userInfo.getPassword().equals(password)) {
			return userInfo;
		}
		return null;
	}

	/**
	 * 从数据库中查询符合用户名要求的记录把数据设置到UserInfoBean中并返回, 如果没有找到符合规则的记录则则返回null.
	 */
	public UserInfoBean getUserInfo(String userName) throws SQLException {

		UserInfoBean userInfo = null;

		Connection conn = dataSource.getConnection();

		String sql = "select id, userName, password, qq, email, homepage, isnull(admin, '') as admin from userinfo where username=? and admin=0";
		PreparedStatement ps = null;
		ResultSet rs = null;
		try {
			ps = conn.prepareStatement(sql);
			ps.setString(1, userName);
			rs = ps.executeQuery();

			if (rs.next()) {
				userInfo = new UserInfoBean();
				userInfo.setId(rs.getInt("id"));
				userInfo.setUserName(rs.getString("userName"));
				userInfo.setPassword(rs.getString("password"));
				userInfo.setQq(rs.getString("qq"));
				userInfo.setEmail(rs.getString("email"));
				userInfo.setHomepage(rs.getString("homepage"));
				userInfo.setAdmin(rs.getString("admin"));
			}
		} finally {

			if (rs != null) {
				try {
					rs.close();
				} catch (SQLException e) {
				}
			}

			if (ps != null) {
				try {
					ps.close();
				} catch (SQLException e) {
				}
			}

			if (conn != null) {
				try {
					conn.close();
				} catch (SQLException e) {
				}
			}
		}
		return userInfo;
	}

	public void addUserInfo(String userName, String password, String homepage,
			String qq, String email, String photo) throws SQLException {

		Connection conn = dataSource.getConnection();

		StringBuffer sql = new StringBuffer();
		sql.append("insert into userinfo ");
		sql.append("(userName, password, homepage, qq, email, photo) ");
		sql.append("values (?, ?, ?, ?, ?, ?)");

		PreparedStatement ps = null;
		try {
			ps = conn.prepareStatement(sql.toString());
			ps.setString(1, userName);
			ps.setString(2, password);
			ps.setString(3, homepage);
			ps.setString(4, qq);
			ps.setString(5, email);
			ps.setString(6, photo);
			ps.executeUpdate();
		} finally {

			if (ps != null) {
				try {
					ps.close();
				} catch (SQLException e) {
				}
			}

			if (conn != null) {
				try {
					conn.close();
				} catch (SQLException e) {
				}
			}
		}
	}

	public boolean isExistUser(String userName) throws SQLException {

		Connection conn = dataSource.getConnection();

		String sql = "select * from userinfo where username = ?";
		PreparedStatement ps = null;
		ResultSet rs = null;
		try {

			ps = conn.prepareStatement(sql);
			ps.setString(1, userName);
			rs = ps.executeQuery();
			if (rs.next()) {
				return true;
			}
		} finally {

			if (rs != null) {
				try {
					rs.close();
				} catch (SQLException e) {
				}
			}

			if (ps != null) {
				try {
					ps.close();
				} catch (SQLException e) {
				}
			}

			if (conn != null) {
				try {
					conn.close();
				} catch (SQLException e) {
				}
			}
		}
		return false;
	}

	/**
	 * 如果管理员用户名和密码和数据库中的用户信息匹配,则返回UserInfoBean, 否则返回null.
	 */
	public UserInfoBean authenticate(String userName, String password,
			String admin) throws SQLException {

		UserInfoBean userInfo = getUserInfo(userName, admin);

		if (userInfo != null && userInfo.getPassword().equals(password)) {
			return userInfo;
		}
		return null;
	}

	/**
	 * 从数据库中查询符合管理员用户名要求的记录把数据设置到UserInfoBean中并返回, 如果没有找到符合规则的记录则则返回null.
	 */
	public UserInfoBean getUserInfo(String userName, String admin)
			throws SQLException {

		UserInfoBean userInfo = null;

		Connection conn = dataSource.getConnection();

		String sql = "select * from userinfo where username = ? and admin = 1";
		PreparedStatement ps = null;
		ResultSet rs = null;
		try {
			ps = conn.prepareStatement(sql);
			ps.setString(1, userName);
			rs = ps.executeQuery();

			if (rs.next()) {
				userInfo = new UserInfoBean();
				userInfo.setId(rs.getInt("id"));
				userInfo.setUserName(rs.getString("userName"));
				userInfo.setPassword(rs.getString("password"));
				userInfo.setQq(rs.getString("qq"));
				userInfo.setEmail(rs.getString("email"));
				userInfo.setHomepage(rs.getString("homepage"));
				userInfo.setAdmin(rs.getString("admin"));
			}
		} finally {

			if (rs != null) {
				try {
					rs.close();
				} catch (SQLException e) {
				}
			}

			if (ps != null) {
				try {
					ps.close();
				} catch (SQLException e) {
				}
			}

			if (conn != null) {
				try {
					conn.close();
				} catch (SQLException e) {
				}
			}
		}
		return userInfo;
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -