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

📄 stubean.java

📁 班级管理系统
💻 JAVA
字号:
package lab.lab;

import java.util.*;
import java.sql.*;
import javax.swing.*;

public class StuBean {
	String sql;

	ResultSet rs = null;

	String sNum, sName, sSex, sBirth, sHome, sEthnic, sYear, sMajor, sCollege;

	String colName, colValue, colValue2;

	int stuId;

	// 添加学生信息
	public void stuAdd(String num, String name, String sex, String birth,
			String home, String ethnic, String year, String major,
			String college) {
		Database DB = new Database();
		this.sNum = num;
		this.sName = name;
		this.sSex = sex;
		this.sBirth = birth;
		this.sHome = home;
		this.sEthnic = ethnic;
		this.sYear = year;
		this.sMajor = major;
		this.sCollege = college;
		if (sName == null || sName.equals("")) {
			JOptionPane.showMessageDialog(null, "请输入学生姓名", "错误",
					JOptionPane.ERROR_MESSAGE);
			return;
		} else {
			sql = "insert into student values ('" + sNum + "','" + sName
					+ "','" + sSex + "','" + sEthnic + "','" + sHome + "','"
					+ sYear + "','" + sMajor + "','" + sCollege + "','"
					+ sBirth + "')";
			try {
				DB.OpenConn();
				DB.executeUpdate(sql);
				JOptionPane.showMessageDialog(null, "成功添加一条新的纪录");
			} catch (Exception e) {
				e.printStackTrace();
				JOptionPane.showMessageDialog(null, "保存失败", "错误",
						JOptionPane.ERROR_MESSAGE);
			} finally {
				try {
					DB.closeStmt();
					DB.closeConn();
				} catch (Exception e) {
					e.printStackTrace();
				}
			}

		}

	}

