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

📄 userproxy.java

📁 计算机技术的快速发展
💻 JAVA
字号:
package com.suninformation.user;

import com.suninformation.database.*;
import java.sql.*;
import com.suninformation.message.*;
//import com.suninformation.schoolmate.SMUser;

/**
 * @author 刘镇
 * 
 * 用户数据库操作代理
 */
public class UserProxy {

	private static final String USER_COUNT = "SELECT count(1) FROM puser";

	private static final String DELETE_USER = "DELETE FROM puser WHERE username=?";

	private static final String ALL_USERS = "SELECT username FROM puser";

	/**
	 * 创建用户
	 * 
	 * @param userName
	 *            String
	 * @param password
	 *            String
	 * @param realName
	 *            String
	 * @param question
	 *            String
	 * @param answer
	 *            String
	 * @param birthday
	 *            Date
	 * @param paperType
	 *            String
	 * @param paperNumber
	 *            String
	 * @param email
	 *            String
	 * @return user
	 * @throws UserAlreadyExistsException
	 * @throws UnacceptableException
	 */
	public User createUser(String userName, String password, String realName,
			Date birthday, int paperType, String paperNumber, String email)
			throws UserAlreadyExistsException, UnacceptableException {
		User us = new User(userName, password, paperType, paperNumber);
		new UserInfoProxy().createUserInfo(userName, realName, email, birthday);
		//new SecurityInfoProxy().createSecurityInfo(userName, email, password,
		//		question, answer);
		return us;
	}

	/**
	 * 获取用户实例
	 * 
	 * @param userName
	 *            String
	 * @return user
	 * @throws UserNotFoundException
	 * @throws UnacceptableException
	 */
	public User getUser(String userName) throws UserNotFoundException,
			UnacceptableException {
		return new User(userName);
	}

	/**
	 * 获取用户总数
	 * 
	 * @return int
	 * @throws UnacceptableException
	 */
	public int getUserCount() throws UnacceptableException {
		int count = -1;
		Connection conn = null;
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		try {
			conn = DBManager.getConnection();
			pstmt = conn.prepareStatement(USER_COUNT);
			rs = pstmt.executeQuery();
			if (rs.next()) {
				count = rs.getInt(1);
			}
		} catch (SQLException sqle) {

		} finally {
			DBManager.closeObject(conn, pstmt, rs);
		}
		return count;
	}

	/**
	 * 获取所有用户
	 * 
	 * @return user[]
	 * @throws UnacceptableException
	 */
	public User[] getUsers() throws UnacceptableException {
		Connection conn = null;
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		User[] iDBUser = null;
		int number = getUserCount();
		if (number >= 1) {
			iDBUser = new User[number];
			try {
				conn = DBManager.getConnection();
				pstmt = conn.prepareStatement(ALL_USERS);
				rs = pstmt.executeQuery();
				for (int i = 1; i <= number; i++) {
					iDBUser[i] = new User(rs.getString(1));
				}
			} catch (Exception e) {
				throw new UnacceptableException("数据库操作失败!", e);
			} finally {
				DBManager.closeObject(conn, pstmt, rs);
			}
		}
		return iDBUser;

	}

	/**
	 * 删除用户
	 * 
	 * @param userName
	 *            String
	 * @throws UserNotFoundException
	 * @throws UnacceptableException
	 */
	public void deleteUser(String userName) throws UserNotFoundException,
			UnacceptableException {
		Connection conn = null;
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		try {
			getUser(userName);
			try {
				conn = DBManager.getConnection();
				pstmt = conn.prepareStatement(DELETE_USER);
				pstmt.setString(1, userName);
				pstmt.execute();
			} catch (Exception e) {
				throw new UnacceptableException("删除用户数据失败!", e);
			} finally {
				DBManager.closeObject(conn, pstmt, rs);
			}
		} catch (UserNotFoundException unfe) {
			throw new UserNotFoundException("用户不存在。", unfe);
		}
	}

	/**
	 * 获取用户信息操作代理
	 * 
	 * @return UserInfoProxy
	 */
	public UserInfoProxy getUserInfoProxy() {
		return new UserInfoProxy();
	}

	/**
	 * 获取用户安全信息操作代理
	 * 
	 * @return SecurityInfoProxy
	 */
	public SecurityInfoProxy getSecurityInfoProxy() {
		return new SecurityInfoProxy();
	}

	/**
	 * 获取用户消息操作代理
	 * 
	 * @return UserMessageProxy
	 */
	public UserMessageProxy getUserMessageProxy() {
		return new UserMessageProxy();
	}

	/**
	 * 注册成为校友录成员
	 * 
	 * @param su
	 * @return
	 */
//	public boolean userSMRegister(SMUser su) {
//		return false;
//	}
}

⌨️ 快捷键说明

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