package dao;

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

import model.*;

public class LoginJdbc {
	Connection conn = null;
	Statement st = null;
	ResultSet rs = null;

	Manager man = null;
	Teacher tea = null;
	Student stu = null;

	String driver = "com.mysql.jdbc.Driver"; // 加载驱动
	String url = "jdbc:mysql://localhost:3306/selectcourse?useUnicode=true&characterEncoding=utf8";
	String user = "root";
	String password = "root";

	// 管理员登录
	public Manager login(int id) {
		String sql = "select * from manager where id=" + id;
		try {
			Class.forName(driver);
			conn = DriverManager.getConnection(url, user, password); // 获取与数据库的连接
			st = conn.createStatement();// 获取用于向数据库发送SQL语句
			rs = st.executeQuery(sql); // 向数据库发送SQL语句,并获取代表结果集
			while (rs.next()) {
				int mid = rs.getInt(1);
				String name = rs.getString(2);
				String pwd = rs.getString(3);
				int age = rs.getInt(4);
				man = new Manager(mid, name, pwd, age);
			}

		} catch (Exception e) {
			close();
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return man;
	}

	// 教师登录
	public Teacher tealogin(int id) {
		String sql = "select * from teacher where id=" + id;
		try {
			Class.forName(driver);
			conn = DriverManager.getConnection(url, user, password); // 获取与数据库的连接
			st = conn.createStatement();// 获取用于向数据库发送SQL语句
			rs = st.executeQuery(sql); // 向数据库发送SQL语句,并获取代表结果集
			while (rs.next()) {
				int tid = rs.getInt(1);
				String name = rs.getString(2);
				String pwd = rs.getString(3);
				String title = rs.getString(4);
				String department = rs.getString(5);
				tea = new Teacher(tid, name, pwd, title, department);
			}

		} catch (Exception e) {
			close();
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return tea;
	}

	// 学生登录
	public Student stulogin(int id) {
		String sql = "select * from student where id=" + id;
		try {
			Class.forName(driver);//加载驱动
			conn = DriverManager.getConnection(url, user, password); // 获取与数据库的连接
			st = conn.createStatement();// 获取用于向数据库发送SQL语句
			rs = st.executeQuery(sql); // 向数据库发送SQL语句,并获取代表结果集
			while (rs.next()) {
				int sid = rs.getInt(1);
				String name = rs.getString(2);
				String pwd = rs.getString(3);
				String bj = rs.getString(4);
				String home = rs.getString(5);
				int phone = rs.getInt(6);
				String email = rs.getString(7);

				stu = new Student(sid, name, pwd, bj, home, phone, email);
			}

		} catch (Exception e) {
			close();
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return stu;
	}

	public void close() {
		try {
			if (rs != null)
				rs.close();
			if (st != null)
				st.close();
			if (conn != null)
				conn.close();
		} catch (SQLException e) {

			e.printStackTrace();
		}
	}

}
最近下载更多
tmf852  LV5 2023年6月10日
Tayirjan  LV11 2023年3月23日
263648  LV7 2022年6月23日
ouyun12345  LV4 2022年5月17日
微信网友_5764743416303616  LV6 2022年4月25日
kuoluoluo  LV3 2022年4月18日
朱朱啊哈  LV16 2022年3月4日
123123123W  LV7 2022年2月28日
CCCbbbcai  LV6 2021年12月23日
3089559272  LV11 2021年12月17日
最近浏览更多
周青松5 1月15日
暂无贡献等级
杨豫川  LV12 1月12日
minjing123  LV8 1月4日
颜菜菜  LV2 1月4日
Nuyoah_L 1月3日
暂无贡献等级
pangzhihui  LV11 2023年12月20日
微信网友_6786215447367680  LV4 2023年12月20日
CCCCWWWW  LV4 2023年12月11日
3320151533  LV1 2023年12月7日
2010160433  LV3 2023年11月30日
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友