Java web項目 個人資金項目管理系統管理員部分代碼

package Thejava;


import java.sql.ResultSet;

public class Admin extends Conn{
	// 定義成員變量
	private int adminID;
	private String adminName;
	private String adminPassword;
	// 執行各種操作的SQL語句
	private String strSql;

	public Admin() throws ClassNotFoundException {
		adminID = 0;
		adminName = "";
		adminPassword = "";
		strSql = "";
	}
	/**
	 * @添加用戶記錄
	 */
	//insert into admin(name,password)values(adminName,adminPassword)
	public boolean addAdmin() {
		strSql = "insert into admin";
		strSql = strSql + "(";
		strSql = strSql + "name,";
		strSql = strSql + "password";
		strSql = strSql + ")";
		strSql = strSql + " values(";
		strSql = strSql + "'" + adminName + "',";
		strSql = strSql + "'" + adminPassword + "'";
		strSql = strSql + ")";
		boolean isAdd = super.AlterSql(strSql);
		return isAdd;
	}
	/**
	 * @判斷用戶名是否存在 by adminName
	 */
	public boolean isExist() {
		strSql = "select * from admin where name='"+adminName+"'";
		ResultSet rs = null;
		boolean isExist = false;
		try {
			rs = super.QuerySql(strSql);
			while(rs.next()) {
				isExist = true;
			}
		} catch (Exception e) {
			System.out.println(e.toString());
		}
		return isExist;
	}
	/**
	 * @判斷用戶名和密碼是否正確
	 */
	public boolean adminValid() {
		strSql = "select * from Admins where name='" + adminName + "' and password='" + adminPassword + "'";
		ResultSet rs = null;
		boolean isValid = false;
		try {
			rs = super.QuerySql(strSql);
			while (rs.next()) {
				this.adminID = rs.getInt("id");
				isValid = true;
			}
		} catch (Exception e) {
			System.out.println(e.toString());
		}
		return isValid;
	}
	/**
	 * @獲取某個用戶的信息 by id
	 */
	public boolean init() {
		strSql = "select * from admin where id=";
		strSql = strSql + adminID;
		try {
			ResultSet rs = super.QuerySql(strSql);
			if (rs.next()) {
				this.adminID = rs.getInt("id");
				this.adminName = rs.getString("name");
				this.adminPassword = rs.getString("password");
				return true;
			} else {
				return false;
			}
		} catch (Exception e) {
			System.out.println(e.toString());
			return false;
		}
	}
	/**
	 * @return the adminID
	 */
	public int getAdminID() {
		return adminID;
	}
	/**
	 * @param adminID the adminID to set
	 */
	public void setAdminID(int adminID) {
		this.adminID = adminID;
	}
	/**
	 * @return the adminName
	 */
	public String getAdminName() {
		return adminName;
	}
	/**
	 * @param adminName the adminName to set
	 */
	public void setAdminName(String adminName) {
		this.adminName = adminName;
	}
	/**
	 * @return the adminPassword
	 */
	public String getAdminPassword() {
		return adminPassword;
	}
	/**
	 * @param adminPassword the adminPassword to set
	 */
	public void setAdminPassword(String adminPassword) {
		this.adminPassword = adminPassword;
	}
	public static void main(String[] args) throws ClassNotFoundException {
		Admin admin = new Admin();
		admin.setAdminName("admin");
		admin.setAdminPassword("admin");
		boolean res = admin.adminValid();
		System.out.println(res + "");
	}
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章