	// 修改学生信息
	public void stuModify(String num, String name, String sex, String birth,
			String home, String ethnic, String year, String major,
			String college) {
		Database DB = new Database();
		this.sNum = num;
		this.sName = name;
		this.sSex = sex;
		this.sBirth = birth;
		this.sHome = home;
		this.sEthnic = ethnic;
		this.sYear = year;
		this.sMajor = major;
		this.sCollege = college;
		if (sName == null || sName.equals("")) {
			JOptionPane.showMessageDialog(null, "请输入学生姓名", "错误",
					JOptionPane.ERROR_MESSAGE);
			return;
		} else {
			sql = "update student set sname='" + sName + "',ssex='" + sSex
					+ "',sethnic='" + sEthnic + "',shome='" + sHome
					+ "',syear='" + sYear + "',smajor='" + sMajor
					+ "',scollege='" + sCollege + "',sbirth='" + sBirth
					+ "' where snum='" + sNum + "'";
		}
		try {
			DB.OpenConn();
			DB.executeUpdate(sql);
			JOptionPane.showMessageDialog(null, "成功修改一条新的纪录");
		} catch (Exception e) {
			e.printStackTrace();
			JOptionPane.showMessageDialog(null, "更新失败", "错误",
					JOptionPane.ERROR_MESSAGE);
		} finally {
			try {
				DB.closeStmt();
				DB.closeConn();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}

	// 删除学生信息

	public void stuDel(String num) {
		Database DB = new Database();
		this.sNum = num;
		sql = "delete from student where snum='" + sNum + "'";
		try {
			DB.OpenConn();
			DB.executeUpdate(sql);
			JOptionPane.showMessageDialog(null, "成功删除一条新的纪录");
		} catch (Exception e) {
			e.printStackTrace();
			JOptionPane.showMessageDialog(null, "删除失败", "错误",
					JOptionPane.ERROR_MESSAGE);
		} finally {
			try {
				DB.closeStmt();
				DB.closeConn();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}

	// 根据学号查询学生信息

	public String[] stuSearch(String num) {
		Database DB = new Database();
		this.sNum = num;
		String[] s = new String[8];
		sql = "select * from student where snum='" + sNum + "'";
		try {
			DB.OpenConn();
			rs = DB.executeQuery(sql);
			if (rs.next()) {
				s[0] = rs.getString("sname");
				s[1] = rs.getString("ssex");
				s[2] = rs.getString("sethnic");
				s[3] = rs.getString("shome");
				s[4] = rs.getString("syear");
				s[5] = rs.getString("smajor");
				s[6] = rs.getString("scollege");
				s[7] = rs.getString("sbirth");
			} else
				s = null;
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				DB.closeStmt();
				DB.closeConn();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return s;
	}

	// 学生信息综合查询(按照一个条件进行查询)
	public String[][] stuAllSearch(String colname, String colvalue)
			throws Exception {
		this.colName = colname;
		this.colValue = colvalue;
		Database DB = new Database();
		String[][] sn = null;
		int row = 0, i = 0;
		Database.toGBK(colvalue);
		if (colValue == null || colValue.equals("")) {
			sql = "select * from student";
		} else {
			sql = "select * from student where " + colName + "='" + colValue
					+ "'";
		}
		try {
			DB.OpenConn();
			rs = DB.executeQuery(sql);
			try{
			if (rs.last()) {
				row = rs.getRow();
			}}catch(Exception e){}
			if (row == 0) {
				sn = null;
			} else {
				sn = new String[row][9];
				rs.first();
				rs.previous();
				while (rs.next()) {
					sn[i][0] = rs.getString("snum");
					sn[i][1] = rs.getString("sname");
					sn[i][2] = rs.getString("ssex");
					sn[i][3] = rs.getString("sethnic");
					sn[i][4] = rs.getString("shome");
					sn[i][5] = rs.getString("syear");
					sn[i][6] = rs.getString("smajor");
					sn[i][7] = rs.getString("scollege");
					sn[i][8] = rs.getString("sbirth");
					i++;
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				DB.closeStmt();
				DB.closeConn();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return sn;
	}

	// 学生信息综合查询(查询某范围的记录)
	public String[][] stuAllSearch(String colname, String colvalue,
			String colvalue2) {
		this.colName = colname;
		this.colValue = colvalue;
		this.colValue2 = colvalue2;
		Database DB = new Database();
		String[][] sn = null;
		int row = 0, i = 0;
		sql = "select * from student where " + colName + " between " + colValue
				+ " and " + colValue2;
		try {
			DB.OpenConn();
			rs = DB.executeQuery(sql);
			if (rs.last()) {
				row = rs.getRow();
			}
			if (row == 0) {
				sn = null;
			} else {
				sn = new String[row][9];
				rs.first();
				rs.previous();
				while (rs.next()) {
					sn[i][0] = rs.getString("snum");
					sn[i][1] = rs.getString("sname");
					sn[i][2] = rs.getString("ssex");
					sn[i][3] = rs.getString("sethnic");
					sn[i][4] = rs.getString("shome");
					sn[i][5] = rs.getString("syear");
					sn[i][6] = rs.getString("smajor");
					sn[i][7] = rs.getString("scollege");
					sn[i][8] = rs.getString("sbirth");
					i++;
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
			//	DB.closeStmt();
			//	DB.closeConn();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return sn;
	}

	// 获得新的学号
	public int getStuId() throws Exception {
		Database DB = new Database();
		sql = "select max(snum) from student";
		DB.OpenConn();
		rs = DB.executeQuery(sql);
		if (rs.next()) {
			String s = rs.getString(1);
			stuId = Integer.parseInt(s.substring(6).trim()) + 1;
		} else
			stuId = 1;
		DB.closeStmt();
		DB.closeConn();
		return stuId;
	}

	// 获得student表中的所有学号snum
	public String[] getAllId() throws Exception {
		String[] s = null;
		int row = 0, i = 0;
		Database DB = new Database();
		sql = "select snum from student";
		DB.OpenConn();
		rs = DB.executeQuery(sql);
		if (rs.last()) {
			row = rs.getRow();
		}
		if (row == 0) {
			s = null;
		} else {
			s = new String[row];
			rs.first();
			rs.previous();
			while (rs.next()) {
				s[i++] = rs.getString(1);
			}
		}
		DB.closeStmt();
		DB.closeConn();
		return s;
	}
}

⌨️ 快捷键说明

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