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

📄 userdao.java

📁 呜呜呜呜呜呜呜呜呜呜呜呜呜呜呜呜呜呜呜4444444444444444444
💻 JAVA
字号:
/**
 * -----------------------------------------------------------------------------
 * 创建时间:2006年2月9日
 * 作    者:孙丰伟
 * 功	 能:系统帐号(用户)管理,系统对用户的管理主要有(参看案例分析中的用例图):
 * 			1.建立新用户,    method: saveAccount()
 * 			2.修改密码,	     method: changePassword()
 * 			3.取消用户,      method: removeAccountById()
 * 			4.浏览用户,	     method: getAccounts()
 * 			5.确认用户与密码,method: isCorrectAccount()
 * 			6.已存在的用户,  method: isExistentAccount()
 * ------------------------------------------------------------------------------
 */
package cn.sunfengwei.employee.model;

import java.sql.*;
import java.util.Collection;
import java.util.Vector;

import cn.sunfengwei.employee.db.ConnectDB;


public class UserDAO {
	private UserDTO account;

	private Connection con;

	private PreparedStatement pstmt;

	private Statement stmt;

	private ResultSet rs;

	public UserDAO() {

	}
/**
 * 功能:
 * 参数说明:
 * 返回值:
 * 算法:
 */
	//如果帐号不存在,则新建一个用户
	public boolean saveAccount(UserDTO account) {
		boolean flag = false;
		con = ConnectDB.getConnection();
		String strQuery = "insert into account values(?,?)";
		try {
			pstmt = con.prepareStatement(strQuery);
			pstmt.setString(1, account.getUserName());
			pstmt.setString(2, account.getPassword());
			if (pstmt.executeUpdate() > 0)
				flag = true;
			pstmt.close();
			con.close();
		} catch (SQLException e) {
			// TODO 自动生成 catch 块
			e.printStackTrace();
		}//end of catch
		return flag;
	}  // end of method saveAccount

	//修改原有帐号密码
	public boolean changePassword(UserDTO account) {
		boolean flag = false;
		con = ConnectDB.getConnection();
		String strQuery = "update account set password=? where userName=?";
		try {
			pstmt = con.prepareStatement(strQuery);
			pstmt.setString(1, account.getPassword());
			pstmt.setString(2, account.getUserName());
			if (pstmt.executeUpdate() > 0)
				flag = true;
			pstmt.close();
			con.close();
		} catch (SQLException e) {
			// TODO 自动生成 catch 块
			e.printStackTrace();
		}
		return flag;
	}

	//移除帐号
	public boolean removeAccountById(String userName) {
		boolean flag = false;
		con = ConnectDB.getConnection();
		String strQuery = "delete from account where userName=?";
		try {
			pstmt = con.prepareStatement(strQuery);
			
			pstmt.setString(1, userName);
			if (pstmt.executeUpdate() > 0)
				flag = true;
			pstmt.close();
			con.close();
		} catch (SQLException e) {
			// TODO 自动生成 catch 块
			e.printStackTrace();
		}
		return flag;
	}

	//是否是正确的可用帐号与密码
	public boolean isCorrectAccount(UserDTO account) {
		boolean flag = false;
		con = ConnectDB.getConnection();
		String strQuery = "select count(*) from account where userName=? and password=?";
		
		try {
			pstmt = con.prepareStatement(strQuery);
			pstmt.setString(1, account.getUserName());
			pstmt.setString(2, account.getPassword());
			rs=pstmt.executeQuery();
			rs.next();
			if(rs.getInt(1)>0)
				flag = true;
			rs.close();
			pstmt.close();
			con.close();
		} catch (SQLException e) {
			// TODO 自动生成 catch 块
			e.printStackTrace();
		}
		return flag;
	}

	//返回所有的帐号,Collection是Vector的接口
	public Vector<UserDTO> getAccounts(UserDTO account) {
		java.util.Vector<UserDTO> accounts=new Vector<UserDTO>();
		con = ConnectDB.getConnection();
		//用到了like 运算符
		String strQuery = "select * from account where userName like ? and password like ?";
		try {
			pstmt = con.prepareStatement(strQuery);
			pstmt.setString(1, "%"+account.getUserName()+"%");   //%admin%  %%
			pstmt.setString(2, "%"+account.getPassword()+"%"); //%%
			rs=pstmt.executeQuery();
			while(rs.next())
			{
			account=new UserDTO();
			account.setUserName(rs.getString(1)); //从结果集中取出userName值 
			account.setPassword(rs.getString(2));
			//System.out.println(account.getUserName());
			accounts.add(account);
			}
			rs.close();
			pstmt.close();
			con.close();
		} catch (SQLException e) {
			// TODO 自动生成 catch 块
			e.printStackTrace();
		}
		return accounts;
	}
//	是否是已有的帐号
	public boolean isExistedAccount(String userName) {
		boolean flag = false;
		con = ConnectDB.getConnection();
		String strQuery = "select count(*) from account where userName=?";
		
		try {
			pstmt = con.prepareStatement(strQuery);
			pstmt.setString(1, userName);
			rs=pstmt.executeQuery();
			rs.next();
			if(rs.getInt(1)>0)
				flag = true;
			rs.close();
			pstmt.close();
			con.close();
		} catch (SQLException e) {
			// TODO 自动生成 catch 块
			//e.printStackTrace();
		}
		return flag;
	}

}

⌨️ 快捷键说明

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