在线考试系统(四)——管理员bean+dao

在线考试系统(四)——管理员bean+dao

项目结构

在这里插入图片描述

adminbean

package pers.ant.oe.bean;

public class adminBean {
	private Integer pid;
	private String pwd;
	public Integer getPid() {
		return pid;
	}
	public void setPid(Integer pid) {
		this.pid = pid;
	}
	public String getPwd() {
		return pwd;
	}
	public void setPwd(String pwd) {
		this.pwd = pwd;
	}
	@Override
	public String toString() {
		return "adminBean [pid=" + pid + ", pwd=" + pwd + "]";
	}
	public adminBean(Integer pid, String pwd) {
		super();
		this.pid = pid;
		this.pwd = pwd;
	}	
}

admindao

package pers.ant.oe.dao;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;

import pers.ant.oe.bean.adminBean;
import pers.ant.oe.jdbc.jdbc;


/**
 * @author Administrator
 * @date 2019-11-26 08:05:01 
 */
public class adminDao {


	 /**
	 * @data  2019-11-26 
	 * @Description: TODO(查找主键是否存在,可用于验证)
	 * @param aid 主键
	 * @return adminBean对象 
	 */
	public static adminBean check(int aid) {
		adminBean bean = null;
		String sql = "select * from admin where aid= '" + aid + "';";
		Connection connection = jdbc.getConn();
		Statement statement = null;
		ResultSet resultSet = null;
		try {
			statement = connection.createStatement();
			resultSet = statement.executeQuery(sql);
			if (resultSet.next())
				bean = new adminBean(aid,resultSet.getString(1));
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			jdbc.close(connection, statement, resultSet);
		}
		return bean;
	}
	

	 /**
	 * @data  2019-11-26 
	 * @Description: TODO(验证登录)
	 * @param aid 账户名
	 * @param pwd 密码
	 * @return 返回登录成功的adminBean
	 */
	public static adminBean check(int aid,String pwd) {
		adminBean bean = null;
		String sql = "select * from admin where aid= '" + aid + "' and pwd='"+ pwd+"';";
		Connection connection = jdbc.getConn();
		Statement statement = null;
		ResultSet resultSet = null;
		try {
			statement = connection.createStatement();
			resultSet = statement.executeQuery(sql);
			if (resultSet.next())
				bean = new adminBean(aid,resultSet.getString(2));
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			jdbc.close(connection, statement, resultSet);
		}
		return bean;
	}
	

	 /**
	 * @data  2019-11-26 
	 * @Description: TODO(添加)
	 * @param aid 账户名
	 * @param pwd 密码
	 * @return
	 */
	public static boolean add(int aid, String pwd) {
		if(check(aid)!=null) {
			System.out.println("该管理员编号已存在");
			return false;
		}
		String sql = "insert  into admin values('" + aid + "','" + pwd + "');";
		Connection connection= jdbc.getConn();
		Statement statement = null;
		try {
			statement= connection.createStatement();
			statement.executeUpdate(sql);
		} catch (Exception e) {
			e.printStackTrace();
		}finally {
			jdbc.close(connection, statement, null);
		}
		return true;//添加成功
	}
	


	 /**
	 * @data  2019-11-26 
	 * @Description: TODO(测试)
	 * @param args
	 */
	public static void main(String []args) {
		int aid=1001;
		String pwdString="admin";
		if(add(aid,pwdString)) {
			System.out.println("添加成功");
		}	
		add(aid, pwdString);
		adminBean bean=check(1001,"admin");
		if(bean==null) {
			System.out.println("不存在该用户");
		}else {
			System.out.println(bean);
		}
		
	}
}
  • 1
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值