2022-12-04更新:
时隔上次更新已过去快两年,期间不少小伙伴私信问源代码啥的。因此在原版基础上修复,完成最终优化版,主要是代码优化和引入新的javaswing用法~,如有疑问欢迎评论区留言交流
(PS:所有java代码和SQL代码都在文章里!!!)
目录
题目:学生信息管理系统
1 系统需求分析
学校每年都有新生入学、老生毕业,还有其他各种人事变动。如何有效地管理这些学生的信息,帮助学校和老师掌握学生的情况,这就是学生信息管理系统需要完成的功能。
1.1 系统功能分析
本系统主要的功能是收集学生的个人信息,以便向教师提供每个学生在校的情况。
主要功能有:
学生个人信息输入,包括:姓名、性别、院系、生日、籍贯、生源所在地等。
学生流动情况的输入,包括:转系、休学、复学、退学、毕业。
奖惩情况的输入。
学生个人情况查询和修改,包括流动情况和奖罚情况。
1.2 系统功能模块设计(划分)
根据系统功能要求可以将系统分解成几个模块来分别设计应用程序界面,如图 1 所示。
1.3 与其它系统的关系
学生信息管理系统是校园信息管理系统的一个组成部分。它为其它系统,如班级信息管理系统、教学管理系统、成绩单系统、宿舍分配系统等,提供学生的基本情况。同时需要其他系统提供的班级设置和院系设置等情况。这些系统可以使用同一个数据库,直接相互调用所需的数据。
2 数据库设计
2.1 数据库需求分析
根据上一节的数据流程图,可以列出以下记录学生信息所需的数据项和数据结构:
学生:学号、姓名、性别、生日、籍贯、所在院系、所在班级。
处罚记录:记录号、级别、处罚对象、记录时间、详细描述、是否生效。
奖励记录:记录号、级别、奖励对象、记录时间、详细描述。
学籍变更记录:记录号、变更情况、记录对象、记录时间、详细描述。
所需的外部数据支持:
班级:班级编号、班级名称、所属院系。
院系:代码、名称。
2.2 数据库概念结构设计
本系统所需数据的 E-R 模型图
2.3 数据库逻辑结构设计
根据系统 E-R 图,需要设计 4 个数据表来存放学生的信息。为了系统的完整,系统中包括了应用程序设计中所需的 2 个外部数据表。为了优化数据表结构,部分字段采用代码形式,因此需要附加 3 个代码表。这 9 个数据表的结构如表 1 到表 9 所示。
2.4 数据库的建立
2.4.1 数据库的建立(请设计者完成)
2.4.2 初始数据的输入
数据表创建成功后,数据库中还没有实际的数据。为了保证外部键能使用,数据需要提前输入,如院系编号、班级编号、学籍变更代码和奖惩级别等等。
学籍变更代码和奖惩级别的主关键字 CODE 从 0 开始用阿拉伯数字依次编号,便于程序设计时,采用单选按钮来选择。
本系统中所用到的学籍变更和奖惩级别代码如表 10 至表 12 所示(请设计者完成这些数据表的逻辑结构)。
表10 学籍变更代码表
代码 说明
0 转系
1 休学
2 复学
3 退学
4 毕业
表11 奖励级别代码
代码 说明
0 校特等奖学金
1 校一等奖学金
2 校二等奖学金
3 校三等奖学金
4 系一等奖学金
5 系二等奖学金
6 系三等奖学金
表12 处罚级别代码
代码 说明
0 警告
1 严重警告
2 记过
3 记大过
4 开除
3 各功能模块的设计与实现
3.1 功能说明
1、学生个人信息输入
2、学籍变更情况的输入
3、奖励情况的输入
4、处罚情况的输入
5、学生个人情况查询和修改
3.2 用户界面设计
完成数据库创建和功能说明以后,我们可以进行下一步工作,即设计用户界面。由于本系统管理内容比较简单,且为校园管理信息系统的一部分,因此使用一个窗体不同的标签页来完成系统要求的 5 项功能。
我们把学生信息管理系统的窗体分成____个主要部分,如图____所示。
1、应用程序主窗体的创建
2、学生个人信息输入界面
3、学籍变更情况输入界面
4、奖励情况输入界面
5、处罚情况输入界面
6、学生个人情况查询和修改界面
3.3 各功能模块的实现
1、学生个人信息输入
2、学籍变更情况输入
3、奖励情况输入
4、处罚情况输入
5、学生个人情况查询和修改
——————————————- 以上是题目 ——————————————-
4、界面、数据库和源代码
本管理系统主要分为 登录和选项卡面板 两个模块,将多个界面放在选项卡面板,每个界面完成不同的功能,分别是学生个人情况查询修改、学生信息输入、处罚情况/学籍变更/奖励信息输入
湖科大的可⊕ⓠ1119094861
4.1、表字段和建表脚本
以下是数据库表结构,q2数据库下一共有9个表
数据表创建成功后,数据库中还没有实际的数据。为了保证外部键能使用,数据需要提前输入,如院系编号、班级编号、学籍变更代码和奖惩级别等等
change表和change_code表:
class和department表:
reward和reward_levels表:
student表
——2020/12/28更新原因:不少学弟学妹要源码包的,补充如下数据库的创建代码
首先必须使用如下命令先建库!(库名用q1,保持一致,若需修改则上面的代码里也要变动):
create database q1;
之后直接运行如下脚本(进入数据库复制粘贴或使用数据库工具运行都可)
/*
Navicat MySQL Data Transfer
Source Server : ZTY
Source Server Version : 50528
Source Host : localhost:3306
Source Database : infodb
Target Server Type : MYSQL
Target Server Version : 50528
File Encoding : 65001
Date: 2021-01-09 16:13:35
*/
SET FOREIGN_KEY_CHECKS=0;
-- ----------------------------
-- Table structure for chang
-- ----------------------------
DROP TABLE IF EXISTS `chang`;
CREATE TABLE `chang` (
`ID` int(11) DEFAULT NULL,
`STUDENTID` int(11) DEFAULT NULL,
`CHAN` int(11) DEFAULT NULL,
`REC_TIME` varchar(15) DEFAULT NULL,
`DSRPIN` varchar(15) DEFAULT NULL,
KEY `SI_SI` (`STUDENTID`),
KEY `chan_code` (`CHAN`)
) ENGINE=InnoDB DEFAULT CHARSET=gbk;
-- ----------------------------
-- Records of chang
-- ----------------------------
INSERT INTO `chang` VALUES ('3', '3', '2', '2020/12/31', '复学');
INSERT INTO `chang` VALUES ('4', '4', '5', '2020/12/31', '转学');
INSERT INTO `chang` VALUES ('5', '5', '5', '2020/12/31', '转学');
INSERT INTO `chang` VALUES ('6', '6', '3', '2020/12/30', '退学');
INSERT INTO `chang` VALUES ('7', '7', '4', '2020/12/30', '复学');
INSERT INTO `chang` VALUES ('8', '8', '4', '2020/12/29', '复学');
INSERT INTO `chang` VALUES ('9', '9', '1', '2020/2/28', ' 休学');
INSERT INTO `chang` VALUES ('2', '2', '0', '2020/12/31', '转专业');
INSERT INTO `chang` VALUES ('1', '1', '0', '2020/12/31', '转专业');
INSERT INTO `chang` VALUES ('10', '10', '3', '2020/2/27', '退学');
-- ----------------------------
-- Table structure for change_code
-- ----------------------------
DROP TABLE IF EXISTS `change_code`;
CREATE TABLE `change_code` (
`CODE` int(11) NOT NULL,
`DSRPIN` varchar(15) DEFAULT NULL,
PRIMARY KEY (`CODE`)
) ENGINE=InnoDB DEFAULT CHARSET=gbk;
-- ----------------------------
-- Records of change_code
-- ----------------------------
INSERT INTO `change_code` VALUES ('0', '转系');
INSERT INTO `change_code` VALUES ('1', '休学');
INSERT INTO `change_code` VALUES ('2', '复学');
INSERT INTO `change_code` VALUES ('3', '退学');
INSERT INTO `change_code` VALUES ('4', '毕业');
INSERT INTO `change_code` VALUES ('5', '转学');
-- ----------------------------
-- Table structure for class
-- ----------------------------
DROP TABLE IF EXISTS `class`;
CREATE TABLE `class` (
`ID` int(11) NOT NULL,
`NAME` varchar(10) DEFAULT NULL,
`MONITOR` int(11) DEFAULT NULL,
PRIMARY KEY (`ID`),
KEY `ID_index` (`ID`),
KEY `MONITOR_SI` (`MONITOR`),
CONSTRAINT `MONITOR_SI` FOREIGN KEY (`MONITOR`) REFERENCES `student` (`STUDENTID`)
) ENGINE=InnoDB DEFAULT CHARSET=gbk;
-- ----------------------------
-- Records of class
-- ----------------------------
INSERT INTO `class` VALUES ('1', '一班', null);
INSERT INTO `class` VALUES ('2', '二班', null);
INSERT INTO `class` VALUES ('3', '三班', null);
INSERT INTO `class` VALUES ('4', '四班', null);
INSERT INTO `class` VALUES ('5', '五班', null);
INSERT INTO `class` VALUES ('6', '六班', null);
INSERT INTO `class` VALUES ('7', '七班', null);
-- ----------------------------
-- Table structure for department
-- ----------------------------
DROP TABLE IF EXISTS `department`;
CREATE TABLE `department` (
`ID` int(11) NOT NULL,
`NAME` varchar(10) DEFAULT NULL,
PRIMARY KEY (`ID`)
) ENGINE=InnoDB DEFAULT CHARSET=gbk;
-- ----------------------------
-- Records of department
-- ----------------------------
INSERT INTO `department` VALUES ('1', '资安');
INSERT INTO `department` VALUES ('2', '土木');
INSERT INTO `department` VALUES ('3', '机电');
INSERT INTO `department` VALUES ('4', '计科');
INSERT INTO `department` VALUES ('5', '化工');
INSERT INTO `department` VALUES ('6', '物电');
INSERT INTO `department` VALUES ('7', '人文');
INSERT INTO `department` VALUES ('8', '外国语');
INSERT INTO `department` VALUES ('9', '马克思');
INSERT INTO `department` VALUES ('10', '体育');
-- ----------------------------
-- Table structure for punishment
-- ----------------------------
DROP TABLE IF EXISTS `punishment`;
CREATE TABLE `punishment` (
`ID` int(11) DEFAULT NULL,
`STUDENTID` int(11) DEFAULT NULL,
`LEVELS` int(11) DEFAULT NULL,
`REC_TIME` varchar(15) DEFAULT NULL,
`ENABLE` varchar(4) DEFAULT NULL,
`DSRPIN` varchar(15) DEFAULT NULL,
KEY `LEVELS_CODETWO` (`LEVELS`),
KEY `SI_SIthree` (`STUDENTID`),
CONSTRAINT `LEVELS_CODETWO` FOREIGN KEY (`LEVELS`) REFERENCES `punish_levels` (`CODE`),
CONSTRAINT `SI_SIthree` FOREIGN KEY (`STUDENTID`) REFERENCES `student` (`STUDENTID`)
) ENGINE=InnoDB DEFAULT CHARSET=gbk;
-- ----------------------------
-- Records of punishment
-- ----------------------------
INSERT INTO `punishment` VALUES ('1', '6', '2', '2020/12/30', '是', '逃课');
INSERT INTO `punishment` VALUES ('2', '9', '2', '2020/12/30', '是', '发表不当言论');
INSERT INTO `punishment` VALUES ('3', '10', '4', '2020/12/30', '是', '考试作弊');
-- ----------------------------
-- Table structure for punish_levels
-- ----------------------------
DROP TABLE IF EXISTS `punish_levels`;
CREATE TABLE `punish_levels` (
`CODE` int(11) NOT NULL,
`DSRPIN` varchar(15) DEFAULT NULL,
PRIMARY KEY (`CODE`)
) ENGINE=InnoDB DEFAULT CHARSET=gbk;
-- ----------------------------
-- Records of punish_levels
-- ----------------------------
INSERT INTO `punish_levels` VALUES ('0', '警告');
INSERT INTO `punish_levels` VALUES ('1', '严重警告');
INSERT INTO `punish_levels` VALUES ('2', '记过');
INSERT INTO `punish_levels` VALUES ('3', '记大过');
INSERT INTO `punish_levels` VALUES ('4', '开除');
-- ----------------------------
-- Table structure for reward
-- ----------------------------
DROP TABLE IF EXISTS `reward`;
CREATE TABLE `reward` (
`ID` int(11) DEFAULT NULL,
`STUDENTID` int(11) DEFAULT NULL,
`LEVELS` int(11) DEFAULT NULL,
`REC_TIME` varchar(15) DEFAULT NULL,
`DSRPIN` varchar(15) DEFAULT NULL,
KEY `LEVELS_CODE` (`LEVELS`),
KEY `SI_SItwo` (`STUDENTID`),
KEY `DSRPIN` (`DSRPIN`)
) ENGINE=InnoDB DEFAULT CHARSET=gbk;
-- ----------------------------
-- Records of reward
-- ----------------------------
INSERT INTO `reward` VALUES ('2', '2', '1', '2020/12/30', '校编程比赛');
INSERT INTO `reward` VALUES ('3', '3', '2', '2020/12/30', '一校一书');
INSERT INTO `reward` VALUES ('4', '4', '3', '2020/12/30', '新生写作赛');
INSERT INTO `reward` VALUES ('5', '5', '4', '2020/12/30', '新生才艺赛');
INSERT INTO `reward` VALUES ('6', '7', '6', '2020/12/30', '新生辩论赛');
INSERT INTO `reward` VALUES ('7', '8', '5', '2020/12/30', '新生书画赛');
INSERT INTO `reward` VALUES ('1', '1', '0', '2020/12/30', '发表SCI论文');
-- ----------------------------
-- Table structure for reward_levels
-- ----------------------------
DROP TABLE IF EXISTS `reward_levels`;
CREATE TABLE `reward_levels` (
`CODE` int(11) NOT NULL,
`DSRPIN` varchar(15) DEFAULT NULL,
PRIMARY KEY (`CODE`)
) ENGINE=InnoDB DEFAULT CHARSET=gbk;
-- ----------------------------
-- Records of reward_levels
-- ----------------------------
INSERT INTO `reward_levels` VALUES ('0', '校特等奖学金');
INSERT INTO `reward_levels` VALUES ('1', '校一等奖学金');
INSERT INTO `reward_levels` VALUES ('2', '校二等奖学金');
INSERT INTO `reward_levels` VALUES ('3', '校三等奖学金');
INSERT INTO `reward_levels` VALUES ('4', '系一等奖学金');
INSERT INTO `reward_levels` VALUES ('5', '系二等奖学金');
INSERT INTO `reward_levels` VALUES ('6', '系三等奖学金');
-- ----------------------------
-- Table structure for student
-- ----------------------------
DROP TABLE IF EXISTS `student`;
CREATE TABLE `student` (
`STUDENTID` int(11) NOT NULL,
`NAME` char(10) NOT NULL,
`SEX` char(2) NOT NULL,
`CLASS` int(11) NOT NULL,
`DEPARTMENT` int(11) DEFAULT NULL,
`BIRTHDAY` varchar(15) DEFAULT NULL,
`NATIVE_PLACE` varchar(10) DEFAULT NULL,
PRIMARY KEY (`STUDENTID`),
KEY `SI_C` (`CLASS`),
KEY `department_id` (`DEPARTMENT`)
) ENGINE=InnoDB DEFAULT CHARSET=gbk;
-- ----------------------------
-- Records of student
-- ----------------------------
INSERT INTO `student` VALUES ('1', '赵伊', '女', '1', '10', '2001.1.1', '湖南长沙');
INSERT INTO `student` VALUES ('2', '钱程', '男', '2', '9', '2001.2.2', '江西南昌');
INSERT INTO `student` VALUES ('3', '孙三', '男', '3', '8', '2001.3.3', '黑龙江哈尔滨');
INSERT INTO `student` VALUES ('4', '李四', '男', '4', '7', '2001.4.14', '河北石家庄');
INSERT INTO `student` VALUES ('5', '周五', '男', '5', '6', '2001.5.4', '山西太原');
INSERT INTO `student` VALUES ('6', '吴太和', '女', '6', '5', '2001.6.1', '内蒙古呼和浩特');
INSERT INTO `student` VALUES ('7', '郑七', '男', '7', '4', '2001.7.1', '吉林长春');
INSERT INTO `student` VALUES ('8', '王力宏', '男', '1', '3', '2001.8.1', '江苏南京');
INSERT INTO `student` VALUES ('9', '冯绍峰', '男', '2', '2', '2001.9.10', '浙江杭州');
INSERT INTO `student` VALUES ('10', '陈芊芊', '女', '3', '1', '2001.10.1', '安徽合肥');
4.2、源代码(3个文件)
4.2.1、login.java
package com.ZTY;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
/**
* @Author: pumpkin
* @Time: 2021-01-09 Hunan University of Science and Technology
* @Changes:
* 1、增加了登录跳转功能
* 2、学生个人情况查询和修改部分,增加了查询、删除记录和可展示的面板
*/
public class login implements ActionListener {
// 定义主窗口
private final JFrame jf;
// 定义输入用户名和密码的标签提示
private final JLabel InputUserName;
private final JLabel InputPassWord;
// 定义输入用户名文本框
private final JTextField UserName;
// 定义输入密码框
private final JPasswordField PassWord;
// 定义登录和取消按钮
private final JButton Login;
private final JButton Cancel;
login() {
// 各组件实例化过程
jf = new JFrame("学生信息管理系统");
InputUserName = new JLabel("账号(123)");
InputPassWord = new JLabel("密码(123):");
UserName = new JTextField();
PassWord = new JPasswordField();
Login = new JButton("登录");
Cancel = new JButton("退出");
// 设置主窗口大小、位置和布局
jf.setSize(400, 150);
jf.setLocation(400, 400);
// 设置窗口流式布局
jf.setLayout(new FlowLayout());
// 设置用户名和密码框大小
UserName.setPreferredSize(new Dimension(300, 30));
PassWord.setPreferredSize(new Dimension(300, 30));
// 依次向主窗口添加各组件
jf.getContentPane().add(InputUserName);
jf.getContentPane().add(UserName);
jf.getContentPane().add(InputPassWord);
jf.getContentPane().add(PassWord);
jf.getContentPane().add(Login);
jf.getContentPane().add(Cancel);
// 设置主窗口不可调节大小
jf.setResizable(false);
// 设置主窗口默认关闭操作
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// 给登录和取消按钮添加 Action 监听器
Login.addActionListener(this);
Cancel.addActionListener(this);
// 设置主窗口可见
jf.setVisible(true);
}
@Override
public void actionPerformed(ActionEvent e) {
// 如果单击【退出】按钮则程序退出
if (e.getSource().equals(Cancel)) {
System.exit(0);
}
// 如果单击【登录】按钮则检查用户名和密码是否匹配
else if (e.getSource().equals(Login)) {
// 如果用户名和密码匹配,则打开具体操作面板
if (UserName.getText().equals("123") && String.valueOf(PassWord.getPassword()).equals("123")) {
jf.setVisible(false);
jf.dispose();
QUESTIONONE2 hh= new QUESTIONONE2();
}
// 如果用户名和密码不匹配,则给出提示对话框
else {
JOptionPane.showOptionDialog(jf, "用户名或密码错误", "登陆失败",
JOptionPane.CLOSED_OPTION,
JOptionPane.ERROR_MESSAGE, null, null, null);
}
}
}
// 启动登录界面
public static void main(String[] args) {
new login();
}
}
4.2.2、QUESTIONONE2.java
package com.ZTY;
import javax.swing.*;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Vector;
/**
* @Author: ZTY
* @Time: 2022/12/4 20:22 Zhejiang Dahua Technology Co., Ltd
* @Changes:
* 1、Optimize lines of code,
* 2、Add BoxLayout to reduce the sub-panel-num of JTabbedPane from 5 to 3
*/
public class QUESTIONONE2 extends JFrame{
JPanel jap,jap2,jap3,jap4,jap5,jap7,jap8,jap9;
JLabel jab1,jab2,jab3,jab4,jab5,jab6,jab7,jab9;
JPanel jap1,jap6;
JTextField JaTF1,JaTF2,JaTF3,JaTF4,JaTF5,JaTF6,JaTF7;
JButton jab;
JPanel jbp,jbp1,jbp2,jbp3,jbp4,jbp5;
JTextField JbTF1,JbTF2,JbTF4,JbTF5;
JLabel jbb1,jbb2,jbb4,jbb5;
JButton jbb;
JPanel jcp,jcp1,jcp2,jcp3,jcp4,jcp5;
JTextField JcTF1,JcTF2,JcTF3,JcTF4,JcTF5;
JLabel jcb1,jcb2,jcb3,jcb4,jcb5;
JButton jcb;
JPanel jdp,jdp1,jdp2,jdp3,jdp4,jdp5,jdp6;
JTextField JdTF1,JdTF2,JdTF3,JdTF4,JdTF5,JdTF6;
JLabel jdb1,jdb2,jdb3,jdb4,jdb5,jdb6;
JButton jdb;
JPanel jep,jep1,jep2,jep3,jep4,jep5,jep6,jep7,jep8,jep9,jep10,jep11,jep12,jep13;
JTextField JeTF1,JeTF3,JeTF4,JeTF5,JeTF6,JeTF7,JeTF8,JeTF9,JeTF10,JeTF11,JeTF12,JeTF13;
JLabel jeb1,jeb4,jeb5,jeb6,jeb7,jeb8,jeb9,jeb10,jeb11,jeb12,jeb13;
JButton JeB1,JeB2;
JTabbedPane JTP;
JTable studentJTable = null;
JScrollPane studentJScrollPane = null;
Vector studentVector = null;
Vector titleVector = null;
JButton jBQueryAll = null;
JButton jBDeleteCurrentRecord = null;
JButton jBDeleteAllRecords = null;
private static DbProcess2 dbProcess=new DbProcess2();
String SelectQueryFieldStr="";
public QUESTIONONE2 (){
JComboBox<String>jCBSelectQueryField = new JComboBox<String>();//查询字段
jCBSelectQueryField.addItem("学号");
jCBSelectQueryField.addItem("姓名");
jCBSelectQueryField.addItem("性别");
jCBSelectQueryField.addItem("班级");
jCBSelectQueryField.addItem("院系");
jCBSelectQueryField.addItem("生日");
jCBSelectQueryField.addItem("住址");
jCBSelectQueryField.addItemListener(new ItemListener() {//下拉框事件监听
public void itemStateChanged(ItemEvent event) {
switch (event.getStateChange()) {
case ItemEvent.SELECTED:
SelectQueryFieldStr = (String) event.getItem();
System.out.println("选中:" + SelectQueryFieldStr);
break;
case ItemEvent.DESELECTED:
System.out.println("取消选中:" + event.getItem());
break;
}
}
});
jap=new JPanel(new GridLayout(9,1));//return jap;
jap1=new JPanel();
JaTF1=new JTextField(8);
jab1 = new JLabel("学号");
jab1.setFont(new Font(null,Font.PLAIN,20));
jap1.add(jab1);
jap1.add(JaTF1);
jap2=new JPanel();
JaTF2=new JTextField(8);
jab2 = new JLabel("姓名");
jab2.setFont(new Font(null,Font.PLAIN,20));
jap2.add(jab2);
jap2.add(JaTF2);
jap3=new JPanel();
JaTF3=new JTextField(8);
jab3 = new JLabel("性别");
jab3.setFont(new Font(null,Font.PLAIN,20));
jap3.add(jab3);
jap3.add(JaTF3);
JComboBox<String> jCBSexField = new JComboBox<String>();
jCBSexField.addItem("男");
jCBSexField.addItem("女");
jCBSexField.addItemListener(new ItemListener() {//下拉框事件监听
public void itemStateChanged(ItemEvent event) {
String SexFieldStr;
switch (event.getStateChange()) {
case ItemEvent.SELECTED:
SexFieldStr = (String) event.getItem();
System.out.println("选中:" + SexFieldStr);
JaTF3.setText(jCBSexField.getSelectedItem().toString());
break;
case ItemEvent.DESELECTED:
System.out.println("取消选中:" + event.getItem());
break;
}
}
});
jap3.add(jCBSexField);
jap4=new JPanel();
JaTF4=new JTextField(8);
jab4 = new JLabel("生日");
jab4.setFont(new Font(null,Font.PLAIN,20));
jap4.add(jab4);
jap4.add(JaTF4);
jap5=new JPanel();
JaTF5=new JTextField(8);
jab5 = new JLabel("籍贯");
jab5.setFont(new Font(null,Font.PLAIN,20));
jap5.add(jab5);
jap5.add(JaTF5);
jap6=new JPanel();
JaTF6=new JTextField(8);
jab6 = new JLabel("院系");
jab6.setFont(new Font(null,Font.PLAIN,20));
jap6.add(jab6);
jap6.add(JaTF6);
jap7=new JPanel();
JaTF7=new JTextField(8);
jab7 = new JLabel("班级");
jab7.setFont(new Font(null,Font.PLAIN,20));
jap7.add(jab7);
jap7.add(JaTF7);
jap8=new JPanel();
jab=new JButton("输入");
jab.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
if(e.getActionCommand().equals("输入")
&&!JaTF1.getText().isEmpty()
&&!JaTF2.getText().isEmpty()
&&!JaTF3.getText().isEmpty()
&&!JaTF4.getText().isEmpty()
&&!JaTF5.getText().isEmpty()
&&!JaTF6.getText().isEmpty()
&&!JaTF7.getText().isEmpty())
{
System.out.println("actionPerformed(). 输入");
String sNo=JaTF1.getText().trim();
String sName=JaTF2.getText().trim();
String sSex=JaTF3.getText().trim();
String sBir=JaTF4.getText().trim();
String sHom=JaTF5.getText().trim();
String sDep=JaTF6.getText().trim();
String sCla=JaTF7.getText().trim();
String sql = "insert into student(STUDENTID,NAME,SEX,CLASS,DEPARTMENT,BIRTHDAY,NATIVE_PLACE) values(";
sql = sql + sNo + ",'"+ sName + "','" + sSex + "'," + sCla + "," + sDep + ",'" + sBir + "','" + sHom + "')";
System.out.println(sql);
new DbProcess2().executeUpdate(sql);
}
}
});
jap9=new JPanel();
jBQueryAll = new JButton("查询所有记录");
jBDeleteCurrentRecord = new JButton("删除当前记录");
jBDeleteAllRecords = new JButton("删除所有记录");
studentVector = new Vector();
titleVector = new Vector();
// 定义表头
titleVector.add("学号");
titleVector.add("姓名");
titleVector.add("性别");
titleVector.add("班级");
titleVector.add("院系");
titleVector.add("生日");
titleVector.add("住址");
//studentTableModel = new DefaultTableModel(tableTitle, 15);
studentJTable = new JTable(studentVector, titleVector);
studentJTable.setPreferredScrollableViewportSize(new Dimension(500,160));
studentJScrollPane = new JScrollPane(studentJTable);
//分别设置水平和垂直滚动条自动出现
studentJScrollPane.setHorizontalScrollBarPolicy(
JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
studentJScrollPane.setVerticalScrollBarPolicy(
JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
jBQueryAll.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e){
System.out.println("actionPerformed(). 查询所有记录");
queryAllProcess();
}
});
jBDeleteCurrentRecord.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e){
System.out.println("actionPerformed(). 删除当前记录");
deleteCurrentRecordProcess();
}
});
jBDeleteAllRecords.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e){
System.out.println("actionPerformed(). 删除所有记录");
deleteAllRecordsProcess();
}
});
Dimension preferredSize = new Dimension(150,20);
jab.setPreferredSize(preferredSize);
jap8.add(jab);
jab9=new JLabel("作者:18计科ZTY 交流+Q:1119094861");
jap9=new JPanel(new FlowLayout());//布局
jap9.add(jab9);
jap.add(jap1);jap.add(jap2);jap.add(jap3);jap.add(jap4);
jap.add(jap5);jap.add(jap6);jap.add(jap7);jap.add(jap8);
jap.add(jap9);
jbp=new JPanel();//return jbp;
jbp1=new JPanel();jbp2=new JPanel();jbp3=new JPanel();jbp4=new JPanel();jbp5=new JPanel();
JbTF1=new JTextField(8);
jbb1=new JLabel("记录号");
jbb1.setFont(new Font(null,Font.PLAIN,18));
jbp1.add(jbb1);jbp1.add(JbTF1);
JbTF2=new JTextField(10);
jbb2=new JLabel("学号");
jbb2.setFont(new Font(null,Font.PLAIN,18));
jbp2.add(jbb2);jbp2.add(JbTF2);
JTextField JbTF3=new JTextField(5);
JLabel jbb3=new JLabel("变更代码");
jbb3.setFont(new Font(null,Font.PLAIN,18));
jbp3.add(jbb3);jbp3.add(JbTF3);
JbTF4=new JTextField(7);
jbb4=new JLabel("记录时间");
jbb4.setFont(new Font(null,Font.PLAIN,18));
jbp4.add(jbb4);jbp4.add(JbTF4);
JbTF5=new JTextField(7);
jbb5=new JLabel("描述");
jbb5.setFont(new Font(null,Font.PLAIN,18));
jbp5.add(jbb5);jbp5.add(JbTF5);
jbb=new JButton("输入");
Dimension preferredSize2 = new Dimension(60,18);
jbb.setPreferredSize(preferredSize2);
jbp5.add(jbb);
jbp.add(jbp1);jbp.add(jbp2);jbp.add(jbp3);jbp.add(jbp4);jbp.add(jbp5);
jbb.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
if(e.getActionCommand().equals("输入")
&&!JbTF1.getText().isEmpty() &&!JbTF2.getText().isEmpty()
&&!JbTF3.getText().isEmpty() &&!JbTF4.getText().isEmpty()
&&!JbTF5.getText().isEmpty())
{
System.out.println("actionPerformed(). 输入");
String ID=JbTF1.getText().trim();
String SI=JbTF2.getText().trim();
String CE=JbTF3.getText().trim();
String R_T=JbTF4.getText().trim();
String DSRPIN=JbTF5.getText().trim();
String sql = "insert into chang(ID,STUDENTID,CHAN,REC_TIME,DSRPIN) values(";
sql=sql +ID+','+SI+','+CE+','+"'"+R_T+"'"+ ','+"'"+DSRPIN+"')";
System.out.println(sql);
new DbProcess2().executeUpdate(sql);
}
}
});
jcp=new JPanel();//return jcp;
jcp1=new JPanel();jcp2=new JPanel();jcp3=new JPanel();jcp4=new JPanel();jcp5=new JPanel();
JcTF1=new JTextField(8);
jcb1=new JLabel("记录号");
jcb1.setFont(new Font(null,Font.PLAIN,18));
jcp1.add(jcb1);jcp1.add(JcTF1);
JcTF2=new JTextField(10);
jcb2=new JLabel("学号");
jcb2.setFont(new Font(null,Font.PLAIN,18));
jcp2.add(jcb2);jcp2.add(JcTF2);
JcTF3=new JTextField(5);
jcb3=new JLabel("级别代码");
jcb3.setFont(new Font(null,Font.PLAIN,18));
jcp3.add(jcb3);jcp3.add(JcTF3);
JcTF4=new JTextField(7);
jcb4=new JLabel("记录时间");
jcb4.setFont(new Font(null,Font.PLAIN,18));
jcp4.add(jcb4);jcp4.add(JcTF4);
JcTF5=new JTextField(7);
jcb5=new JLabel("描述");
jcb5.setFont(new Font(null,Font.PLAIN,18));
jcp5.add(jcb5);jcp5.add(JcTF5);
jcb=new JButton("输入");
Dimension preferredSize3 = new Dimension(60,18);
jcb.setPreferredSize(preferredSize3);
jcp5.add(jcb);
jcp.add(jcp1);jcp.add(jcp2);jcp.add(jcp3);jcp.add(jcp4);jcp.add(jcp5);
jcb.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
if(e.getActionCommand().equals("输入")
&&!JcTF1.getText().isEmpty() &&!JcTF2.getText().isEmpty()
&&!JcTF3.getText().isEmpty() &&!JcTF4.getText().isEmpty()
&&!JcTF5.getText().isEmpty())
{
System.out.println("actionPerformed(). 输入");
String ID=JcTF1.getText().trim();
String SI=JcTF2.getText().trim();
String LEVELS=JcTF3.getText().trim();
String R_T=JcTF4.getText().trim();
String DSRPIN=JcTF5.getText().trim();
String sql = "insert into reward(ID,STUDENTID,LEVELS,REC_TIME,DSRPIN) values(";
sql=sql +ID+','+SI+','+LEVELS+','+"'"+R_T+"'"+ ','+"'"+DSRPIN+"')";
System.out.println(sql);
new DbProcess2().executeUpdate(sql);
}
}
});
jdp=new JPanel();// return jdp;
jdp1=new JPanel();jdp2=new JPanel();jdp3=new JPanel();jdp4=new JPanel();jdp5=new JPanel();jdp6=new JPanel();
JdTF1=new JTextField(8);
jdb1=new JLabel("记录号");
jdb1.setFont(new Font(null,Font.PLAIN,18));
jdp1.add(jdb1);jdp1.add(JdTF1);
JdTF2=new JTextField(10);
jdb2=new JLabel("学号");
jdb2.setFont(new Font(null,Font.PLAIN,18));
jdp2.add(jdb2);jdp2.add(JdTF2);
JdTF3=new JTextField(5);
jdb3=new JLabel("级别代码");
jdb3.setFont(new Font(null,Font.PLAIN,18));
jdp3.add(jdb3);jdp3.add(JdTF3);
JdTF4=new JTextField(7);
jdb4=new JLabel("记录时间");
jdb4.setFont(new Font(null,Font.PLAIN,18));
jdp4.add(jdb4);jdp4.add(JdTF4);
JdTF5=new JTextField(7);
jdb5=new JLabel("描述");
jdb5.setFont(new Font(null,Font.PLAIN,18));
jdp5.add(jdb5);jdp5.add(JdTF5);
JdTF6=new JTextField(3);
jdb6=new JLabel("是否生效");
jdb6.setFont(new Font(null,Font.PLAIN,18));
jdp6.add(jdb6);jdp6.add(JdTF6);
jdb=new JButton("输入");
Dimension preferredSize4 = new Dimension(60,18);
jdb.setPreferredSize(preferredSize4);
jdp.add(jdp1);jdp.add(jdp2);jdp.add(jdp3);jdp.add(jdp4);jdp.add(jdp5);jdp.add(jdp6);jdp.add(jdb);
jdb.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
if(e.getActionCommand().equals("输入")
&&!JdTF1.getText().isEmpty() &&!JdTF2.getText().isEmpty()
&&!JdTF3.getText().isEmpty() &&!JdTF4.getText().isEmpty()
&&!JdTF5.getText().isEmpty() &&!JdTF6.getText().isEmpty())
{
System.out.println("actionPerformed(). 输入");
String ID=JdTF1.getText().trim();
String SI=JdTF2.getText().trim();
String LEVELS=JdTF3.getText().trim();
String R_T=JdTF4.getText().trim();
String DSRPIN=JdTF5.getText().trim();
String ENABLE=JdTF6.getText().trim();
String sql = "insert into punishment(ID,STUDENTID,LEVELS,REC_TIME,ENABLE,DSRPIN) values(";
sql=sql +ID+','+SI+','+LEVELS+','+"'"+R_T+"'"+ ','+ "'" + ENABLE + "',"+"'"+DSRPIN+"')";
System.out.println(sql);
new DbProcess2().executeUpdate(sql);
}//输入级别为0-4之间不能超过4
}
});
Box vBox = Box.createVerticalBox();
JLabel JL1 = new JLabel("处罚");
JL1.setFont(new Font("宋体",Font.BOLD,16));
vBox.add(JL1);
vBox.add(jdp);
JLabel JL2 = new JLabel("学籍");
JL2.setFont(new Font("宋体",Font.BOLD,16));
vBox.add(JL2);
vBox.add(jbp);
JLabel JL3 = new JLabel("奖励");
JL3.setFont(new Font("宋体",Font.BOLD,16));
vBox.add(JL3);
vBox.add(jcp);
vBox.add(Box.createVerticalGlue()); // 添加一个水平方向胶状的不可见组件,撑满剩余水平空间
jep=new JPanel();// return jep;
jep1=new JPanel();jep2=new JPanel();jep3=new JPanel();jep4=new JPanel();
jep5=new JPanel();jep6=new JPanel();jep7=new JPanel();jep8=new JPanel();
jep9=new JPanel();jep10=new JPanel();jep11=new JPanel();jep12=new JPanel();
jep13=new JPanel();
JeTF1=new JTextField(8);
jeb1=new JLabel("");
jeb1.setFont(new Font(null,Font.PLAIN,20));
jep1.add(jeb1);
jep1.add(jCBSelectQueryField);
jep1.add(JeTF1);
JeB1=new JButton("查询");
Dimension preferredSize5 = new Dimension(136,20);
JeB1.setPreferredSize(preferredSize5);
JeB1.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
if(e.getActionCommand().equals("查询")&&!JeTF1.getText().trim().isEmpty())
{
String sql=JeTF1.getText().trim();
queryall(sql);
queryProcess(sql);
}
}
});
jep2.add(JeB1);
JeB2=new JButton("修改");
jep3.add(JeB2);
JeTF4=new JTextField(8);
jeb4=new JLabel("学籍变动");
jeb4.setFont(new Font(null,Font.PLAIN,20));
jep4.add(jeb4);jep4.add(JeTF4);
JeTF5=new JTextField(8);
jeb5=new JLabel("记录时间");
jeb5.setFont(new Font(null,Font.PLAIN,20));
jep5.add(jeb5);jep5.add(JeTF5);
JeTF6=new JTextField(8);
jeb6=new JLabel("描述");
jeb6.setFont(new Font(null,Font.PLAIN,20));
jep6.add(jeb6);jep6.add(JeTF6);
JeTF7=new JTextField(8);
jeb7=new JLabel("奖励");
jeb7.setFont(new Font(null,Font.PLAIN,20));
jep7.add(jeb7);jep7.add(JeTF7);
JeTF8=new JTextField(8);
jeb8=new JLabel("记录时间");
jeb8.setFont(new Font(null,Font.PLAIN,20));
jep8.add(jeb8);jep8.add(JeTF8);
JeTF9=new JTextField(8);
jeb9=new JLabel("描述");
jeb9.setFont(new Font(null,Font.PLAIN,20));
jep9.add(jeb9);jep9.add(JeTF9);
JeTF10=new JTextField(4);
jeb10=new JLabel("处罚");
jeb10.setFont(new Font(null,Font.PLAIN,20));
jep10.add(jeb10);jep10.add(JeTF10);
JeTF11=new JTextField(3);
jeb11=new JLabel("记录时间");
jeb11.setFont(new Font(null,Font.PLAIN,20));
jep11.add(jeb11);jep11.add(JeTF11);
JeTF12=new JTextField(8);
jeb12=new JLabel("描述");
jeb12.setFont(new Font(null,Font.PLAIN,20));
jep12.add(jeb12);jep12.add(JeTF12);
JeTF13=new JTextField(2);
jeb13=new JLabel("是否生效");
jeb13.setFont(new Font(null,Font.PLAIN,20));
jep13.add(jeb13);jep13.add(JeTF13);
Dimension preferredSize6 = new Dimension(136,20);
JeB2.setPreferredSize(preferredSize6);
JeB2.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
if(e.getActionCommand().equals("修改")&&!JeTF1.getText().trim().isEmpty())
{
String sql=JeTF1.getText().trim();
changeall(sql);
queryall(sql);
}
}
});
jep.add(jep1);jep.add(jep2);jep.add(jep3);
jep.add(jep4);jep.add(jep5);jep.add(jep6);
jep.add(jep7);jep.add(jep8);jep.add(jep9);
jep.add(jep10);jep.add(jep11);jep.add(jep12);jep.add(jep13);
jep.add(studentJScrollPane);//
jep.add(jBQueryAll);
jep.add(jBDeleteCurrentRecord);
jep.add(jBDeleteAllRecords);
JTP=new JTabbedPane();
JTP.addTab("学生个人情况查询和修改",jep);
JTP.addTab("学生个人信息输入",jap);
JTP.addTab("处罚情况、学籍变更、奖励信息输入",vBox);
// JTP.addTab("学籍变更情况输入界面",jbp);
// JTP.addTab("奖励信息输入界面",jcp);
JTP.addChangeListener(new ChangeListener() {
@Override
public void stateChanged(ChangeEvent e) {
System.out.println("当前选中的选项卡: " + JTP.getSelectedIndex());
}
});
// 设置默认选中的选项卡
JTP.setSelectedIndex(0);
this.add(JTP);
this.setLayout(new GridLayout(2, 1));
this.setContentPane(JTP);
this.setSize(550,450);
this.setLocation(400, 180);
this.setTitle("学生信息管理系统");
this.setBackground(Color.blue);
this.setVisible(true);
this.setResizable(false);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void queryall(String sql)
{
String s1="select change_code.DSRPIN,chang.REC_TIME,chang.DSRPIN as REASON from chang join change_code on change_code.CODE = chang.CHAN";
s1=s1+" where "+jCBSelectQueryFieldTransfer(SelectQueryFieldStr)+" = "+sql;
String s2="select reward_levels.DSRPIN,reward.REC_TIME,reward.DSRPIN as REASON from reward join reward_levels on reward_levels.CODE=reward.LEVELS";
s2=s2+" where "+jCBSelectQueryFieldTransfer(SelectQueryFieldStr)+" = "+sql;
String s3="select punish_levels.DSRPIN,punishment.REC_TIME,punishment.DSRPIN as REASON,punishment.ENABLE from punishment join punish_levels on punish_levels.CODE=punishment.LEVELS";
s3=s3+" where "+jCBSelectQueryFieldTransfer(SelectQueryFieldStr)+" = "+sql;
try{
dbProcess.connect();
dbProcess.sta=dbProcess.con.createStatement();
ResultSet RS1=dbProcess.sta.executeQuery(s1);
while(RS1.next())
{
String P1=RS1.getString("DSRPIN");
String P2=RS1.getString("REC_TIME");
String P3=RS1.getString("REASON");
System.out.println(P1);System.out.println(P2);System.out.println(P3);
JeTF4.setText(P1);JeTF5.setText(P2);JeTF6.setText(P3);
}
}
catch(SQLException ex) {
System.err.println(ex.getMessage());
}
dbProcess.disconnect();
try {
dbProcess.connect();
dbProcess.sta=dbProcess.con.createStatement();
ResultSet RS1=dbProcess.sta.executeQuery(s2);
while(RS1.next())
{
String P4=RS1.getString("DSRPIN");
String P5=RS1.getString("REC_TIME");
String P6=RS1.getString("REASON");
System.out.println(P4);System.out.println(P5);System.out.println(P6);
JeTF7.setText(P4);JeTF8.setText(P5);JeTF9.setText(P6);
}
}
catch(SQLException ex) {
System.err.println(ex.getMessage());
}
dbProcess.disconnect();
try {
dbProcess.connect();
dbProcess.sta=dbProcess.con.createStatement();
ResultSet RS1=dbProcess.sta.executeQuery(s3);
while(RS1.next())
{
String P7=RS1.getString("DSRPIN");
String P8=RS1.getString("REC_TIME");
String P9=RS1.getString("REASON");
String P10=RS1.getString("ENABLE");
System.out.println(P7);System.out.println(P8);System.out.println(P9);
JeTF10.setText(P7);JeTF11.setText(P8);JeTF12.setText(P9);JeTF13.setText(P10);
}
}
catch(SQLException ex) {
System.err.println(ex.getMessage());
}
dbProcess.disconnect();
}
public void queryProcess(String sQueryField)
{
try{
// 建立查询条件
String sql = "select * from student where ";
String queryFieldStr = jCBSelectQueryFieldTransfer(SelectQueryFieldStr);
if(queryFieldStr.equals("STUDENTID")||queryFieldStr.equals("CLASS")||queryFieldStr.equals("DEPARTMENT")){
sql = sql + queryFieldStr;
sql = sql + " = " + sQueryField;
}else{
sql = sql + queryFieldStr;
sql = sql + " = ";
sql = sql + "'" + sQueryField + "';";
}
System.out.println("queryProcess(). sql = " + sql);
dbProcess.connect();
ResultSet rs = dbProcess.executeQuery(sql);
// 将查询获得的记录数据,转换成适合生成JTable的数据形式
studentVector.clear();
while(rs.next()){
Vector v = new Vector();
v.add(Integer.valueOf(rs.getInt("STUDENTID")));
v.add(rs.getString("NAME"));
v.add(rs.getString("SEX"));
v.add(Integer.valueOf(rs.getInt("CLASS")));
v.add(Integer.valueOf(rs.getInt("DEPARTMENT")));
v.add(rs.getString("BIRTHDAY"));
v.add(rs.getString("NATIVE_PLACE"));
studentVector.add(v);
}
studentJTable.updateUI();
dbProcess.disconnect();
}catch(SQLException sqle){
System.out.println("sqle = " + sqle);
JOptionPane.showMessageDialog(null,
"数据操作错误","错误",JOptionPane.ERROR_MESSAGE);
}catch(Exception e){
System.out.println("e = " + e);
JOptionPane.showMessageDialog(null,
"数据操作错误","错误",JOptionPane.ERROR_MESSAGE);
}
}
public void changeall(String sql)
{
String changchan=JeTF4.getText();
String changtime=JeTF5.getText();
String changedes=JeTF6.getText();
String rew=JeTF7.getText();
String rewtime=JeTF8.getText();
String rewdesc=JeTF9.getText();
String pun=JeTF10.getText();
String puntime=JeTF11.getText();
String pundesc=JeTF12.getText();
String punfun=JeTF13.getText();
String d1="update chang set CHAN=(select a.CHAN from (select CHAN,CODE from chang,change_code where chang.CHAN=change_code.CODE AND chang_code.DSRPIN ="+changchan+" group by CHAN)as a),"
+"REC_TIME ='"+changtime+"',"+"DSRPIN ='"+changedes+"' "
+"where STUDENTID"+" = "+sql;
System.out.println(d1);
String d2="update reward set LEVELS=(select b.LEVELS from (select LEVELS,CODE from reward,reward_levels where reward.LEVELS=reward_levels.CODE AND reward_levels.DSRPIN ='"+rew+"' group by LEVELS)as b),"
+"REC_TIME ='"+rewtime+"' ,"+"DSRPIN ='"+rewdesc+"' "
+"where STUDENTID"+" = "+sql;
System.out.println(d2);
String d3="update punishment set LEVELS=(select c.LEVELS from(select LEVELS,CODE from punishment,punish_levels where punishment.LEVELS=punish_levels.CODE AND punish_levels.DSRPIN ='"+pun+"' group by LEVELS)as c),"
+"REC_TIME ='"+puntime+"',"+"DSRPIN ='"+pundesc+"' ,"+"ENABLE ='"+punfun+"' "
+"where STUDENTID"+" = "+sql;
System.out.println(d3);
try {
dbProcess.connect();
dbProcess.sta=dbProcess.con.createStatement();
dbProcess.sta.execute(d1);
JeTF4.setText("");JeTF5.setText("");JeTF6.setText("");
}
catch(SQLException ex) {
System.err.println(ex.getMessage());
}
dbProcess.disconnect();
try {
dbProcess.connect();
dbProcess.sta=dbProcess.con.createStatement();
dbProcess.sta.execute(d2);
JeTF7.setText("");JeTF8.setText("");JeTF9.setText("");
}
catch(SQLException ex) {
System.err.println(ex.getMessage());
}
dbProcess.disconnect();
try {
dbProcess.connect();
dbProcess.sta=dbProcess.con.createStatement();
dbProcess.sta.execute(d3);
JeTF10.setText("");JeTF11.setText("");JeTF12.setText("");JeTF13.setText("");
}
catch(SQLException ex) {
System.err.println(ex.getMessage());
}
dbProcess.disconnect();
}
public void queryAllProcess()
{
try{
// 建立查询条件
String sql = "select * from student;";
System.out.println("queryAllProcess(). sql = " + sql);
dbProcess.connect();
ResultSet rs = dbProcess.executeQuery(sql);
// 将查询获得的记录数据,转换成适合生成JTable的数据形式
studentVector.clear();
while(rs.next()){
Vector v = new Vector();
v.add(Integer.valueOf(rs.getInt("STUDENTID")));
v.add(rs.getString("NAME"));
v.add(rs.getString("SEX"));
v.add(Integer.valueOf(rs.getInt("CLASS")));
v.add(Integer.valueOf(rs.getInt("DEPARTMENT")));
v.add(rs.getString("BIRTHDAY"));
v.add(rs.getString("NATIVE_PLACE"));
studentVector.add(v);
}
studentJTable.updateUI();
dbProcess.disconnect();
}catch(SQLException sqle){
System.out.println("sqle = " + sqle);
JOptionPane.showMessageDialog(null,
"数据操作错误","错误",JOptionPane.ERROR_MESSAGE);
}
}
public void deleteCurrentRecordProcess()
{
String sNo = JeTF1.getText().trim();
// 建立删除条件
String sql = "delete from student where "+jCBSelectQueryFieldTransfer(SelectQueryFieldStr)+"='" + sNo + "';";
System.out.println("deleteCurrentRecordProcess(). sql = " + sql);
try{
if (dbProcess.executeUpdate1(sql) < 1) {
System.out.println("deleteCurrentRecordProcess(). delete database failed.");
}
}catch(Exception e){
System.out.println("e = " + e);
JOptionPane.showMessageDialog(null,
"数据操作错误","错误",JOptionPane.ERROR_MESSAGE);
}
queryAllProcess();
}
public void deleteAllRecordsProcess()
{
// 建立删除条件
String sql = "delete from student;";
System.out.println("deleteAllRecordsProcess(). sql = " + sql);
try{
if (dbProcess.executeUpdate1(sql) < 1) {
System.out.println("deleteAllRecordsProcess(). delete database failed.");
}
}catch(Exception e){
System.out.println("e = " + e);
JOptionPane.showMessageDialog(null,
"数据操作错误","错误",JOptionPane.ERROR_MESSAGE);
}
queryAllProcess();
}
public String jCBSelectQueryFieldTransfer(String InputStr)
{
String outputStr = "";
System.out.println("jCBSelectQueryFieldTransfer(). InputStr = " + InputStr);
if(InputStr.equals("学号")){
outputStr = "STUDENTID";
}else if(InputStr.equals("姓名")){
outputStr = "NAME";
}else if(InputStr.equals("性别")){
outputStr = "SEX";
}else if(InputStr.equals("班级")){
outputStr = "CLASS";
}else if(InputStr.equals("院系")){
outputStr = "DEPARTMENT";
}else if(InputStr.equals("生日")){
outputStr = "BIRTHDAY";
}else if(InputStr.equals("住址")){
outputStr = "NATIVE_PLACE";
}
System.out.println("jCBSelectQueryFieldTransfer(). outputStr = " + outputStr);
return outputStr;
}
}
4.2.3、DbProcess2.java
package com.ZTY;
import java.sql.*;
/**
* @Author: ZTY
* @Time: 2020-02-05
*/
public class DbProcess2{
public Connection con = null;public Statement sta=null;
ResultSet rs = null;
static final String JDBC_DRIVER = "com.mysql.cj.jdbc.Driver";
//注意:下行3306后面的q1为我建立的数据库名,要改成自己建立的数据库名!!!
static final String DB_URL = "jdbc:mysql://localhost:3306/q2?useSSL=false&serverTimezone=UTC";
//数据库账号密码,也要改为自己的
static final String USER = "root";static final String PASS = "2000918131X";
public DbProcess2(){
try {
//Mysql数据库设置驱动程序类型
Class.forName(JDBC_DRIVER);
System.out.println("连接数据库...");
System.out.println("mysql数据库驱动加载成功");
}
catch(ClassNotFoundException e){
e.printStackTrace();
}
}
public void executeUpdate(String sql){
connect();
try{
sta=con.createStatement();
sta.execute(sql);
}
catch(SQLException ex) {
System.err.println(ex.getMessage());
}
disconnect();
}
public void connect(){
try{
con = DriverManager.getConnection(DB_URL,USER,PASS);
if(con!=null){
System.out.println("数据库连接成功");
}
}
catch(Exception e){
e.printStackTrace();
}
}
public void disconnect(){
try{
if(con != null){
System.out.println("数据库断开成功");
con.close();
con = null;
}
}
catch(Exception e){
e.printStackTrace();
}
}
public ResultSet executeQuery(String sql) {
try {
System.out.println("executeQuery(). sql = " + sql);
PreparedStatement pstm = con.prepareStatement(sql);
// ??в??
rs = pstm.executeQuery();
}
catch(SQLException ex) {
ex.printStackTrace();
}
return rs;
}
public int executeUpdate1(String sql) {
int count = 0;
connect();
try {
Statement stmt = con.createStatement();
count = stmt.executeUpdate(sql);
}
catch(SQLException ex) {
System.err.println(ex.getMessage());
}
disconnect();
return count;
}
}
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
如需转载请保留出处:https://bianchenghao.cn/34458.html