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

📄 clabean.java

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

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

public class ClaBean {
	String sql;

	ResultSet rs = null;

	String cNum, cDept, cMteacher, cStunum, cMoney, cAttack;

	String colName, colValue, colValue2;

	int claId;

	// 添加班级信息
	public void claAdd(String num, String dept, String mteacher,
			String stunum, String money, String attack) {
		Database DB = new Database();
		this.cNum = num;
		this.cDept = dept;
		this.cMteacher = mteacher;
		this.cStunum = stunum;
		this.cMoney = money;
		this.cAttack = attack;
		if (cDept == null || cDept.equals("")) {
			JOptionPane.showMessageDialog(null, "请输入班级属院系", "错误",
					JOptionPane.ERROR_MESSAGE);
			return;
		} else {
			sql = "insert into Class values ('" + cNum + "','" + cDept
					+ "','" + cMteacher + "','" + cStunum + "','" + cMoney + "'," +
							"'" + cAttack + "')";
			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 claModify(String num, String dept, String mteacher,
			String stunum, String money, String attack) {
		Database DB = new Database();
		this.cNum = num;
		this.cDept = dept;
		this.cMteacher = mteacher;
		this.cStunum = stunum;
		this.cMoney = money;
		this.cAttack = attack;
		if (cDept == null || cDept.equals("")) {
			JOptionPane.showMessageDialog(null, "请输入班级姓名", "错误",
					JOptionPane.ERROR_MESSAGE);
			return;
		} else {
			sql = "update Class set cdept='" + cDept + "',cmteacher='" + cMteacher
					+ "',cstunum='" + cStunum + "',cmoney='" + cMoney
					+ "',cattack='" + cAttack+ "'," +
					" where tnum='" + cNum + "'";
		}
		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 claDel(String num) {
		Database DB = new Database();
		this.cNum = num;
		sql = "delete from Class where cnum='" + cNum + "'";
		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[] claSearch(String num) {
		Database DB = new Database();
		this.cNum = num;
		String[] s = new String[5];
		sql = "select * from Class where cnum='" + cNum + "'";
		try {
			DB.OpenConn();
			rs = DB.executeQuery(sql);
			if (rs.next()) {
				s[0] = rs.getString("cname");
				s[1] = rs.getString("cdept");
				s[2] = rs.getString("cmteacher");
				s[3] = rs.getString("cstunum");
				s[4] = rs.getString("cmoney");
				s[5] = rs.getString("attack");
			} else
				s = null;
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				DB.closeStmt();
				DB.closeConn();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return s;
	}

	// 班级信息综合查询(按照一个条件进行查询)
	public String[][] claAllSearch(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 Class";
		} else {
			sql = "select * from Class where " + colName + "='" + colValue
					+ "'";
		}
		try {
			DB.OpenConn();
			rs = DB.executeQuery(sql);
			if (rs.last()) {
				row = rs.getRow();
			}
			if (row == 0) {
				sn = null;
			} else {
				sn = new String[row][5];
				rs.first();
				rs.previous();
				while (rs.next()) {
					sn[i][0] = rs.getString("cnum");
					sn[i][1] = rs.getString("cdept");
					sn[i][2] = rs.getString("cmteacher");
					sn[i][3] = rs.getString("cstunum");
					sn[i][4] = rs.getString("cmoney");
					sn[i][5] = rs.getString("cattack");
					i++;
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				DB.closeStmt();
				DB.closeConn();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return sn;
	}

	// 班级信息综合查询(查询某范围的记录)
	public String[][] claAllSearch(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 Class 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][5];
				rs.first();
				rs.previous();
				while (rs.next()) {
					sn[i][0] = rs.getString("cnum");
					sn[i][1] = rs.getString("cdept");
					sn[i][2] = rs.getString("cmteacher");
					sn[i][3] = rs.getString("cstunum");
					sn[i][4] = rs.getString("cmoney");
					sn[i][5] = rs.getString("cattack");
					i++;
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				DB.closeStmt();
				DB.closeConn();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return sn;
	}

	// 获得新的班级
	public int getClaId() throws Exception {
		Database DB = new Database();
		sql = "select max(tnum) from Class";
		DB.OpenConn();
		rs = DB.executeQuery(sql);
		if (rs.next()) {
			String s = rs.getString(1);
			claId = Integer.parseInt(s.substring(6).trim()) + 1;
		} else
			claId = 1;
		DB.closeStmt();
		DB.closeConn();
		return claId;
	}

	// 获得class表中的所有学号tnum
	public String[] getAllId() throws Exception {
		String[] s = null;
		int row = 0, i = 0;
		Database DB = new Database();
		sql = "select tnum from class";
		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 + -