?? userregisterbean.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 + -