第一章  需求分析

1.课程设计的目的:

通过本次综合实训,使学生完成以下目标:

(1)掌握数据库的基础知识,具有分析和解决数据库领域复杂工程问题的能力;

(2)能基于数据库技术的专业知识,具备针对复杂数据库应用系统开展需求分析、方案设计与优化、物理实现和综合评价等工程实践活动的能力;

(3)能基于数据库设计的基本理念,具备在数据库应用工程项目开发中合理划分设计模块、并撰写相关设计文档的能力;

(4)能及时跟踪数据库领域及相关行业发展状况,针对复杂数据库应用工程问题,具备针对具体应用场合选择和使用恰当的技术和工具的能力。

2.课程设计题目描述:

教学信息管理系统

3.系统需求分析:

随着互联网技术的发展,越来越多的技术工作早已深入计算机系统,像员工信息管理系统、合同管理系统、财务管理系统,以及关乎到我们学习信息的教学信息管理系统,都与互联网技术密切结合。

而今天我要做的教学信息管理系统也是如此,设计要求如下:

(1)根据素材文档和主题设计表结构,建立表的关系

(2)结合所学开发工具实现数据库应用程序的开发

(3)撰写实训报告

另外,系统实现功能分析如下:

登录功能:不同身份进入不同的操作界面;

管理员

学生

任课教师

辅导员

管理员权限:最高管理权限

  • 添加数据:能对课程、学生、成绩、教学任务等添加新数据;
  • 修改数据:能对课程、学生、成绩等修改;
  • 删除数据: 能对课程、学生、成绩等删除数据;
  • 查询:查询各专业课程信息,每门课成绩信息信息、每个老师的授课情况;
  • 统计:每门课不及格人员信息、学生学分;

普通学生权限:查询成绩信息、课程信息(如教材、任课教师等)、每学期开课情况,修改个人信息等;

任课教师权限:录入任课班级所教课程的学生成绩;

辅导员选修:查询所带班级的学生学号,姓名,课程号,课程名,成绩。

第二章  概念设计

1. 根据以上对系统的需求分析来进行系统设计,本系统使用的数据库实体分别为学生信息实体、课程信息实体、教材信息实体、成绩信息实体以及任课教师信息实体。

(1)学生信息实体(student)

学生信息实体主要包括学号、姓名、班级等属性信息,E-R如下:

(2)课程信息实体(course)

课程信息实体主要包括课程类别、课程编号、课程名称、考核模式、学分、学时、课程备注、课程性质以及学年等属性信息,具体E-R如下图:

(3)教材信息实体(book)

教材信息实体主要包括课程编号、课程名称和教材名称等属性信息,具体E-R图如下:

(4)成绩信息实体(grade)

成绩信息实体主要包括学号、姓名、课程名称以及成绩等属性信息,具体E-R图如下:

(5)任课教师信息实体(teacher)

任课教师信息实体主要包括课程名称、课程备注、班级以及任课教师等属性信息,具体E-R图如下:

(6)用户信息实体(user)

用户信息实体主要包括管理员、学生、任课教师和辅导员等属性信息,具体E-R如下:

2.学生、课程、教材、成绩和任课教师总概念结构关系E-R图如下:

第三章 逻辑设计

1.实体属性表

学生(student)

学号、姓名、班级

课程(course)

课程类别、课程编号、课程名称、考核模式、学分、学时、课程备注、课程性质、学年

教材(book)

课程编号、课程名称、教材名称、ISBM号、版本信息、定价、使用班级

成绩(grade)

学号、姓名、课程名称、成绩

任课教师(teacher)

课程名称、课程备注、班级、任课教师

用户(user)

管理员、学生、任课教师、辅导员

2. 设计学生信息实体表(student)、课程信息实体表(course)、教材信息实体表(book)、成绩信息实体表(grade)、任课教师信息实体表(teacher)用户信息表(user)。

(1)学生信息表(student)

学生信息表主要包括学号、姓名和班级等属性信息,如下图:

(2)课程信息表(course)

课程信息表主要包括课程类别、课程编号、课程名称、考核模式、学分、学时、课程备注、课程性质以及学年等属性信息,如下图:

(3)教材信息表(book)

教材信息表主要包括课程编号、课程名称、教材名称ISBM号、版本信息、定价以及使用班级等属性信息,如下图:

(4)成绩信息表

成绩信息表主要包括学号、姓名、课程名称以及成绩等属性信息,如下图:

(5)任课教师信息表

任课教师信息表主要包括课程名称、课程备注、班级以及任课教师等属性信息,如下图:

(6)用户表(user)

用户表主要包括管理员、学生、任课教师和辅导员四个属性信息,如下图:

第四章 数据库设计与实现

1.根据总概念分析E-R图建立各个信息表之间的联系,确定表、字段、数据类型、约束、外键、触发器等,建立数据库关系图,如下图:

2.SQL语句实现

创建教学管理系统中各个数据表,如下:

USE `教学信息管理系统`;

(1)学生信息表的创建

DROP TABLE IF EXISTS `student`;

CREATE TABLE `student` (

`学号` char(12) NOT NULL,

`姓名` char(10) NOT NULL,

`班级` char(20) NOT NULL,

PRIMARY KEY (`学号`)

) ENGINE=InnoDB DEFAULT CHARSET=utf8;

(2)课程信息表的创建

DROP TABLE IF EXISTS `course`;

CREATE TABLE `course` (

`课程类别` char(10) NOT NULL,

`课程编号` char(10) NOT NULL,

`课程名称` char(20) NOT NULL,

`考核模式` char(10) NOT NULL,

`学分` int DEFAULT NULL,

`学时` int DEFAULT NULL,

`课程备注` char(20) DEFAULT NULL,

`课程性质` char(10) DEFAULT NULL,

`学年` char(10) DEFAULT NULL,

PRIMARY KEY (`课程名称`)

) ENGINE=InnoDB DEFAULT CHARSET=utf8;

(3)教材信息表的创建

DROP TABLE IF EXISTS `book`;

CREATE TABLE `book` (

`课程编号` char(10) NOT NULL,

`课程名称` char(20) NOT NULL,

`教材名称` char(20) DEFAULT NULL,

`ISBM号` char(20) DEFAULT NULL,

`版本信息` char(20) DEFAULT NULL,

`定价` double DEFAULT NULL,

`使用班级` char(20) DEFAULT NULL,

PRIMARY KEY (`课程名称`)

CONSTRAINT `book_ibfk_1` FOREIGN KEY (`课程名称`) REFERENCES `course` (`课程名称`)

) ENGINE=InnoDB DEFAULT CHARSET=utf8;

(4)成绩信息表的创建

DROP TABLE IF EXISTS `grade`;

CREATE TABLE `grade` (

`学号` char(12) NOT NULL,

`姓名` char(10) NOT NULL,

`课程名称` char(20) NOT NULL,

`成绩` double DEFAULT NULL,

KEY `学号` (`学号`),

KEY `课程名称` (`课程名称`),

CONSTRAINT `grade_ibfk_1` FOREIGN KEY (`学号`) REFERENCES `student` (`学号`),

CONSTRAINT `grade_ibfk_2` FOREIGN KEY (`课程名称`) REFERENCES `course` (`课程名称`)

) ENGINE=InnoDB DEFAULT CHARSET=utf8;

(5)任课教师信息表的创建

DROP TABLE IF EXISTS `teacher`;

CREATE TABLE `teacher` (

`课程名称` char(20) NOT NULL,

`课程备注` char(20) DEFAULT NULL,

`班级` char(10) NOT NULL,

`任课教师` char(10) DEFAULT NULL,

PRIMARY KEY (`课程名称`),

CONSTRAINT `teacher_ibfk_1` FOREIGN KEY (`课程名称`) REFERENCES `course` (`课程名称`)

) ENGINE=InnoDB DEFAULT CHARSET=utf8;

(6)用户信息表的创建

DROP TABLE IF EXISTS `user`;

CREATE TABLE `user` (

`管理员` char(20) CHARACTER SET utf8 COLLATE utf8_general_ci DEFAULT NULL,

`学生` char(20) CHARACTER SET utf8 COLLATE utf8_general_ci DEFAULT NULL,

`任课教师` char(20) CHARACTER SET utf8 COLLATE utf8_general_ci DEFAULT NULL,

`辅导员` char(20) CHARACTER SET utf8 COLLATE utf8_general_ci DEFAULT NULL,

) ENGINE=InnoDB DEFAULT CHARSET=utf8;

第五章 数据库实现

1.数据的载入(此处图片省略,附上sql文件代码,有需要可以查看运行截图)

链接:aql.sqlhttps://gitcode.net/Pluto_ssy/mysql

(1)在SQLyog中教学信息管理系统数据库的student表中导入学生信息数据,如下图:

(2)向course表中导入课程信息数据,如下图:

(3)向grade表中导入成绩数据,如下图:

(4)向teacher表中导入任课老师的信息数据,如下图:

(5)向book表中导入教材的信息数据,如下图:

(6)向user表中导入使用者的信息数据,如下图:

2.另外建立四个表来存放管理数据库的用户账号密码,分别为管理员密码表(admin)、学生密码表(studentpswd)、任课教师密码表(teacherpswd)和 辅导员密码表(coach)。

(1)管理员密码表(admin)

(2)学生密码表(studentpswd)

(3)任课教师密码表(teacherpswd)

(4)辅导员密码表(coach)

第六章 应用程序设计

1.教学信息管理系统用户登录模板,如下图:

2.用户操作模板。

(1)管理员操作模板,如下图:

(2)辅导员操作模板,如下图:

(3)任课教师操作模板,如下图:

(4)学生操作模板,如下图:

3.具体用户操作功能实现。(此处列举任课教师登录情况举例,别的图片省略,可自行运行代码截图)

(1)在登陆界面选择“管理员”角色,输入正确的ID和密码。验证成功则可进入管理员管理界面。管理员号和登录密码存在数据库中的管理员密码信息表(admin),表中存在的管理员才允许登录。

(i)点击教师信息管理按钮,出现相关操作按钮,可以实现相应要求。

添加,如下图:

删除,如下图:

修改,如下图:

查询,如下图:

(ii)点击学生信息管理按钮、课程信息管理按钮和成绩信息管理按钮,出现相关操作按钮与操作均与教师信息按钮类似,可以实现相应要求,如下图相关操作略。

(2)在登陆界面选择“辅导员”角色,输入正确的ID和密码。验证成功则可进入辅导员管理界面。辅导员号和登录密码存在数据库中的辅导员密码信息表(coach),表中存在的辅导员才允许登录。

(i)在出现相关操作按钮,点击可以实现相应要求。

点击个人信息修改密码,如下图。

à

查询相关信息(学生、课程、成绩),如下图:

(3)在登陆界面选择“任课教师”角色,输入正确的ID和密码。验证成功则可进入系统管理员管理界面。任课教师职工号和登录密码存在数据库中的任课教师员密码信息表(teacherpswd),表中存在的任课教师才允许登录。

(i)在出现相关操作按钮,点击可以实现相应要求。

点击个人信息修改密码,如下图:

à

点击成绩信息管理,可以修改相关学生成绩,如下图。

 à

点击学生信息查询和课程信息查询,会出现相应内容,如下图:

(4)在登陆界面选择“学生”角色,输入正确的ID和密码。验证成功则可进入学生管理界面。学生学号和登录密码存在数据库中的学生密码信息表(studentpswd),表中存在的学生才允许登录。

(i)在出现相关操作按钮,点击可以实现相应要求。

个人信息管理、修改密码,如下图:

à

成绩信息查询和课程信息查询,如下图:

第七章 附录代码(乱序)

Check.java

package sqlyog;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.Random;
public class Check {static String s;public static void main(String[] args) {// TODO Auto-generated method stubfinal JFrame f=new JFrame("MyPanel");Panel p=new MyPanel();//s=p.s1;System.out.println(s);f.add(new MyPanel());//f.setLocation(400,600);f.setSize(200, 100);f.setLocationRelativeTo(null);f.setVisible(true);f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);}
}

CM.java

package sqlyog;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.sql.*;
import java.util.*;public class CM extends JFrame implements ActionListener{//定义菜单按钮Button btnadd=new Button("添加");Button btndelete=new Button("删除");Button btnupdate=new Button("修改");Button btnsearch=new Button("查询");Button btndisplay=new Button("刷新显示");Button btnreturn=new Button("返回");//定义菜单栏JMenuBar mb=new JMenuBar();//定义滚轮面板JScrollPane jsp;JScrollPane jsp1;//定义表格JTable tb;JTable tb1;//定义字体Font f1=new Font("行楷",Font.BOLD,15);Font f2=new Font("宋体",Font.ITALIC,36);//创建连接数据库变量对象Connection conn;Statement stmt;ResultSet rs;//设置端口常量String dbURL="jdbc:mysql://localhost:3306/教学信息管理系统";String username="root";String userpwd="123456";//定义一个Object数组Object[][] arr;public void connDB() { //连接数据库方法try {conn=DriverManager.getConnection(dbURL,username,userpwd);stmt=conn.createStatement();}catch(Exception e) {e.printStackTrace();}}public void closeDB() {  //关闭数据库方法try {rs.close();stmt.close();conn.close();}catch(Exception e) {e.printStackTrace();}}public CM() {  //无参构造方法super("课程信息管理");//设置按钮字体和颜色btnadd.setFont(f1);btnadd.setBackground(new Color(131,175,155));btnupdate.setFont(f1);btndelete.setBackground(new Color(131,175,155));btnupdate.setFont(f1);btnupdate.setBackground(new Color(131,175,155));btnsearch.setFont(f1);btnsearch.setBackground(new Color(131,175,155));btndisplay.setFont(f1);btndisplay.setBackground(new Color(131,175,155));btnreturn.setFont(f1);btnreturn.setBackground(new Color(131,175,155));//将按钮添加进菜单栏mb.add(btnadd);mb.add(btndelete);mb.add(btnupdate);mb.add(btnsearch);mb.add(btndisplay);mb.add(btnreturn);//连接数据库this.connDB();//注册事件监听器btnadd.addActionListener(this);btndelete.addActionListener(this);btnupdate.addActionListener(this);btnsearch.addActionListener(this);btndisplay.addActionListener(this);btnreturn.addActionListener(this);setSize(500,300);setDefaultCloseOperation(EXIT_ON_CLOSE);setLocationRelativeTo(null);setVisible(true);//将菜单栏添加进容器中this.setJMenuBar(mb);}   public void update() {String cno1=null; //局部变量一定要初始化int row=tb.getSelectedRow();  //代表鼠标选定的行数if(row==-1) {  //表示未被选中JOptionPane.showMessageDialog(null,"请选择要修改的信息!");}else {int x=0;try {rs=stmt.executeQuery("select * from course");while(rs.next() && x<=row) {cno1=rs.getString("课程编号");x++;}this.dispose();new CMupdate(cno1);}catch(Exception e) {e.printStackTrace();}}}public void display() {int i=0,j=0;//定义一个数组来存储所有数据ArrayList list=new ArrayList();try {rs=stmt.executeQuery("select * from course");while(rs.next()) {list.add(rs.getString("课程类别"));list.add(rs.getString("课程编号"));list.add(rs.getString("课程名称"));list.add(rs.getString("考核模式"));list.add(rs.getString("学分"));list.add(rs.getString("学时"));list.add(rs.getString("课程备注"));list.add(rs.getString("课程性质"));list.add(rs.getString("学年"));i++;}}catch(Exception e) {e.printStackTrace();}arr=new Object[i][9];String[] listname= {"课程类别","课程编号","课程名称","考核模式","学分","学时","课程备注","课程性质","学年"};//用二维数组来存储所有数据try {rs=stmt.executeQuery("select * from course order by 课程类别");while(rs.next()) {arr[j][0]=rs.getString("课程类别");arr[j][1]=rs.getString("课程编号");arr[j][2]=rs.getString("课程名称");arr[j][3]=rs.getString("考核模式");arr[j][4]=rs.getString("学分");arr[j][5]=rs.getString("学时");arr[j][6]=rs.getString("课程备注");arr[j][7]=rs.getString("课程性质");arr[j][8]=rs.getString("学年");j++;}}catch(Exception e) {e.printStackTrace();}tb=new JTable(arr,listname);jsp=new JScrollPane(tb);this.add(jsp);}public void delete() {   //删除信息方法String tno=null;  //定义字符变量,来接收教师号   int row=tb.getSelectedRow();  //代表鼠标选定的行数if(row==-1) {  //表示未被选中JOptionPane.showMessageDialog(null,"请选择要删除的记录!");}else {int x=0;try {rs=stmt.executeQuery("select * from course");while(rs.next() && x<=row) {tno=rs.getString("课程编号");x++;}stmt.executeUpdate("delete from teacher where 课程编号="+tno);   //局部变量一定要初始化JOptionPane.showMessageDialog(null,"删除成功!");this.dispose();new CM().display();}catch(Exception e) {e.printStackTrace();}}}public void show(String str) {   //查询结果方法JFrame f=new JFrame("查询结果");//JPanel p=new JPanel();f.setLayout(new FlowLayout());f.setDefaultCloseOperation(EXIT_ON_CLOSE);f.setSize(500,150);f.setVisible(true);f.setLocationRelativeTo(null);Button btnrt=new Button("返回");btnrt.setFont(f1);btnrt.setBackground(new Color(131,175,155));//btnrt.setSize(20,10);this.connDB();arr=new Object[1][9];try {rs=stmt.executeQuery("select * from course where 课程编号="+str);while(rs.next()) {arr[0][0]=rs.getString("课程类别");arr[0][1]=rs.getString("课程编号");arr[0][2]=rs.getString("课程名称");arr[0][3]=rs.getString("考核模式");arr[0][4]=rs.getString("学分");arr[0][5]=rs.getString("学时");arr[0][6]=rs.getString("课程备注");arr[0][7]=rs.getString("课程性质");arr[0][8]=rs.getString("学年");}}catch(Exception e){e.printStackTrace();}String[] list={"课程类别","课程编号","课程名称","考核模式","学分","学时","课程备注","课程性质","学年"};tb1=new JTable(arr,list); //创建表格jsp1=new JScrollPane(tb1);//f.add(btnrt,BorderLayout.NORTH);f.add(btnrt);f.add(jsp1);  //此处有个小bug,只有添加进滚轮面板,才能将头部名称显示出来//f1.add(btnrt); btnrt.addMouseListener(new MouseAdapter() {public void mouseClicked(MouseEvent e) {f.dispose();}});}//该方法用来确认是否在数据库中找到课程编号public boolean searchtest(String str) {boolean x=false;this.connDB();try {rs=stmt.executeQuery("select * from course");while(rs.next()) {if(rs.getString("课程编号").trim().equals(str)) {  //在java中,判断字符串是否相同,一定要使用equals函数!!!!!!!!x=true;}}//return x;}catch(Exception e) {e.printStackTrace();}return x;}public void search() {   //等效于将一个窗口写在方法里面JFrame f=new JFrame("查询");f.setLayout(new FlowLayout());f.setSize(240,180);f.setVisible(true);f.setLocationRelativeTo(null);JPanel p1=new JPanel();JPanel p2=new JPanel();JLabel stuno=new JLabel("输入课程编号:");JTextField stuno1=new JTextField(10);Button ok=new Button("确定");Button cancel=new Button("取消");p1.add(stuno);p1.add(stuno1);p2.add(ok);p2.add(cancel);f.add(p1);f.add(p2);//为组件注册监听器ok.addMouseListener(new MouseAdapter() {public void mouseClicked(MouseEvent e) {if(stuno1.getText().equals("")) {JOptionPane.showMessageDialog(null,"请输入课程号");}else {if(!(searchtest(stuno1.getText().trim()))) {f.dispose();JOptionPane.showMessageDialog(null,"对不起,该课程不存在!");}else {f.dispose();            //new SM(stuno1.getText());show(stuno1.getText());}}}});cancel.addMouseListener(new MouseAdapter() {public void mouseClicked(MouseEvent e) {f.dispose();//}});f.addWindowListener(new WindowAdapter() {public void windowClosing(WindowEvent e) {Window w=(Window)e.getComponent();  w.dispose();}});}public void actionPerformed(ActionEvent e) {//对事件进行相对应得处理if(e.getSource()==btnadd) {  this.dispose();new CMadd();}if(e.getSource()==btndelete) {JOptionPane.showMessageDialog(null,"删除操作,谨慎操作!");this.delete();}if(e.getSource()==btnupdate) {//this.dispose();this.update();}if(e.getSource()==btnsearch) {this.search();}if(e.getSource()==btndisplay) {this.dispose();new CM().display();}if(e.getSource()==btnreturn) {this.dispose();new GLFrame();}}public static void main(String[] args) {// TODO Auto-generated method stubnew CM().display();}}

CMadd.java

package sqlyog;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.sql.*;public class CMadd extends JFrame implements ActionListener{//定义面板Panel p=new Panel();Panel p1=new Panel();Panel p2=new Panel();/*Panel psno=new Panel();Panel psname=new Panel();Panel pssex=new Panel();Panel psage=new Panel();Panel psclass=new Panel();Panel pspwd=new Panel();*///定义属性标签及文本框JLabel by=new JLabel("课程类别:");JTextField tby=new JTextField(10);JLabel no=new JLabel("课程编号:");JTextField cno=new JTextField(10);JLabel name=new JLabel("课程名称:");JTextField cname=new JTextField(10);JLabel ks=new JLabel("考核模式:");JTextField tks=new JTextField(10);JLabel f=new JLabel("学分:");JTextField tf=new JTextField(10);JLabel t=new JLabel("学时:");JTextField tt=new JTextField(10);JLabel bu=new JLabel("课程备注:");JTextField tbu=new JTextField(10);JLabel xz=new JLabel("课程性质:");JTextField txz=new JTextField(10);JLabel xn=new JLabel("学年:");JTextField txn=new JTextField(10);//定义字体Font f1=new Font("宋体",Font.BOLD,20);Font f2=new Font("幼圆",Font.ITALIC,30);Font f3=new Font("楷体",Font.BOLD,18);Font f4=new Font("隶书",Font.PLAIN,40);//定义按钮Button btnsure=new Button("确定");Button btnagain=new Button("重置");Button btncancel=new Button("取消");//定义连接字符String dbURL = "jdbc:mysql://localhost:3306/教学信息管理系统";String userName = "root";String userPwd = "123456";Connection conn;Statement stmt;ResultSet rs;public void connDB() {   //连接数据库方法try {//连接数据库conn=DriverManager.getConnection(dbURL,userName,userPwd);stmt=conn.createStatement();}catch(Exception e) {e.printStackTrace();//System.out.println("连接失败!");}}public void closeDB() {  //关闭数据库方法try {rs.close();stmt.close();conn.close();}catch(Exception e) {e.printStackTrace();//System.out.println("关闭失败!");}}public CMadd() {  //构造方法super("课程信息添加页面");setLayout(new FlowLayout());//设置字体by.setFont(f3);no.setFont(f3);name.setFont(f3);ks.setFont(f3);f.setFont(f3);t.setFont(f3);bu.setFont(f3);xz.setFont(f3);xn.setFont(f3);//按钮字体、颜色btnsure.setFont(f1);btnsure.setBackground(new Color(131,175,155)); //淡草色btncancel.setFont(f1);btncancel.setBackground(new Color(131,175,155)); //淡草色btnagain.setFont(f1);btnagain.setBackground(new Color(131,175,155)); //淡草色//设置面板的网格布局管理p1.setLayout(new GridLayout(8,2));//p2.setLayout(new GridLayout(4,2));//将标签和文本框添加进面板p1.add(by);p1.add(tby);p1.add(no);p1.add(cno);p1.add(name);p1.add(cname);p1.add(ks);p1.add(tks);p1.add(f);p1.add(tf);p1.add(t);p1.add(tt);p1.add(bu);p1.add(tbu);p1.add(xz);p1.add(txz);p1.add(xn);p1.add(txn);//将按钮添加进面板p.add(btnsure);p.add(btnagain);p.add(btncancel);//添加面板this.add(p1);//this.add(p2);this.add(p);//设置顶层容器的大小、位置、可见性及close功能setDefaultCloseOperation(EXIT_ON_CLOSE);setSize(500,350);setLocationRelativeTo(null);setVisible(true);//注册监听器btnsure.addActionListener(this);btnagain.addActionListener(this);btncancel.addActionListener(this);}public void insert() {  //插入方法try {this.connDB();//插入数据stmt.executeUpdate("insert into course values("+"'"+tby.getText().trim()+"'"+","+"'"+cno.getText().trim()+"'"+","+"'"+cname.getText().trim()+"'"+","+"'"+tks.getText().trim()+"'"+","+"'"+tf.getText().trim()+"'"+","+"'"+tt.getText().trim()+"'"+","+"'"+tbu.getText().trim()+"'"+","+"'"+txz.getText().trim()+"'"+","+"'"+txn.getText().trim()+"'"+")");//System.out.println("insert success!");JOptionPane.showMessageDialog(null,"课表信息添加成功!");this.dispose();new CM().display();}catch(Exception e) {e.printStackTrace();}}//判断是否找到课程号public boolean searchtest(String str) {boolean x=false;this.connDB();try {rs=stmt.executeQuery("select * from grade");while(rs.next()) {if(rs.getString("学号").trim().equals(str)) {  //在java中,判断字符串是否相同,一定要使用equals函数!!!!!!!!x=true;}}//return x;}catch(Exception e) {e.printStackTrace();}return x;}public void actionPerformed(ActionEvent e) {if(e.getSource()==btnsure) {if(tby.getText().equals("") || cno.getText().equals("") || cname.getText().equals("") || tks.getText().equals("") || tf.getText().equals("") || tt.getText().equals("") || tbu.getText().equals("") || txz.getText().equals("") || txn.getText().equals("")) {JOptionPane.showMessageDialog(null,"信息不能为空!");}else if(searchtest(cno.getText())){JOptionPane.showMessageDialog(null,"该课程已经存在!请重新修改");tby.setText("");cno.setText("");cname.setText("");tks.setText("");tf.setText("");tt.setText("");tbu.setText("");txz.setText("");txn.setText("");}else {this.insert();}}if(e.getSource()==btnagain) {tby.setText("");cno.setText("");cname.setText("");tks.setText("");tf.setText("");tt.setText("");tbu.setText("");txz.setText("");txn.setText("");}if(e.getSource()==btncancel) {this.dispose();new CM().display();;//System.exit(0);}}public static void main(String[] args) {// TODO Auto-generated method stubnew CMadd();}}

CMupdate.java

package sqlyog;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.sql.*;public class CMupdate extends JFrame implements ActionListener{//定义面板Panel p=new Panel();Panel p1=new Panel();Panel p2=new Panel();/*Panel psno=new Panel();Panel psname=new Panel();Panel pssex=new Panel();Panel psage=new Panel();Panel psclass=new Panel();Panel pspwd=new Panel();*///定义属性标签及文本框JLabel by=new JLabel("课程类别:");JTextField tby=new JTextField(10);JLabel no=new JLabel("课程编号:");JTextField cno=new JTextField(10);JLabel name=new JLabel("课程名称:");JTextField cname=new JTextField(10);JLabel ks=new JLabel("考核模式:");JTextField tks=new JTextField(10);JLabel f=new JLabel("学分:");JTextField tf=new JTextField(10);JLabel t=new JLabel("学时:");JTextField tt=new JTextField(10);JLabel bu=new JLabel("课程备注:");JTextField tbu=new JTextField(10);JLabel xz=new JLabel("课程性质:");JTextField txz=new JTextField(10);JLabel xn=new JLabel("学年:");JTextField txn=new JTextField(10);//定义字体Font f1=new Font("宋体",Font.BOLD,20);Font f2=new Font("幼圆",Font.ITALIC,30);Font f3=new Font("楷体",Font.BOLD,18);Font f4=new Font("隶书",Font.PLAIN,40);//定义按钮Button btnsure=new Button("确定");Button btnagain=new Button("重置");Button btncancel=new Button("取消");//定义连接字符String dbURL = "jdbc:mysql://localhost:3306/教学信息管理系统";String userName = "root";String userPwd = "123456";Connection conn;Statement stmt;ResultSet rs;public void connDB() {   //连接数据库方法try {//连接数据库conn=DriverManager.getConnection(dbURL,userName,userPwd);stmt=conn.createStatement();}catch(Exception e) {e.printStackTrace();//System.out.println("连接失败!");}}public void closeDB() {  //关闭数据库方法try {rs.close();stmt.close();conn.close();}catch(Exception e) {e.printStackTrace();//System.out.println("关闭失败!");}}public CMupdate(String str) {  //含参构造方法super("课程信息修改页面");setLayout(new FlowLayout());cno.setText(str);cno.setEditable(false);//设置字体by.setFont(f3);no.setFont(f3);name.setFont(f3);ks.setFont(f3);f.setFont(f3);t.setFont(f3);bu.setFont(f3);xz.setFont(f3);xn.setFont(f3);//按钮字体、颜色btnsure.setFont(f1);btnsure.setBackground(new Color(131,175,155)); //淡草色btncancel.setFont(f1);btncancel.setBackground(new Color(131,175,155)); //淡草色btnagain.setFont(f1);btnagain.setBackground(new Color(131,175,155)); //淡草色//设置面板的网格布局管理p1.setLayout(new GridLayout(4,2));//p2.setLayout(new GridLayout(4,2));//将标签和文本框添加进面板p1.add(by);p1.add(tby);p1.add(no);p1.add(cno);p1.add(name);p1.add(cname);p1.add(ks);p1.add(tks);p1.add(f);p1.add(tf);p1.add(t);p1.add(tt);p1.add(bu);p1.add(tbu);p1.add(xz);p1.add(txz);p1.add(xn);p1.add(txn);//将按钮添加进面板p.add(btnsure);p.add(btnagain);p.add(btncancel);//添加面板this.add(p1);//this.add(p2);this.add(p);//设置顶层容器的大小、位置、可见性及close功能setDefaultCloseOperation(EXIT_ON_CLOSE);setSize(500,350);setLocationRelativeTo(null);setVisible(true);//注册监听器btnsure.addActionListener(this);btnagain.addActionListener(this);btncancel.addActionListener(this);}public void insert() {  //插入方法try {this.connDB();//插入数据stmt.executeUpdate("insert into course values("+"'"+tby.getText().trim()+"'"+","+"'"+cno.getText().trim()+"'"+","+"'"+cname.getText().trim()+"'"+","+"'"+tks.getText().trim()+"'"+","+"'"+tf.getText().trim()+"'"+","+"'"+tt.getText().trim()+"'"+","+"'"+tbu.getText().trim()+"'"+","+"'"+txz.getText().trim()+"'"+","+"'"+txn.getText().trim()+"'"+")");//System.out.println("insert success!");JOptionPane.showMessageDialog(null,"课表信息修改成功!");this.dispose();new CM().display();}catch(Exception e) {e.printStackTrace();}}public void delete(String s) {   //删除信息方法try {//stmt.executeUpdate("delete  from 教师表  where 教师号="+s);   //局部变量一定要初始化stmt.executeUpdate("delete  from course where 课程编号="+s);//JOptionPane.showMessageDialog(null,"删除成功!");}catch(Exception e) {e.printStackTrace();}}public void actionPerformed(ActionEvent e) {if(e.getSource()==btnsure) {if(tby.getText().equals("") || cno.getText().equals("") || cname.getText().equals("") || tks.getText().equals("") || tf.getText().equals("") || tt.getText().equals("") || tbu.getText().equals("") || txz.getText().equals("") || txn.getText().equals("")) {JOptionPane.showMessageDialog(null,"信息不能为空!");}else {this.delete(cno.getText());this.insert();}}if(e.getSource()==btnagain) {tby.setText("");cno.setText("");cname.setText("");tks.setText("");tf.setText("");tt.setText("");tbu.setText("");txz.setText("");txn.setText("");}if(e.getSource()==btncancel) {this.dispose();new CM().display();;//System.exit(0);}}public static void main(String[] args) {// TODO Auto-generated method stubnew CMadd();}
}

CoachFrame.java

package sqlyog;
import java.awt.*;
import java.awt.event.*;
import java.sql.*;
import javax.swing.*;
import java.util.*;public class CoachFrame extends JFrame implements ActionListener{JLabel l=new JLabel("--辅导员页面--");//定义面板容器JPanel p1=new JPanel();JPanel p2=new JPanel();//设置字体类型Font f1=new Font("宋体",Font.BOLD,30);Font f2=new Font("幼圆",Font.ITALIC,30);Font f3=new Font("楷体",Font.BOLD,17);Font f4=new Font("隶书",Font.PLAIN,40);//设置6个按钮,以便管理员操作JButton btnTmg=new JButton("个人信息管理");JButton btnSsh=new JButton("学生信息查询");JButton btnCsh=new JButton("课程信息查询");JButton btnSCmg=new JButton("成绩信息查询");JButton btnEXIT=new JButton("退出");String tno;String dbURL="jdbc:mysql://localhost:3306/教学信息管理系统";String useName="root";String usePwd="123456";Connection conn;Statement stmt;ResultSet rs;Object[][] arr;String tpwd;JScrollPane scroll1,scroll2,scroll3,scroll4;JTable tb1,tb2,tb3,tb4; int row;public CoachFrame(String str) {   //构造方法super("辅导员页面");setLayout(new FlowLayout());//设置标签的颜色l.setFont(f1);l.setForeground(Color.blue);//设置按钮字体和颜色btnTmg.setFont(f3);btnTmg.setContentAreaFilled(false);//btnTM.setBackground(Color.blue);btnSsh.setFont(f3);btnSsh.setContentAreaFilled(false);btnCsh.setFont(f3);btnCsh.setContentAreaFilled(false);btnSCmg.setFont(f3);btnSCmg.setContentAreaFilled(false);btnEXIT.setFont(f3);btnEXIT.setContentAreaFilled(false);this.tno=str;p1.add(l); p1.setOpaque(false);p2.setOpaque(false);p2.setLayout(new GridLayout(3,2,10,10));p2.add(btnTmg);p2.add(btnSsh);p2.add(btnCsh);p2.add(btnSCmg);p2.add(btnEXIT);//布局管理器this.add(p1);this.add(p2);//设置顶层容器的大小、位置、可见性及close功能setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);setSize(350,300);setLocationRelativeTo(null);setVisible(true);this.connDB();btnTmg.addActionListener(this);btnSsh.addActionListener(this);btnCsh.addActionListener(this);btnSCmg.addActionListener(this);btnEXIT.addActionListener(this);}public void connDB() {try {conn=DriverManager.getConnection(dbURL,useName,usePwd);stmt=conn.createStatement();}catch(Exception e) {e.printStackTrace();}}public void closeDB() {try {rs.close();stmt.close();conn.close();}catch(Exception e) {e.printStackTrace();}}public void tmg(String str) {   //个人信息管理方法JFrame f=new JFrame("个人信息");//JPanel p=new JPanel();f.setLayout(new FlowLayout());f.setDefaultCloseOperation(EXIT_ON_CLOSE);f.setSize(500,150);f.setVisible(true);f.setLocationRelativeTo(null);Button btnchange=new Button("修改密码");Button btnrt=new Button("返回");btnchange.setFont(f3);btnchange.setBackground(new Color(131,175,155));btnrt.setFont(f3);btnrt.setBackground(new Color(131,175,155));//btnrt.setSize(20,10);this.connDB();arr=new Object[1][7];try {rs=stmt.executeQuery("select * from coach where 辅导员号="+str);while(rs.next()) {arr[0][0]=rs.getString("辅导员号");arr[0][1]=rs.getString("密码");}}catch(Exception e){e.printStackTrace();}String[] list= {"辅导员号","密码"};tb1=new JTable(arr,list); //创建表格scroll1=new JScrollPane(tb1);//f.add(btnrt,BorderLayout.NORTH);f.add(btnchange);f.add(btnrt);f.add(scroll1);  //此处有个小bug,只有添加进滚轮面板,才能将头部名称显示出来//f1.add(btnrt); btnrt.addMouseListener(new MouseAdapter() {public void mouseClicked(MouseEvent e) {f.dispose();new CoachFrame(tno);}});btnchange.addMouseListener(new MouseAdapter() {public void mouseClicked(MouseEvent e) {f.dispose();change();}});}public void change() {  //创建修改密码页面,新窗口用于修改密码this.connDB();JFrame f=new JFrame("修改密码");f.setLayout(new FlowLayout());JPanel p=new JPanel();JPanel p1=new JPanel();p.setLayout(new GridLayout(3,2));JLabel btn1=new JLabel("初始密码:");btn1.setFont(f3);JTextField tf1=new JTextField(10);JLabel btn2=new JLabel("修改密码:");btn2.setFont(f3);JTextField tf2=new JTextField(10);Button ok=new Button("确定");ok.setBackground(new Color(131,175,155));Button cancel=new Button("取消");cancel.setBackground(new Color(131,175,155));p.add(btn1);p.add(tf1);p.add(btn2);p.add(tf2);p1.add(ok);p1.add(cancel);f.add(p);f.add(p1);f.setDefaultCloseOperation(EXIT_ON_CLOSE);f.setLocation(530,300);//f.setLocationRelativeTo(null);f.setSize(300,150);f.setVisible(true);ok.addMouseListener(new MouseAdapter() {public void mouseClicked(MouseEvent e) {try {   //获取初始密码rs=stmt.executeQuery("select * from coach where 辅导员号="+tno);while(rs.next()) {tpwd=rs.getString("密码").trim();}}catch(Exception e1) {e1.printStackTrace();}if(tf2.getText().equals("") || tf1.getText().equals("")) {JOptionPane.showMessageDialog(null, "密码不能为空!请重新修改!");}else{if(!tpwd.equals(tf1.getText().trim())) {     //spwd.equals(tf1.getText().trim())JOptionPane.showMessageDialog(null,"初始密码错误,请重新输入密码!");  //"原密码是"+spwd+"输入密码是"+tf1.getText();tf1.setText("");tf2.setText("");}else {try {stmt.executeUpdate("update coach set 密码 ="+tf2.getText().trim()+"where 职工号="+tno);}catch(Exception e1) {e1.printStackTrace();}JOptionPane.showMessageDialog(null,"密码修改成功!请重新登录!");f.dispose();new DLFrame();}}}});cancel.addMouseListener(new MouseAdapter() {public void mouseClicked(MouseEvent e) {f.dispose();new CoachFrame(tno);}});}public void ssh(String str) {  //学生信息查询JFrame f=new JFrame("学生信息");//JPanel p=new JPanel();f.setLayout(new FlowLayout());f.setDefaultCloseOperation(EXIT_ON_CLOSE);f.setSize(500,150);f.setVisible(true);f.setLocationRelativeTo(null);Button btnrt=new Button("返回");btnrt.setFont(f3);btnrt.setBackground(new Color(131,175,155));//btnrt.setSize(20,10);this.connDB();int i=0,j=0;ArrayList al=new ArrayList();try {rs=stmt.executeQuery("select * from student");while(rs.next()) {al.add(rs.getString("学号"));al.add(rs.getString("姓名"));al.add(rs.getString("班级"));i++;}}catch(Exception e){e.printStackTrace();}arr=new Object[i][3];try {rs=stmt.executeQuery("select * from student");while(rs.next()) {arr[j][0]=rs.getString("学号");arr[j][1]=rs.getString("姓名");arr[j][2]=rs.getString("班级");j++;}}catch(Exception e){e.printStackTrace();}String[] list= {"学号","姓名","班级"};tb2=new JTable(arr,list); //创建表格scroll2=new JScrollPane(tb2);//f.add(btnrt,BorderLayout.NORTH);f.add(btnrt);f.add(scroll2);  //此处有个小bug,只有添加进滚轮面板,才能将头部名称显示出来//f1.add(btnrt); btnrt.addMouseListener(new MouseAdapter() {public void mouseClicked(MouseEvent e) {f.dispose();new CoachFrame(tno);}});}public void csh(String str) {  //课程信息查询JFrame f=new JFrame("课程信息");//JPanel p=new JPanel();f.setLayout(new FlowLayout());f.setDefaultCloseOperation(EXIT_ON_CLOSE);f.setSize(500,150);f.setVisible(true);f.setLocationRelativeTo(null);Button btnrt=new Button("返回");btnrt.setFont(f3);btnrt.setBackground(new Color(131,175,155));//btnrt.setSize(20,10);this.connDB();int i=0,j=0;ArrayList al=new ArrayList();try {rs=stmt.executeQuery("select  distinct 课程类别,课程编号,课程名称,考核模式,学分,学时,课程备注,课程性质,学年  from course");while(rs.next()) {al.add(rs.getString("课程类别"));al.add(rs.getString("课程编号"));al.add(rs.getString("课程名称"));al.add(rs.getString("考核模式"));al.add(rs.getInt("学分"));al.add(rs.getInt("学时"));al.add(rs.getString("课程备注"));al.add(rs.getString("课程性质"));al.add(rs.getString("学年"));i++;}}catch(Exception e){e.printStackTrace();}arr=new Object[i][9];try {rs=stmt.executeQuery("select  distinct 课程类别,课程编号,课程名称,考核模式,学分,学时,课程备注,课程性质,学年  from course");while(rs.next()) {arr[j][0]=rs.getString("课程类别");arr[j][1]=rs.getString("课程编号");arr[j][2]=rs.getString("课程名称");arr[j][3]=rs.getString("考核模式");arr[j][4]=rs.getInt("学分");arr[j][5]=rs.getInt("学时");arr[j][6]=rs.getString("课程备注");arr[j][7]=rs.getString("课程性质");arr[j][8]=rs.getString("学年");j++;}}catch(Exception e){e.printStackTrace();}String[] list= {"课程类别","课程编号","课程名称","考核模式","学分","学时","课程备注","课程性质","学年"};tb3=new JTable(arr,list); //创建表格scroll3=new JScrollPane(tb3);//f.add(btnrt,BorderLayout.NORTH);f.add(btnrt);f.add(scroll3);  //此处有个小bug,只有添加进滚轮面板,才能将头部名称显示出来//f1.add(btnrt); btnrt.addMouseListener(new MouseAdapter() {public void mouseClicked(MouseEvent e) {f.dispose();new CoachFrame(tno);}});}public void scmg(String str) {  //成绩信息管理JFrame f=new JFrame("学生成绩信息");//JPanel p=new JPanel();f.setLayout(new FlowLayout());f.setDefaultCloseOperation(EXIT_ON_CLOSE);f.setSize(500,150);f.setVisible(true);f.setLocationRelativeTo(null);Button btnrt=new Button("返回");btnrt.setFont(f3);btnrt.setBackground(new Color(131,175,155));//btnrt.setSize(20,10);this.connDB();int i=0,j=0;ArrayList al=new ArrayList();try {rs=stmt.executeQuery("select * from grade");while(rs.next()) {al.add(rs.getString("学号"));al.add(rs.getString("姓名"));al.add(rs.getString("课程名称"));al.add(rs.getString("成绩"));i++;}}catch(Exception e){e.printStackTrace();}arr=new Object[i][4];try {rs=stmt.executeQuery("select * from grade");while(rs.next()) {arr[j][0]=rs.getString("学号");arr[j][1]=rs.getString("姓名");arr[j][2]=rs.getString("课程名称");arr[j][3]=rs.getString("成绩");j++;}}catch(Exception e){e.printStackTrace();}String[] list= {"学号","姓名","课程名称","成绩"};tb4=new JTable(arr,list); //创建表格scroll4=new JScrollPane(tb4);//f.add(btnrt,BorderLayout.NORTH);f.add(btnrt);f.add(scroll4);  //此处有个小bug,只有添加进滚轮面板,才能将头部名称显示出来//f1.add(btnrt); btnrt.addMouseListener(new MouseAdapter() {public void mouseClicked(MouseEvent e) {f.dispose();new CoachFrame(tno);}});}//bug方法  只能新建类来解决/*public void scmg(String s1,String s2) { //精确到班级成绩的页面String csname=null;this.connDB();try {rs=stmt.executeQuery("select distinct 班级号 ,班级名称 from 学生信息表 where 班级号="+"'"+s2+"'");while(rs.next()) {csname=rs.getString("班级名称").trim();}}catch(Exception e) {e.printStackTrace();}JFrame f=new JFrame(csname+" --学生成绩信息");//JPanel p=new JPanel();f.setLayout(new FlowLayout());f.setDefaultCloseOperation(EXIT_ON_CLOSE);f.setSize(500,150);f.setVisible(true);f.setLocationRelativeTo(null);Button btnrt=new Button("返回");Button btnchange=new Button("修改");btnchange.setFont(f3);btnchange.setBackground(new Color(131,175,155));btnrt.setFont(f3);btnrt.setBackground(new Color(131,175,155));//btnrt.setSize(20,10);this.connDB();int i=0,j=0;ArrayList al=new ArrayList();try {rs=stmt.executeQuery("select * from 学生信息表  where 教师号="+s1+"and 班级号='"+s2+"'");while(rs.next()) {al.add(rs.getString("学号"));al.add(rs.getString("姓名"));al.add(rs.getString("性别"));al.add(rs.getInt("年龄"));al.add(rs.getString("班级名称"));al.add(rs.getString("课程号"));al.add(rs.getString("课程名"));al.add(rs.getString("成绩"));i++;}}catch(Exception e){e.printStackTrace();}arr=new Object[i][8];try {rs=stmt.executeQuery("select * from 学生信息表  where 教师号="+s1+"and 班级号= '"+s2+"'");while(rs.next()) {arr[j][0]=rs.getString("学号");arr[j][1]=rs.getString("姓名");arr[j][2]=rs.getString("性别");arr[j][3]=rs.getString("年龄");arr[j][4]=rs.getString("班级名称");arr[j][5]=rs.getString("课程号");arr[j][6]=rs.getString("课程名");arr[j][7]=rs.getString("成绩");j++;}}catch(Exception e){e.printStackTrace();}String[] list= {"学号","姓名","性别","年龄","班级名称","课程号","课程名","成绩"};tb5=new JTable(arr,list); //创建表格scroll5=new JScrollPane(tb5);//f.add(btnrt,BorderLayout.NORTH);f.add(btnchange);f.add(btnrt);f.add(scroll5);  //此处有个小bug,只有添加进滚轮面板,才能将头部名称显示出来//String sno1=null; //局部变量一定要初始化,获取学生学号String cno=null; //获取课程号///*Bug:千万不要在同一个方法里定义获取行数!!!!!!!!!!!无法执行                    ////row=tb5.getSelectedRow();  //代表鼠标选定的行数                                              ///btnrt.addMouseListener(new MouseAdapter() {public void mouseClicked(MouseEvent e) {f.dispose();scmg(tno);}});btnchange.addMouseListener(new MouseAdapter() {public void mouseClicked(MouseEvent e) {if(row==-1) {  //表示未被选中JOptionPane.showMessageDialog(null,"请选择要修改的信息!");}else {//int x=0;try {//String sno1=(String)tb1.getModel().getValueAt(row,column);f.dispose();//scchange1(sno1,cno);}catch(Exception e1) {e1.printStackTrace();}}//f.dispose();//scchange();}});}public void test() {  //用来获取行数}*/public void actionPerformed(ActionEvent e) {//按钮为“教师信息管理”,跳转页面if(e.getSource().equals(btnTmg)) {this.dispose();this.tmg(tno);}//按钮为“学生信息管理”,跳转页面if(e.getSource().equals(btnSsh)) {//new SM().display();this.dispose();this.ssh(tno);}//按钮为“课程信息管理”,跳转页面if(e.getSource().equals(btnCsh)) {this.dispose();this.csh(tno);}//按钮为“成绩信息管理”,跳转页面if(e.getSource().equals(btnSCmg)) {this.dispose();this.scmg(tno);}//按钮为“退出管理系统”,程序退出if(e.getSource().equals(btnEXIT)) {//System.exit(0);this.dispose();new DLFrame();}}public static void main(String[] args) {// TODO Auto-generated method stubnew CoachFrame("20050002");}}

DLFrame.java

//登录页面
package sqlyog;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
//import java.util.EventListener;
import java.sql.*;
//登录页面
public class DLFrame extends JFrame implements ActionListener,ItemListener{JPanel p1,p2,p3;Font f1,f2,f3,f4;/*Font f1=new Font("宋体",Font.BOLD,26);  //如果变量已经声明,则再定义必须放在方法里面(*此处出错)Font f2=new Font("幼圆",Font.ITALIC,20);Font f3=new Font("黑体",Font.BOLD,30);Font f4=new Font("隶书",Font.PLAIN,40);*/JLabel head1=new JLabel("用户登录 ");/*head1.setFont(f1);head1.setForeground(Color.BLUE);head2.setFont(f2);head2.setForeground(Color.GRAY);*///定义标签和文本框以及下拉列表//用户名标签和输入文本框JLabel usename=new JLabel("用户ID:");JTextField usenametext=new JTextField(12);//密码标签和密码文本框,JPasswordField对象会自动对密码进行隐藏处理JLabel password=new JLabel("密 码:");JPasswordField txtPwd=new JPasswordField(10);//添加验证码JLabel test=new JLabel("验证码:");//此处后期需要重写//角色标签和下拉列表,此处使用swing中的JComboBox类,也可使用awt中的Choice类JLabel role=new JLabel("角色:");JComboBox boxrole=new JComboBox();//创建三个选择按钮JButton a=new JButton("登录");JButton b=new JButton("重置");JButton c=new JButton("取消");//创建Connection和Statement对象//连接数据库和调用数据库Connection conn;Statement stmt;ResultSet rs;//定义连接字符String dbURL = "jdbc:mysql://localhost:3306/教学信息管理系统";String userName = "root";String userPwd = "123456";//定义变量来接收下拉列表的索引值,也可返回具体值int index=0;String index1;static int ok=1;static int cancel=0;int actionCode=0;public DLFrame(){        //构造方法super("教学信息管理系统");setLayout(new FlowLayout());p1=new JPanel();p2=new JPanel();p3=new JPanel();f1=new Font("宋体",Font.BOLD,40);f2=new Font("幼圆",Font.ITALIC,40);f3=new Font("楷书",Font.BOLD,28);f4=new Font("隶书",Font.PLAIN,50);//设置面板1的标签head1.setFont(f1);head1.setForeground(Color.BLUE);//p1.setBackground(new Color(255,240,240));p1.setBackground(null);p1.setOpaque(false);p1.add(head1);//面板2为4行2列的网格布局管理器p2.setLayout(new GridLayout(4,2));/*usename.setFont(f3);password.setFont(f3);role.setFont(f3);*///下拉列表中添加数据boxrole.addItem("管理员");boxrole.addItem("任课教师");boxrole.addItem("学生");boxrole.addItem("辅导员");//p2.setBackground(new Color(240,255,240));p2.setBackground(null);p2.setOpaque(false);boxrole.setOpaque(false);usenametext.setOpaque(false);txtPwd.setOpaque(false);p2.add(role);p2.add(boxrole);p2.add(usename);p2.add(usenametext);p2.add(password);p2.add(txtPwd);//将3个按钮添加进面板3中//p3.setBackground(new Color(230,230,250));p3.setBackground(null);p3.setOpaque(false);//设置透明a.setContentAreaFilled(false);b.setContentAreaFilled(false);c.setContentAreaFilled(false);//setGround(a);p3.add(a);p3.add(b);p3.add(c);//将三个面板添加进框架容器中this.add(p1);this.add(p2);this.add(p3);//this.add(new MyPanel());//设置顶层容器的大小、位置、可见性及close功能setDefaultCloseOperation(EXIT_ON_CLOSE);setSize(350,300);setLocationRelativeTo(null);setVisible(true);//注册事件监听器//这里的this就是本类,已经是ActionListener的实现类    --有待探究boxrole.addItemListener(this);  a.addActionListener(this);b.addActionListener(this);c.addActionListener(this);}public void connDB() {   //连接数据库方法//对于jdbc4版本,加载驱动可以选择性省略try {//加载驱动Class.forName("com.mysql.jdbc.Driver");//System.out.println("加载成功");}catch(ClassNotFoundException e) {e.printStackTrace();}try {//连接数据库//两条语句选择性使用,Debug时此处出问题!//conn=DriverManager.getConnection("jdbc:mysql://localhost:3306;DatabaseName=教学信息管理系统","root","123456");conn=DriverManager.getConnection(dbURL,userName,userPwd);stmt=conn.createStatement();}catch(Exception e) {e.printStackTrace();//System.out.println("连接失败!");}}public void closeDB() {  //关闭数据库方法try {rs.close();stmt.close();conn.close();}catch(Exception e) {e.printStackTrace();//System.out.println("关闭失败!");}}//实现ItemListener的抽象方法//对下拉列表进行事件处理public void itemStateChanged(ItemEvent e) {//该高级事件用于监听用户的选定与否if(e.getStateChange()==ItemEvent.SELECTED) {  //判断下拉列表是否选定JComboBox j=(JComboBox)e.getSource();  //强制类型转换+获取事件源index=j.getSelectedIndex();//index1=j.getSelectedItem();}}//该方法用来确认是否在数据库中找到学号public boolean searchsno(String str) {boolean x=false;this.connDB();try {rs=stmt.executeQuery("select * from studentpswd");while(rs.next()) {if(rs.getString("学号").trim().equals(str)) {x=true;}}//return x;}catch(Exception e) {e.printStackTrace();}return x;}//该方法用来确认是否在数据库中找到任课教师职工号public boolean searchtno(String str) {boolean x=false;this.connDB();try {rs=stmt.executeQuery("select * from teacherpswd");while(rs.next()) {if(rs.getString("职工号").trim().equals(str)) {  //在java中,判断字符串是否相同,一定要使用equals函数!!!!!!!!x=true;}}//return x;}catch(Exception e) {e.printStackTrace();}return x;}//该方法用来确认是否能在数据库中找到管理员IDpublic boolean searchmanagerno(String str) {boolean x=false;this.connDB();try {rs=stmt.executeQuery("select * from admin");while(rs.next()) {if(rs.getString("管理员号").trim().equals(str)) {  //在java中,判断字符串是否相同,一定要使用equals函数!!!!!!!!x=true;}}//return x;}catch(Exception e) {e.printStackTrace();}return x;}//该方法用来确认是否在数据库中找到辅导员号public boolean searchcoach(String str) {boolean x=false;this.connDB();try {rs=stmt.executeQuery("select * from coach");while(rs.next()) {if(rs.getString("辅导员号").trim().equals(str)) {x=true;}}//return x;}catch(Exception e) {e.printStackTrace();}return x;}//实现ActionListener的抽象方法//对三个按钮进行事件处理public void actionPerformed(ActionEvent e){Object source=e.getSource();String un;String pw;boolean success= false;  //用于判断是否登录成功if(source==a) {  //如果事件源是“确定”按钮if(usenametext.getText().equals("") || txtPwd.getText().equals("")) {  //判断用户名和密码是否为空//JOptionPane类用作提示框,常见方法:1.showMessageDialog; 2.showOptionDialogJOptionPane.showMessageDialog(null,"登录名和密码不能为空!");}else {//用户名和密码均不为空,进行连接数据库this.connDB();//这里先连接数据库,再执行数据库操作步骤,等效为将一个try{}catch{}分解成几个,效果一样!try {if(index==0) {  //管理员登录//trim()方法:从当前 String 对象移除所有前导空白字符和尾部空白字符if(!searchmanagerno(usenametext.getText().trim())) {JOptionPane.showMessageDialog(null,"对不起,此用户不存在!请重新登录!");usenametext.setText("");txtPwd.setText("");}else {rs=stmt.executeQuery("select * from admin  where 管理员号="+ usenametext.getText().trim());while(rs.next()) {pw=rs.getString(2).trim();if(txtPwd.getText().equals(pw)) {JOptionPane.showMessageDialog(null,"管理员登录成功!");this.setVisible(false);new GLFrame();  //进入管理员界面}else {JOptionPane.showMessageDialog(null,"密码错误!请重试");txtPwd.setText("");}}}}//*此处仍然有bug,就是对于不存在的用户无法准确识别记于11/22,后续完善!*///*if(index==0) {  //管理员登录rs=stmt.executeQuery("select * from 管理员");while(rs.next()) {un=rs.getString(1).trim();pw=rs.getString(2).trim();if(un==usename.getText().trim() && pw==txtPwd.getText().trim()) {success=true;break;}}if(success==false) {JOptionPane.showMessageDialog(null,"登录失败!");}else {JOptionPane.showMessageDialog(null,"管理员登录成功!");this.setVisible(false);//new ManagerFrame();  //进入管理员界面}}*///*该bug已经通过布尔类型的search方法改进,记于12/07!if(index==1) {  //任课教师登录if(!searchtno(usenametext.getText().trim())) {JOptionPane.showMessageDialog(null,"对不起,此用户不存在!请重新登录!");usenametext.setText("");txtPwd.setText("");}else {rs=stmt.executeQuery("select * from teacherpswd  where 职工号="+ usenametext.getText().trim());while(rs.next()) {pw=rs.getString("密码").trim();if(txtPwd.getText().equals(pw)) {JOptionPane.showMessageDialog(null,"任课教师登录成功!");this.setVisible(false);new TeacherFrame(usenametext.getText());  //进入教师界面}else {JOptionPane.showMessageDialog(null,"密码错误!请重试");txtPwd.setText("");}}}}if(index==2) {  //学生登录if(!searchsno(usenametext.getText().trim())) {JOptionPane.showMessageDialog(null,"对不起,此用户不存在!请重新登录!");usenametext.setText("");txtPwd.setText("");}else {rs=stmt.executeQuery("select * from studentpswd where 学号="+ usenametext.getText().trim());while(rs.next()) {pw=rs.getString("密码").trim();if(txtPwd.getText().equals(pw)) {JOptionPane.showMessageDialog(null,"学生登录成功!");this.setVisible(false);new StudentFrame(usenametext.getText());  //进入学生界面}else {JOptionPane.showMessageDialog(null,"密码错误!请重试");txtPwd.setText("");}}}}if(index==3) {  //辅导员登录if(!searchcoach(usenametext.getText().trim())) {JOptionPane.showMessageDialog(null,"对不起,此用户不存在!请重新登录!");usenametext.setText("");txtPwd.setText("");}else {rs=stmt.executeQuery("select * from coach where 辅导员号="+ usenametext.getText().trim());while(rs.next()) {pw=rs.getString("密码").trim();if(txtPwd.getText().equals(pw)) {JOptionPane.showMessageDialog(null,"辅导员登录成功!");this.setVisible(false);new CoachFrame(usenametext.getText());  //进入学生界面}else {JOptionPane.showMessageDialog(null,"密码错误!请重试");txtPwd.setText("");}}}}}catch(Exception e1) {e1.printStackTrace();//System.out.println("连接失败!");}closeDB();}}if(source==b) {   //如果事件源是“重置”按钮usenametext.setText("");    txtPwd.setText("");}if(source==c) {   //如果事件源是“取消”按钮//System.exit(0);this.dispose();new DLFrame();}}public static void main(String[] args){new DLFrame();}
}

GLFrame.java

package sqlyog;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class GLFrame extends JFrame implements ActionListener{///注意//在类里面只有成员变量和方法,切忌在成员变量里直接定义//“管理员”标签JLabel l=new JLabel("--管理员--");//定义面板容器JPanel p1=new JPanel();JPanel p2=new JPanel();//设置字体类型Font f1=new Font("宋体",Font.BOLD,30);Font f2=new Font("幼圆",Font.ITALIC,30);Font f3=new Font("楷体",Font.BOLD,18);Font f4=new Font("隶书",Font.PLAIN,40);//设置6个按钮,以便管理员操作JButton btnTM=new JButton("教师信息管理");JButton btnSM=new JButton("学生信息管理");JButton btnCM=new JButton("课程信息管理");JButton btnSCM=new JButton("成绩信息管理");JButton btnEXIT=new JButton("退出管理系统");public GLFrame() {   //构造方法super("管理员管理页面");setLayout(new FlowLayout());//设置标签的颜色l.setFont(f1);l.setForeground(Color.blue);//设置按钮字体和颜色btnTM.setFont(f3);btnTM.setContentAreaFilled(false);//btnTM.setBackground(Color.blue);btnSM.setFont(f3);btnSM.setContentAreaFilled(false);btnCM.setFont(f3);btnCM.setContentAreaFilled(false);btnSCM.setFont(f3);btnSCM.setContentAreaFilled(false);btnEXIT.setFont(f3);btnEXIT.setContentAreaFilled(false);p1.add(l);p1.setOpaque(false);p2.setLayout(new GridLayout(3,2,10,10));p2.setOpaque(false);p2.add(btnTM);p2.add(btnSM);p2.add(btnCM);p2.add(btnSCM);p2.add(btnEXIT);//布局管理器this.add(p1);this.add(p2);//设置顶层容器的大小、位置、可见性及close功能setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);setSize(350,300);setLocationRelativeTo(null);setVisible(true);btnTM.addActionListener(this);btnSM.addActionListener(this);btnCM.addActionListener(this);btnSCM.addActionListener(this);btnEXIT.addActionListener(this);}public void actionPerformed(ActionEvent e) {//按钮为“教师信息管理”,跳转页面if(e.getSource().equals(btnTM)) {this.dispose();new TM().display();}//按钮为“学生信息管理”,跳转页面if(e.getSource().equals(btnSM)) {//new SM().display();this.dispose();new SM().display();}//按钮为“课程信息管理”,跳转页面if(e.getSource().equals(btnCM)) {this.dispose();new CM().display();}//按钮为“成绩信息管理”,跳转页面if(e.getSource().equals(btnSCM)) {this.dispose();new SCM().display();}//按钮为“退出管理系统”,程序退出if(e.getSource().equals(btnEXIT)) {//System.exit(0);this.dispose();new DLFrame();}}public static void main(String[] args) {new GLFrame();}}

MyPane.java

package sqlyog;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Panel;
import java.util.Random;class MyPanel extends Panel{static String s1;public void paint(Graphics g){int width=70;int height=40;g.setColor(Color.LIGHT_GRAY);g.fillRect(0,0, width, height);g.setColor(Color.green);            //设置上下文颜色g.drawRect(0,0,width-1,height-1); //绘制边框Random r=new Random();  //产生随机数//绘制背景圆点for(int i=0;i<200;i++){int x=r.nextInt(width)-2;int y=r.nextInt(height)-2;g.drawOval(x, y, 2, 2);}g.setFont(new Font("黑体",Font.BOLD,30));g.setColor(Color.yellow);char[] chars=("0123456789"+"abcdefghijklmnopqrstuvwxyz"+"HIJKLMNOPQRSTUVWXYZ").toCharArray();StringBuilder sb=new StringBuilder();for(int i=0;i<4;i++){int pos=r.nextInt(chars.length);char c=chars[pos];sb.append(c+" ");}g.drawString(sb.toString(),20,30);s1=sb.toString();g.setColor(Color.red);for(int i=0;i<200;i++){int x=r.nextInt(width)-2;int y=r.nextInt(height)-2;g.drawOval(x, y, 2, 2);}}public static void main(String[] args) {new MyPanel();}
}

SCM.java

package sqlyog;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.sql.*;
import java.util.*;public class SCM extends JFrame implements ActionListener{//定义菜单按钮Button btnadd=new Button("添加");Button btndelete=new Button("删除");Button btnupdate=new Button("修改");Button btnsearch=new Button("查询");Button btndisplay=new Button("刷新显示");Button btnreturn=new Button("返回");//定义菜单栏JMenuBar mb=new JMenuBar();//定义滚轮面板JScrollPane jsp;JScrollPane jsp1;//定义表格JTable tb;JTable tb1;//定义字体Font f1=new Font("行楷",Font.BOLD,15);Font f2=new Font("宋体",Font.ITALIC,36);//创建连接数据库变量对象Connection conn;Statement stmt;ResultSet rs;//设置端口常量String dbURL="jdbc:mysql://localhost:3306/教学信息管理系统";String username="root";String userpwd="123456";//定义一个Object数组Object[][] arr;public void connDB() { //连接数据库方法try {conn=DriverManager.getConnection(dbURL,username,userpwd);stmt=conn.createStatement();}catch(Exception e) {e.printStackTrace();}}public void closeDB() {  //关闭数据库方法try {rs.close();stmt.close();conn.close();}catch(Exception e) {e.printStackTrace();}}public SCM() {  //无参构造方法super("成绩信息管理");//设置按钮字体和颜色btnadd.setFont(f1);btnadd.setBackground(new Color(131,175,155));btndelete.setFont(f1);btndelete.setBackground(new Color(131,175,155));btnupdate.setFont(f1);btnupdate.setBackground(new Color(131,175,155));btnsearch.setFont(f1);btnsearch.setBackground(new Color(131,175,155));btndisplay.setFont(f1);btndisplay.setBackground(new Color(131,175,155));btnreturn.setFont(f1);btnreturn.setBackground(new Color(131,175,155));//将按钮添加进菜单栏mb.add(btnadd);mb.add(btndelete);mb.add(btnupdate);mb.add(btnsearch);mb.add(btndisplay);mb.add(btnreturn);//连接数据库this.connDB();//注册事件监听器btnadd.addActionListener(this);btndelete.addActionListener(this);btnupdate.addActionListener(this);btnsearch.addActionListener(this);btndisplay.addActionListener(this);btnreturn.addActionListener(this);setSize(500,300);setDefaultCloseOperation(EXIT_ON_CLOSE);setLocationRelativeTo(null);setVisible(true);//将菜单栏添加进容器中this.setJMenuBar(mb);}public void update() {String sno1=null,cno1=null; //局部变量一定要初始化int row=tb.getSelectedRow();  //代表鼠标选定的行数if(row==-1) {  //表示未被选中JOptionPane.showMessageDialog(null,"请选择要修改的信息!");}else {int x=0;try {rs=stmt.executeQuery("select * from grade");while(rs.next() && x<=row) {sno1=rs.getString("学号");cno1=rs.getString("课程名称");x++;}this.dispose();new SCMupdate(sno1,cno1);}catch(Exception e) {e.printStackTrace();}}       }public void display() {int i=0,j=0;//定义一个数组来存储所有数据ArrayList list=new ArrayList();try {rs=stmt.executeQuery("select * from grade ");while(rs.next()) {list.add(rs.getString("学号"));list.add(rs.getString("姓名"));list.add(rs.getString("课程名称"));list.add(rs.getString("成绩"));i++;}}catch(Exception e) {e.printStackTrace();}arr=new Object[i][4];String[] listname= {"学号","姓名","课程名称","成绩"};//用二维数组来存储所有数据try {rs=stmt.executeQuery("select * from grade order by 学号");while(rs.next()) {arr[j][0]=rs.getString("学号");arr[j][1]=rs.getString("姓名");arr[j][2]=rs.getString("课程名称");arr[j][3]=rs.getString("成绩");j++;}}catch(Exception e) {e.printStackTrace();}tb=new JTable(arr,listname);jsp=new JScrollPane(tb);this.add(jsp);}public void delete() {   //删除信息方法String sno=null;  //定义字符变量,来接收学号   int row=tb.getSelectedRow();  //代表鼠标选定的行数if(row==-1) {  //表示未被选中JOptionPane.showMessageDialog(null,"请选择要删除的记录!");}else {int x=0;try {rs=stmt.executeQuery("select * from grade");while(rs.next() && x<=row) {sno=rs.getString("学号");x++;}stmt.executeUpdate("delete from grade where 学号="+sno);   //局部变量一定要初始化JOptionPane.showMessageDialog(null,"删除成功!");this.dispose();new SCM().display();}catch(Exception e) {e.printStackTrace();}}}public void show(String s1) {   //查询结果方法int i=0,j=0;JFrame f=new JFrame("查询结果");//JPanel p=new JPanel();f.setLayout(new FlowLayout());f.setDefaultCloseOperation(EXIT_ON_CLOSE);f.setSize(500,150);f.setVisible(true);f.setLocationRelativeTo(null);Button btnrt=new Button("返回");btnrt.setFont(f1);btnrt.setBackground(new Color(131,175,155));//btnrt.setSize(20,10);this.connDB();ArrayList list=new ArrayList();try {rs=stmt.executeQuery("select * from grade ");while(rs.next()) {list.add(rs.getString("学号"));list.add(rs.getString("姓名"));list.add(rs.getString("课程名称"));list.add(rs.getString("成绩"));i++;}}catch(Exception e) {e.printStackTrace();}arr=new Object[i][4];try {rs=stmt.executeQuery("select * from grade where 学号="+s1);while(rs.next()) {arr[j][0]=rs.getString("学号");arr[j][1]=rs.getString("姓名");arr[j][2]=rs.getString("课程名称");arr[j][3]=rs.getString("成绩");j++;}}catch(Exception e){e.printStackTrace();}String[] listname={"学号","姓名","课程名称","成绩"};tb1=new JTable(arr,listname); //创建表格jsp1=new JScrollPane(tb1);//f.add(btnrt,BorderLayout.NORTH);f.add(btnrt);f.add(jsp1);  //此处有个小bug,只有添加进滚轮面板,才能将头部名称显示出来//f1.add(btnrt); btnrt.addMouseListener(new MouseAdapter() {public void mouseClicked(MouseEvent e) {f.dispose();}});}//该方法用来确认是否在数据库中找到学号public boolean searchtestsno(String str) {boolean x=false;this.connDB();try {rs=stmt.executeQuery("select * from grade");while(rs.next()) {if(rs.getString("学号").trim().equals(str)) {  //在java中,判断字符串是否相同,一定要使用equals函数!!!!!!!!x=true;}}//return x;}catch(Exception e) {e.printStackTrace();}return x;}//该方法用来确认是否在数据库中找到课程名称public boolean searchtestcno(String str) {boolean x=false;this.connDB();try {rs=stmt.executeQuery("select * from grade");while(rs.next()) {if(rs.getString("课程名称").trim().equals(str)) {  //在java中,判断字符串是否相同,一定要使用equals函数!!!!!!!!x=true;}}//return x;}catch(Exception e) {e.printStackTrace();}return x;}public void searchsno() {   //用于学号查询JFrame f=new JFrame("查询");f.setLayout(new FlowLayout());f.setSize(240,180);f.setVisible(true);f.setLocationRelativeTo(null);JPanel p1=new JPanel();JPanel p2=new JPanel();JLabel stuno=new JLabel("输入学号:");JTextField stuno1=new JTextField(10);Button ok=new Button("确定");Button cancel=new Button("取消");p1.add(stuno);p1.add(stuno1);p2.add(ok);p2.add(cancel);f.add(p1);f.add(p2);//为组件注册监听器ok.addMouseListener(new MouseAdapter() {public void mouseClicked(MouseEvent e) {if(stuno1.getText().equals("")) {JOptionPane.showMessageDialog(null,"请输入学号");}else {if(!(searchtestsno(stuno1.getText().trim()))) {f.dispose();JOptionPane.showMessageDialog(null,"对不起,该记录不存在!");}else {f.dispose();            //new SM(stuno1.getText());show(stuno1.getText());}}}});cancel.addMouseListener(new MouseAdapter() {public void mouseClicked(MouseEvent e) {f.dispose();search();}});f.addWindowListener(new WindowAdapter() {public void windowClosing(WindowEvent e) {Window w=(Window)e.getComponent();  w.dispose();}});}public void searchcno() {   //用于课程查询JFrame f=new JFrame("查询");f.setLayout(new FlowLayout());f.setSize(240,180);f.setVisible(true);f.setLocationRelativeTo(null);JPanel p1=new JPanel();JPanel p2=new JPanel();JLabel stuno=new JLabel("输入课程名称:");JTextField stuno1=new JTextField(10);Button ok=new Button("确定");Button cancel=new Button("取消");p1.add(stuno);p1.add(stuno1);p2.add(ok);p2.add(cancel);f.add(p1);f.add(p2);//为组件注册监听器ok.addMouseListener(new MouseAdapter() {public void mouseClicked(MouseEvent e) {if(stuno1.getText().equals("")) {JOptionPane.showMessageDialog(null,"请输入课程名称");}else {if(!(searchtestcno(stuno1.getText().trim()))) {f.dispose();JOptionPane.showMessageDialog(null,"对不起,该记录不存在!");}else {f.dispose();            //new SM(stuno1.getText());show(stuno1.getText());}}}});cancel.addMouseListener(new MouseAdapter() {public void mouseClicked(MouseEvent e) {f.dispose();search();}});f.addWindowListener(new WindowAdapter() {public void windowClosing(WindowEvent e) {Window w=(Window)e.getComponent();  w.dispose();}});}public void search() {  //查询成绩页面,供选择查询方式Frame f=new Frame("成绩查询选择");f.setLayout(new FlowLayout());f.setSize(240,100);f.setLocationRelativeTo(null);f.setVisible(true);Button btnsno=new Button("学号查询");Button btncno=new Button("课程名称查询");Button btncancel=new Button("取消");btnsno.setBackground(new Color(131,175,155));btncno.setBackground(new Color(131,175,155));btncancel.setBackground(Color.GRAY);f.add(btnsno);f.add(btncno);f.add(btncancel);      btnsno.addMouseListener(new MouseAdapter() {public void mouseClicked(MouseEvent e) {f.dispose();searchsno();}});btncno.addMouseListener(new MouseAdapter() {public void mouseClicked(MouseEvent e) {f.dispose();searchcno();}});btncancel.addMouseListener(new MouseAdapter() {public void mouseClicked(MouseEvent e) {f.dispose();}});f.addWindowListener(new WindowAdapter() {public void windowClosing(WindowEvent e) {Window window=(Window)e.getComponent();window.dispose();}});}public void actionPerformed(ActionEvent e) {//对事件进行相对应得处理if(e.getSource()==btnadd) {  this.dispose();new SCMadd();}if(e.getSource()==btndelete) {JOptionPane.showMessageDialog(null,"删除,请谨慎操作!");this.delete();}if(e.getSource()==btnupdate) {//this.dispose();this.update();}if(e.getSource()==btnsearch) {this.search();}if(e.getSource()==btndisplay) {this.dispose();new SCM().display();}if(e.getSource()==btnreturn) {this.dispose();new GLFrame();}}public static void main(String[] args) {// TODO Auto-generated method stubnew SCM().display();}}

SCMadd.java

package sqlyog;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.sql.*;public class SCMadd extends JFrame implements ActionListener{//定义面板Panel p=new Panel();Panel p1=new Panel();Panel p2=new Panel();/*Panel psno=new Panel();Panel psname=new Panel();Panel pssex=new Panel();Panel psage=new Panel();Panel psclass=new Panel();Panel pspwd=new Panel();*///定义属性标签及文本框JLabel sno=new JLabel("学号:");JTextField sno1=new JTextField(12);JLabel name=new JLabel("姓名:");JTextField cname=new JTextField(10);JLabel cno=new JLabel("课程名称:");JTextField cno1=new JTextField(20);JLabel sc=new JLabel("成绩:");JTextField sc1=new JTextField(10);//定义字体Font f1=new Font("宋体",Font.BOLD,20);Font f2=new Font("幼圆",Font.ITALIC,30);Font f3=new Font("楷体",Font.BOLD,18);Font f4=new Font("隶书",Font.PLAIN,40);//定义按钮Button btnsure=new Button("确定");Button btnagain=new Button("重置");Button btncancel=new Button("取消");//定义连接字符String dbURL = "jdbc:mysql://localhost:3306/教学信息管理系统";String userName = "root";String userPwd = "123456";Connection conn;Statement stmt;ResultSet rs;public void connDB() {   //连接数据库方法try {//连接数据库conn=DriverManager.getConnection(dbURL,userName,userPwd);stmt=conn.createStatement();}catch(Exception e) {e.printStackTrace();//System.out.println("连接失败!");}}public void closeDB() {  //关闭数据库方法try {rs.close();stmt.close();conn.close();}catch(Exception e) {e.printStackTrace();//System.out.println("关闭失败!");}}public SCMadd() {  //构造方法super("成绩信息添加页面");setLayout(new FlowLayout());//设置字体sno.setFont(f3);name.setFont(f3);cno.setFont(f3);sc.setFont(f3);//按钮字体、颜色btnsure.setFont(f1);btnsure.setBackground(new Color(131,175,155)); //淡草色btncancel.setFont(f1);btncancel.setBackground(new Color(131,175,155)); //淡草色btnagain.setFont(f1);btnagain.setBackground(new Color(131,175,155)); //淡草色//设置面板的网格布局管理p1.setLayout(new GridLayout(8,2));//p2.setLayout(new GridLayout(4,2));//将标签和文本框添加进面板p1.add(sno);p1.add(sno1);p1.add(name);p1.add(cname);p1.add(cno);p1.add(cno1);p1.add(sc);p1.add(sc1);//将按钮添加进面板p.add(btnsure);p.add(btnagain);p.add(btncancel);//添加面板this.add(p1);//this.add(p2);this.add(p);//设置顶层容器的大小、位置、可见性及close功能setDefaultCloseOperation(EXIT_ON_CLOSE);setSize(500,350);setLocationRelativeTo(null);setVisible(true);//注册监听器btnsure.addActionListener(this);btnagain.addActionListener(this);btncancel.addActionListener(this);}public void insert() {  //插入方法try {this.connDB();//插入数据stmt.executeUpdate("insert into grade values("+"'"+sno1.getText().trim()+"'"+","+"'"+cname.getText().trim()+"'"+","+"'"+cno1.getText().trim()+"'"+","+"'"+sc1.getText().trim()+"'"+")");//System.out.println("insert success!");JOptionPane.showMessageDialog(null,"成绩信息添加成功!");this.dispose();new SCM().display();}catch(Exception e) {e.printStackTrace();}}public boolean searchtest(String s1,String s2) {boolean x=false;this.connDB();try {rs=stmt.executeQuery("select * from grade");while(rs.next()) {if(rs.getString("学号").trim().equals(s1) && rs.getString("课程名称").trim().equals(s2)) {  //在java中,判断字符串是否相同,一定要使用equals函数!!!!!!!!x=true;}}//return x;}catch(Exception e) {e.printStackTrace();}return x;}public void actionPerformed(ActionEvent e) {if(e.getSource()==btnsure) {if(sno1.getText().equals("") || cname.getText().equals("") || cno1.getText().equals("") || sc1.getText().equals("") ) {JOptionPane.showMessageDialog(null,"信息不能为空!");}else if(searchtest(sno1.getText(),cno1.getText())){JOptionPane.showMessageDialog(null,"该条成绩记录已经存在!请重新添加!");sno1.setText("");cname.setText("");cno1.setText("");sc1.setText("");}else {this.insert();}}if(e.getSource()==btnagain) {sno1.setText("");cname.setText("");cno1.setText("");sc1.setText("");}if(e.getSource()==btncancel) {this.dispose();new SCM().display();;//System.exit(0);}}public static void main(String[] args) {// TODO Auto-generated method stubnew SCMadd();}}

SCmg.java

package sqlyog;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.sql.*;
import java.util.*;public class SCmg extends JFrame implements ActionListener{Font f1=new Font("宋体",Font.BOLD,30);Font f2=new Font("幼圆",Font.ITALIC,30);Font f3=new Font("楷体",Font.BOLD,17);Font f4=new Font("隶书",Font.PLAIN,40);Button btnrt=new Button("返回");Button btnchange=new Button("修改");String dbURL="jdbc:mysql://localhost:3306/教学信息管理系统";String useName="root";String usePwd="123456";Connection conn;Statement stmt;ResultSet rs;JTable tb;JScrollPane scroll;//int row;Object[][] arr;String csname;String csno;String tno;public void connDB(){  //连接数据库方法try {conn=DriverManager.getConnection(dbURL,useName,usePwd);stmt=conn.createStatement();}catch(Exception e) {e.printStackTrace();}}public void closeDB() {  //关闭数据库方法try {rs.close();stmt.close();conn.close();}catch(Exception e) {e.printStackTrace();}}public SCmg(String s1,String s2) {tno=s1;csno=s2;this.connDB();try {rs=stmt.executeQuery("select distinct 姓名 from student where 学号="+"'"+s2+"'");while(rs.next()) {csname=rs.getString("姓名").trim();}}catch(Exception e) {e.printStackTrace();}this.setTitle(csname+" --学生成绩信息");setLayout(new FlowLayout());setDefaultCloseOperation(EXIT_ON_CLOSE);setSize(500,150);setVisible(true);setLocationRelativeTo(null);btnchange.setFont(f3);btnchange.setBackground(new Color(131,175,155));btnrt.setFont(f3);btnrt.setBackground(new Color(131,175,155));int i=0,j=0;ArrayList al=new ArrayList();try {rs=stmt.executeQuery("select * from grade where 学号='"+s2+"'");while(rs.next()) {al.add(rs.getString("学号"));al.add(rs.getString("姓名"));al.add(rs.getString("课程名称"));al.add(rs.getString("成绩"));i++;}}catch(Exception e){e.printStackTrace();}arr=new Object[i][4];try {rs=stmt.executeQuery("select * from grade where 学号='"+s2+"'");while(rs.next()) {arr[j][0]=rs.getString("学号");arr[j][1]=rs.getString("姓名");arr[j][2]=rs.getString("课程名称");arr[j][3]=rs.getString("成绩");j++;}}catch(Exception e){e.printStackTrace();}String[] list= {"学号","姓名","课程名称","成绩"};tb=new JTable(arr,list); //创建表格scroll=new JScrollPane(tb);//f.add(btnrt,BorderLayout.NORTH);add(btnchange);add(btnrt);add(scroll);  //此处有个小bug,只有添加进滚轮面板,才能将头部名称显示出来//row=tb.getSelectedRow();btnchange.addActionListener(this);btnrt.addActionListener(this);}public void scchange(String sno,String cno) {  //成绩修改精确到个人页面String sname=null; String cname=null;this.connDB();try {rs=stmt.executeQuery("select * from grade where 学号="+sno+"and 课程名称="+cno);while(rs.next()) {sname=rs.getString("姓名").trim();cname=rs.getString("课程名称").trim();}}catch(Exception e) {e.printStackTrace();}JFrame f=new JFrame(sname+"同学 --成绩页面");f.setLayout(new FlowLayout());f.setLocation(470,280);f.setDefaultCloseOperation(EXIT_ON_CLOSE);f.setSize(320,200);f.setVisible(true);JPanel p=new JPanel();p.setLayout(new GridLayout(4,2));JLabel l0=new JLabel("学号");JTextField tf0=new JTextField(10);tf0.setText(sno);tf0.setEditable(false);JLabel l1=new JLabel("姓名");JTextField tf1=new JTextField(10);tf1.setText(sname);tf1.setEditable(false);JLabel l2=new JLabel("课程名称");JTextField tf2=new JTextField(10);tf2.setText(cname);tf2.setEditable(false);JLabel l3=new JLabel("成绩");JTextField tf3=new JTextField(10);JButton btn1=new JButton("确定");btn1.setFont(f3);JButton btn2=new JButton("取消");btn2.setFont(f3);p.setOpaque(false);p.add(l0);p.add(tf0);p.add(l1);p.add(tf1);p.add(l2);p.add(tf2);p.add(l3);p.add(tf3);f.add(p);f.add(btn1);f.add(btn2);btn1.addMouseListener(new MouseAdapter() {  //确认数据更新public void mouseClicked(MouseEvent e) {f.dispose();try {stmt.executeUpdate("update grade set 成绩="+"'"+tf3.getText()+"'"+" where 学号="+sno+"and 课程名称="+cno);JOptionPane.showMessageDialog(null,"成绩修改成功!");new TeacherFrame(tno);}catch(Exception e1) {e1.printStackTrace();}}});btn2.addMouseListener(new MouseAdapter() {public void mouseClicked(MouseEvent e) {f.dispose();}});}public void se() {this.connDB();int row=tb.getSelectedRow();if(row==-1) {  //表示未被选中JOptionPane.showMessageDialog(null,"请选择要修改的信息!");}else {//int x=0;try {String sno=(String)tb.getModel().getValueAt(row,0);String cno=(String)tb.getModel().getValueAt(row,2);this.dispose();scchange(sno,cno);}catch(Exception e1) {e1.printStackTrace();}}}public void actionPerformed(ActionEvent e) {if(e.getSource()==btnchange) {se();}if(e.getSource()==btnrt) {this.dispose();new TeacherFrame(tno).scmg(tno);}}public static void main(String[] args) {// TODO Auto-generated method stubnew SCmg("201903060410","形势与政策");}}

SCMupdate.java

package sqlyog;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.sql.*;public class SCMupdate extends JFrame implements ActionListener{//定义面板Panel p=new Panel();Panel p1=new Panel();Panel p2=new Panel();/*Panel psno=new Panel();Panel psname=new Panel();Panel pssex=new Panel();Panel psage=new Panel();Panel psclass=new Panel();Panel pspwd=new Panel();*///定义属性标签及文本框JLabel sno=new JLabel("学号:");JTextField sno1=new JTextField(12);JLabel name=new JLabel("姓名:");JTextField cname=new JTextField(10);JLabel cno=new JLabel("课程名称:");JTextField cno1=new JTextField(20);JLabel sc=new JLabel("成绩:");JTextField sc1=new JTextField(10);//定义字体Font f1=new Font("宋体",Font.BOLD,20);Font f2=new Font("幼圆",Font.ITALIC,30);Font f3=new Font("楷体",Font.BOLD,18);Font f4=new Font("隶书",Font.PLAIN,40);//定义按钮Button btnsure=new Button("确定");Button btnagain=new Button("重置");Button btncancel=new Button("取消");//定义连接字符String dbURL = "jdbc:mysql://localhost:3306/教学信息管理系统";String userName = "root";String userPwd = "123456";Connection conn;Statement stmt;ResultSet rs;public void connDB() {   //连接数据库方法try {//连接数据库conn=DriverManager.getConnection(dbURL,userName,userPwd);stmt=conn.createStatement();}catch(Exception e) {e.printStackTrace();//System.out.println("连接失败!");}}public void closeDB() {  //关闭数据库方法try {rs.close();stmt.close();conn.close();}catch(Exception e) {e.printStackTrace();//System.out.println("关闭失败!");}}public SCMupdate(String s1,String s2) {  //含参构造方法super("成绩信息修改页面");setLayout(new FlowLayout());sno1.setText(s1);sno1.setEditable(false);cno1.setText(s2);cno1.setEditable(false);//设置字体sno.setFont(f3);name.setFont(f3);cno.setFont(f3);sc.setFont(f3);//按钮字体、颜色btnsure.setFont(f1);btnsure.setBackground(new Color(131,175,155)); //淡草色btncancel.setFont(f1);btncancel.setBackground(new Color(131,175,155)); //淡草色btnagain.setFont(f1);btnagain.setBackground(new Color(131,175,155)); //淡草色//设置面板的网格布局管理p1.setLayout(new GridLayout(4,2));//p2.setLayout(new GridLayout(4,2));//将标签和文本框添加进面板p1.add(sno);p1.add(sno1);p1.add(name);p1.add(cname);p1.add(cno);p1.add(cno1);p1.add(sc);p1.add(sc1);//将按钮添加进面板p.add(btnsure);p.add(btnagain);p.add(btncancel);//添加面板this.add(p1);//this.add(p2);this.add(p);//设置顶层容器的大小、位置、可见性及close功能setDefaultCloseOperation(EXIT_ON_CLOSE);setSize(500,350);setLocationRelativeTo(null);setVisible(true);//注册监听器btnsure.addActionListener(this);btnagain.addActionListener(this);btncancel.addActionListener(this);}public void insert() {  //插入方法try {this.connDB();//插入数据stmt.executeUpdate("insert into grade values("+"'"+sno1.getText().trim()+"'"+","+"'"+cname.getText().trim()+"'"+"'"+cno1.getText().trim()+"'"+","+"'"+sc1.getText().trim()+"'"+")");//System.out.println("insert success!");JOptionPane.showMessageDialog(null,"成绩信息添加成功!");this.dispose();new CM().display();}catch(Exception e) {e.printStackTrace();}}public void delete(String s1,String s2) {   //删除信息方法try {//stmt.executeUpdate("delete  from 教师表  where 教师号="+s);   //局部变量一定要初始化stmt.executeUpdate("delete  from grade where 学号="+s1+"and 课程名称="+s2);//JOptionPane.showMessageDialog(null,"删除成功!");}catch(Exception e) {e.printStackTrace();}}public void actionPerformed(ActionEvent e) {if(e.getSource()==btnsure) {if(sno1.getText().equals("") || cname.getText().equals("") ||cno1.getText().equals("") || sc1.getText().equals("") ) {JOptionPane.showMessageDialog(null,"信息不能为空!");}else {this.delete(sno1.getText(),cno1.getText());this.insert();}}if(e.getSource()==btnagain) {sno1.setText("");cname.setText("");cno1.setText("");sc1.setText("");}if(e.getSource()==btncancel) {this.dispose();new SCM().display();;//System.exit(0);}}public static void main(String[] args) {// TODO Auto-generated method stubnew SCMupdate(" "," ");}
}

SM.java

package sqlyog;
import java.util.*;
import java.sql.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;public class SM extends JFrame implements ActionListener{   //学生管理系统//定义面板//JPanel p1=new JPanel();//JPanel p2=new JPanel();//创建5个按钮Button btnAdd=new Button("添加");Button btnDelete=new Button("删除");Button btnUpdate=new Button("修改");Button btnSearch=new Button("查询");Button btnDisplay=new Button("刷新显示");Button btnCancel=new Button("返回");//设置字体类型Font f1=new Font("宋体",Font.BOLD,30);Font f2=new Font("幼圆",Font.ITALIC,30);Font f3=new Font("行楷",Font.BOLD,15);Font f4=new Font("隶书",Font.PLAIN,40);//创建菜单栏JMenuBar mb=new JMenuBar();//定义表格JTable stable;JTable stable1;//定义滚动栏JScrollPane scroll;JScrollPane scroll1;//定义连接数据库对象//连接数据库和调用数据库Connection conn;Statement stmt;ResultSet rs;//定义连接字符String dbURL = "jdbc:mysql://localhost:3306/教学信息管理系统";String userName = "root";String userPwd = "123456";//定义数组Object[][] arr;  //由于Object为所有类的父类,可以接收任意数据类型的数组元素public void connDB() {   //连接数据库方法try {//连接数据库conn=DriverManager.getConnection(dbURL,userName,userPwd);stmt=conn.createStatement();}catch(Exception e) {e.printStackTrace();//System.out.println("连接失败!");}}public void closeDB() {  //关闭数据库方法try {rs.close();stmt.close();conn.close();}catch(Exception e) {e.printStackTrace();//System.out.println("关闭失败!");}}public SM() {super("学生信息管理页面");//布局管理//this.add("South",p1);//this.add("center",p1);//设置字体btnAdd.setFont(f3);btnAdd.setBackground(new Color(131,175,155)); //淡草色btnDelete.setFont(f3);btnDelete.setBackground(new Color(131,175,155));btnUpdate.setFont(f3);btnUpdate.setBackground(new Color(131,175,155));btnSearch.setFont(f3);btnSearch.setBackground(new Color(131,175,155));btnDisplay.setFont(f3);btnDisplay.setBackground(new Color(131,175,155));btnCancel.setFont(f3);btnCancel.setBackground(new Color(131,175,155));//将按钮添加进菜单栏中,与直接放进面板的按钮不同mb.add(btnAdd);mb.add(btnDelete);mb.add(btnUpdate);mb.add(btnSearch);mb.add(btnDisplay);mb.add(btnCancel);//连接数据库this.connDB();//注册时间监听器btnAdd.addActionListener(this);btnDelete.addActionListener(this);btnUpdate.addActionListener(this);btnSearch.addActionListener(this);btnDisplay.addActionListener(this);btnCancel.addActionListener(this);//设置窗口的大小和位置setSize(500,300);setLocationRelativeTo(null);setDefaultCloseOperation(EXIT_ON_CLOSE);this.setJMenuBar(mb);  //添加菜单栏以后则不需要用监听器来关闭窗口this.setVisible(true);}public void display() {  //显示所有学生的信息int i=0,j=0,k=0;//长度可变数组集合ArrayList al=new ArrayList();//注意:该try{}catch{}目的即求数据库中数据的组数try {rs=stmt.executeQuery("select * from student");while(rs.next()) {   //首先用数组al来接收所有信息//注意:如果trim()匹配到null数据,则数据库会报错al.add(rs.getString("学号").trim());al.add(rs.getString("姓名").trim());al.add(rs.getString("班级").trim());i++;  //用来统计信息组数}}catch(Exception e){e.printStackTrace();}//定义二维数组arr=new Object[i][6];//创建菜单栏头部名String[] columnNames= {"学号","姓名","班级"};//该代码块才是最终对信息进行分组try {rs=stmt.executeQuery("select * from student order by 学号");while(rs.next()) {   //首先用数组al来接收所有信息arr[j][0]=rs.getString("学号").trim();arr[j][1]=rs.getString("姓名").trim();arr[j][2]=rs.getString("班级").trim();j++;}}catch(Exception e){e.printStackTrace();}stable=new JTable(arr,columnNames); //创建表格scroll=new JScrollPane(stable);this.add(scroll);  //创建滚条}public void delete() {   //删除信息方法String sno=null;  //定义字符变量,来接收学号   int row=stable.getSelectedRow();  //代表鼠标选定的行数if(row==-1) {  //表示未被选中JOptionPane.showMessageDialog(null,"请选择要删除的记录!");}else {int x=0;try {rs=stmt.executeQuery("select * from student");while(rs.next() && x<=row) {sno=rs.getString("学号");x++;}stmt.executeUpdate("delete  from student where 学号="+sno);   //局部变量一定要初始化stmt.executeUpdate("delete  from grade where 学号="+sno);JOptionPane.showMessageDialog(null,"删除成功!");this.dispose();new SM().display();}catch(Exception e) {e.printStackTrace();}}}public void update() {String sno1=null; //局部变量一定要初始化int row=stable.getSelectedRow();  //代表鼠标选定的行数if(row==-1) {  //表示未被选中JOptionPane.showMessageDialog(null,"请选择要修改的信息!");}else {int x=0;try {rs=stmt.executeQuery("select * from student");while(rs.next() && x<=row) {sno1=rs.getString("学号");x++;}this.dispose();new SMupdate(sno1);}catch(Exception e) {e.printStackTrace();}}}public void show(String str) {   //查询结果方法JFrame f1=new JFrame("查询结果");f1.setLayout(new FlowLayout());f1.setDefaultCloseOperation(EXIT_ON_CLOSE);f1.setSize(500,150);f1.setVisible(true);f1.setLocationRelativeTo(null);Button btnrt=new Button("返回");btnrt.setFont(f3);btnrt.setBackground(new Color(131,175,155));this.connDB();arr=new Object[1][6];try {rs=stmt.executeQuery("select * from student  where 学号="+str);while(rs.next()) {arr[0][0]=rs.getString("学号");arr[0][1]=rs.getString("姓名");arr[0][2]=rs.getString("班级");}}catch(Exception e){e.printStackTrace();}String[] columnNames= {"学号","姓名","班级"};stable1=new JTable(arr,columnNames); //创建表格scroll1=new JScrollPane(stable1);f1.add(btnrt);f1.add(scroll1);  //此处有个小bug,只有添加进滚轮面板,才能将头部名称显示出来//f1.add(btnrt);btnrt.addMouseListener(new MouseAdapter() {public void mouseClicked(MouseEvent e) {f1.dispose();}});}//该方法用来确认是否在数据库中找到学号public boolean searchtest(String str) {boolean x=false;this.connDB();try {rs=stmt.executeQuery("select * from student");while(rs.next()) {if(rs.getString("学号").trim().equals(str)) {x=true;}}//return x;}catch(Exception e) {e.printStackTrace();}return x;}public void search() {   //等效于将一个窗口写在方法里面JFrame f=new JFrame("查询");f.setLayout(new FlowLayout());f.setSize(240,180);f.setVisible(true);f.setLocationRelativeTo(null);JPanel p1=new JPanel();JPanel p2=new JPanel();JLabel stuno=new JLabel("输入学号:");JTextField stuno1=new JTextField(10);Button ok=new Button("确定");Button cancel=new Button("取消");p1.add(stuno);p1.add(stuno1);p2.add(ok);p2.add(cancel);f.add(p1);f.add(p2);//为组件注册监听器ok.addMouseListener(new MouseAdapter() {public void mouseClicked(MouseEvent e) {if(stuno1.getText().equals("")) {JOptionPane.showMessageDialog(null,"请输入学号");}else {if(!(searchtest(stuno1.getText().trim()))) {f.dispose();JOptionPane.showMessageDialog(null,"对不起,该学生不存在!");}else {f.dispose();            //new SM(stuno1.getText());show(stuno1.getText());}}}});cancel.addMouseListener(new MouseAdapter() {public void mouseClicked(MouseEvent e) {f.dispose();//}});f.addWindowListener(new WindowAdapter() {public void windowClosing(WindowEvent e) {Window w=(Window)e.getComponent();  w.dispose();}});}public void actionPerformed(ActionEvent e) {if(e.getSource()==btnAdd) {this.dispose();new SMadd();}if(e.getSource()==btnDelete) {JOptionPane.showMessageDialog(null,"删除,请谨慎操作!");this.delete();}if(e.getSource()==btnUpdate) {this.update(); }if(e.getSource()==btnSearch) {this.search(); }if(e.getSource()==btnDisplay) {this.dispose();new SM().display();}if(e.getSource()==btnCancel) {this.dispose();new GLFrame();}}public static void main(String[] args) {// TODO Auto-generated method stubnew SM().display();}}

SMadd.java

package sqlyog;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.sql.*;public class SMadd extends JFrame implements ActionListener,ItemListener{//定义面板Panel p=new Panel();Panel p1=new Panel();Panel p2=new Panel();/*Panel psno=new Panel();Panel psname=new Panel();Panel pssex=new Panel();Panel psage=new Panel();Panel psclass=new Panel();Panel pspwd=new Panel();*///定义属性标签及文本框JLabel no=new JLabel("学号:");JTextField sno=new JTextField(10);JLabel name=new JLabel("姓名:");JTextField sname=new JTextField(20);JLabel classno=new JLabel("班级:");JTextField sclassno=new JTextField(20);//定义字体Font f1=new Font("宋体",Font.BOLD,20);Font f2=new Font("幼圆",Font.ITALIC,30);Font f3=new Font("楷体",Font.BOLD,18);Font f4=new Font("隶书",Font.PLAIN,40);//定义按钮Button btnsure=new Button("确定");Button btnagain=new Button("重置");Button btncancel=new Button("取消");//定义连接字符String dbURL = "jdbc:mysql://localhost:3306/教学信息管理系统";String userName = "root";String userPwd = "123456";Connection conn;Statement stmt;ResultSet rs;public void connDB() {   //连接数据库方法try {//连接数据库conn=DriverManager.getConnection(dbURL,userName,userPwd);stmt=conn.createStatement();}catch(Exception e) {e.printStackTrace();//System.out.println("连接失败!");}}public void closeDB() {  //关闭数据库方法try {rs.close();stmt.close();conn.close();}catch(Exception e) {e.printStackTrace();//System.out.println("关闭失败!");}}public SMadd() {  //构造方法super("学生信息添加页面");setLayout(new FlowLayout());//设置字体no.setFont(f3);name.setFont(f3);classno.setFont(f3);//按钮字体、颜色btnsure.setFont(f1);btnsure.setBackground(new Color(131,175,155)); //淡草色btncancel.setFont(f1);btncancel.setBackground(new Color(131,175,155)); //淡草色btnagain.setFont(f1);btnagain.setBackground(new Color(131,175,155)); //淡草色//设置面板的网格布局管理p1.setLayout(new GridLayout(7,2));//p2.setLayout(new GridLayout(4,2));//将标签和文本框添加进面板p1.add(no);p1.add(sno);p1.add(name);p1.add(sname);p1.add(classno);p1.add(sclassno);//将按钮添加进面板p.add(btnsure);p.add(btnagain);p.add(btncancel);//添加面板this.add(p1);//this.add(p2);this.add(p);//设置顶层容器的大小、位置、可见性及close功能setDefaultCloseOperation(EXIT_ON_CLOSE);setSize(500,350);setLocationRelativeTo(null);setVisible(true);//注册监听器btnsure.addActionListener(this);btnagain.addActionListener(this);btncancel.addActionListener(this);}public void itemStateChanged(ItemEvent e) {if(e.getStateChange()==ItemEvent.SELECTED) {JComboBox j=(JComboBox)e.getSource();}}public void insert() {  //添加插入方法//用来插入数据try {this.connDB();//插入数据stmt.executeUpdate("insert into student values("+"'"+sno.getText().trim()+"'"+","+"'"+sname.getText().trim()+"'"+","+"'"+sclassno.getText().trim()+"'"+")");//System.out.println("insert success!");JOptionPane.showMessageDialog(null,"信息添加成功!");this.dispose();new SM();}catch(Exception e) {e.printStackTrace();}}    //用来寻找学号是否存在public boolean searchtest(String str) {boolean x=false;this.connDB();try {rs=stmt.executeQuery("select * from student");while(rs.next()) {if(rs.getString("学号").trim().equals(str)) {x=true;}}//return x;}catch(Exception e) {e.printStackTrace();}return x;}public void actionPerformed(ActionEvent e) {if(e.getSource()==btnsure) {//先判断填入信息是否为空,在判断密码是否正确if(sno.getText().equals("") || sname.getText().equals("") || sclassno.getText().equals("")) {JOptionPane.showMessageDialog(null,"信息不能为空!");}else if(searchtest(sno.getText())) {JOptionPane.showMessageDialog(null,"该学生已存在");sno.setText("");sname.setText("");sclassno.setText("");}else {this.insert();}}if(e.getSource()==btnagain) {sno.setText("");sname.setText("");sclassno.setText("");}if(e.getSource()==btncancel) {this.dispose();new SM().display();;//System.exit(0);}}public static void main(String[] args) {// TODO Auto-generated method stubnew SMadd();}}

SMupdate.java

package sqlyog;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.sql.*;public class SMupdate extends JFrame implements ActionListener,ItemListener{//定义面板Panel p=new Panel();Panel p1=new Panel();Panel p2=new Panel();/*Panel psno=new Panel();Panel psname=new Panel();Panel pssex=new Panel();Panel psage=new Panel();Panel psclass=new Panel();Panel pspwd=new Panel();*///定义属性标签及文本框JLabel no=new JLabel("学号:");JTextField sno=new JTextField(10);JLabel name=new JLabel("姓名:");JTextField sname=new JTextField(20);JLabel classno=new JLabel("班级:");JTextField sclassno=new JTextField(20);//定义字体Font f1=new Font("宋体",Font.BOLD,20);Font f2=new Font("幼圆",Font.ITALIC,30);Font f3=new Font("楷体",Font.BOLD,18);Font f4=new Font("隶书",Font.PLAIN,40);//定义按钮Button btnsure=new Button("确定");Button btnagain=new Button("重置");Button btncancel=new Button("取消");//定义连接字符String dbURL = "jdbc:mysql://localhost:3306/教学信息管理系统";String userName = "root";String userPwd = "123456";Connection conn;Statement stmt;ResultSet rs;public void connDB() {   //连接数据库方法try {//连接数据库conn=DriverManager.getConnection(dbURL,userName,userPwd);stmt=conn.createStatement();}catch(Exception e) {e.printStackTrace();//System.out.println("连接失败!");}}public void closeDB() {  //关闭数据库方法try {rs.close();stmt.close();conn.close();}catch(Exception e) {e.printStackTrace();//System.out.println("关闭失败!");}}public SMupdate(String str) {  //含参构造方法super("学生信息修改页面");setLayout(new FlowLayout());sno.setText(str);sno.setEditable(false);//设置字体no.setFont(f3);name.setFont(f3);classno.setFont(f3);//按钮字体、颜色btnsure.setFont(f1);btnsure.setBackground(new Color(131,175,155)); //淡草色btncancel.setFont(f1);btncancel.setBackground(new Color(131,175,155)); //淡草色btnagain.setFont(f1);btnagain.setBackground(new Color(131,175,155)); //淡草色//设置面板的网格布局管理p1.setLayout(new GridLayout(7,2));//p2.setLayout(new GridLayout(4,2));//将标签和文本框添加进面板p1.add(no);p1.add(sno);p1.add(name);p1.add(sname);p1.add(classno);p1.add(sclassno);//将按钮添加进面板p.add(btnsure);p.add(btnagain);p.add(btncancel);//添加面板this.add(p1);//this.add(p2);this.add(p);//设置顶层容器的大小、位置、可见性及close功能setDefaultCloseOperation(EXIT_ON_CLOSE);setSize(500,350);setLocationRelativeTo(null);setVisible(true);//注册监听器btnsure.addActionListener(this);btnagain.addActionListener(this);btncancel.addActionListener(this);}public void itemStateChanged(ItemEvent e) {if(e.getStateChange()==ItemEvent.SELECTED) {JComboBox j=(JComboBox)e.getSource();}}public void insert() {  //添加插入方法//用来插入数据try {this.connDB();//插入数据stmt.executeUpdate("insert into student values("+"'"+sno.getText().trim()+"'"+","+"'"+sname.getText().trim()+"'"+","+sclassno.getText().trim()+"'"+")");//System.out.println("insert success!");JOptionPane.showMessageDialog(null,"信息修改成功!");this.dispose();new SM().display();}catch(Exception e) {e.printStackTrace();}}    public void delete(String s) {   //删除信息方法try {stmt.executeUpdate("delete  from grade where 学号="+s);stmt.executeUpdate("delete  from student where 学号="+s);   //局部变量一定要初始化   //JOptionPane.showMessageDialog(null,"删除成功!");}catch(Exception e) {e.printStackTrace();}
}public void actionPerformed(ActionEvent e) {if(e.getSource()==btnsure) {//先判断填入信息是否为空,在判断密码是否正确if(sno.getText().equals("") || sname.getText().equals("") || sclassno.getText().equals("")) {JOptionPane.showMessageDialog(null,"信息不能为空!");}else {this.delete(sno.getText());this.insert();}}if(e.getSource()==btnagain) {sno.setText("");sname.setText("");sclassno.setText("");}if(e.getSource()==btncancel) {this.dispose();new SM().display();;//System.exit(0);}}public static void main(String[] args) {// TODO Auto-generated method stubnew SMupdate(" ");}}

StudentFrame.java

package sqlyog;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.sql.*;
import java.util.ArrayList;public class StudentFrame extends JFrame implements ActionListener{///注意//在类里面只有成员变量和方法,切忌在成员变量里直接定义//“管理员”标签JLabel l=new JLabel("--学生页面--");//定义面板容器JPanel p1=new JPanel();JPanel p2=new JPanel();//设置字体类型Font f1=new Font("宋体",Font.BOLD,30);Font f2=new Font("幼圆",Font.ITALIC,20);Font f3=new Font("楷体",Font.BOLD,15);Font f4=new Font("隶书",Font.PLAIN,18);//设置6个按钮,以便管理员操作Button btnSelf=new Button("个人信息管理");Button btnSCsh=new Button("成绩信息查询");Button btnCsh=new Button("课程信息查询");Button btnEXIT=new Button("退出");//String dbURL="jdbc:mysql://localhost:3306/教学信息管理系统";String userName="root";String userPwd="123456";//Connection conn;Statement stmt;ResultSet rs;//Object[][] arr;String sno;String spwd;JScrollPane scroll1;JScrollPane jsp1;JTable stable1;JTable tb1;public void connDB() {try {conn=DriverManager.getConnection(dbURL,userName,userPwd);stmt=conn.createStatement();}catch(Exception e) {e.printStackTrace();}}public void closeDB() {try {rs.close();stmt.close();conn.close();}catch(Exception e) {e.printStackTrace();}}public StudentFrame(String str) {   //构造方法super("学生页面");setLayout(new FlowLayout());//设置标签的颜色l.setFont(f1);l.setForeground(Color.blue);//设置按钮字体和颜色btnSelf.setFont(f2);btnSelf.setBackground(new Color(0,255,255));btnSCsh.setFont(f3);btnSCsh.setBackground(new Color(255,160,122));btnCsh.setFont(f4);btnCsh.setBackground(new Color(30,144,255));btnEXIT.setFont(f2);btnEXIT.setBackground(new Color(220,20,60));p1.add(l);p1.setOpaque(false);p2.setLayout(new GridLayout(4,2,10,10));p2.setOpaque(false);p2.add(btnSelf);p2.add(btnSCsh);p2.add(btnCsh);p2.add(btnEXIT);this.connDB();this.sno=str;//布局管理器this.add(p1);this.add(p2);//设置顶层容器的大小、位置、可见性及close功能setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);setSize(350,300);setLocationRelativeTo(null);setVisible(true);btnSelf.addActionListener(this);btnSCsh.addActionListener(this);btnCsh.addActionListener(this);btnEXIT.addActionListener(this);}public void csearch(String str) {   //课程查询方法JFrame f=new JFrame("课程信息页面");//JPanel p=new JPanel();f.setLayout(new FlowLayout());f.setDefaultCloseOperation(EXIT_ON_CLOSE);f.setSize(500,150);f.setVisible(true);f.setLocationRelativeTo(null);Button btnrt=new Button("返回");btnrt.setFont(f3);btnrt.setBackground(new Color(131,175,155));//btnrt.setSize(20,10);int i=0,j=0;ArrayList ar=new ArrayList();this.connDB();try {rs=stmt.executeQuery("select * from course");while(rs.next()){ar.add(rs.getString("课程类别"));ar.add(rs.getString("课程编号"));ar.add(rs.getString("课程名称"));ar.add(rs.getString("考核模式"));ar.add(rs.getInt("学分"));ar.add(rs.getInt("学时"));ar.add(rs.getString("课程备注"));ar.add(rs.getString("课程性质"));ar.add(rs.getString("学年"));j++;}}catch(Exception e) {e.printStackTrace();}arr=new Object[j][9];try {rs=stmt.executeQuery("select * from course");while(rs.next()) {arr[j][0]=rs.getString("课程类别");arr[j][1]=rs.getString("课程编号");arr[j][2]=rs.getString("课程名称");arr[j][3]=rs.getString("考核模式");arr[j][4]=rs.getInt("学分");arr[j][5]=rs.getInt("学时");arr[j][6]=rs.getString("课程备注");arr[j][7]=rs.getString("课程性质");arr[j][8]=rs.getString("学年");i++;}}catch(Exception e){e.printStackTrace();}String[] list={"课程类别","课程编号","课程名称","考核模式","学分","学时","课程备注","课程性质","学年"};tb1=new JTable(arr,list); //创建表格jsp1=new JScrollPane(tb1);//f.add(btnrt,BorderLayout.NORTH);f.add(btnrt);f.add(jsp1);  //此处有个小bug,只有添加进滚轮面板,才能将头部名称显示出来//f1.add(btnrt); btnrt.addMouseListener(new MouseAdapter() {public void mouseClicked(MouseEvent e) {f.dispose();new StudentFrame(sno);}});}public void scsearch(String str) {  //查询成绩页面int i=0,j=0;JFrame f=new JFrame("成绩信息页面");//JPanel p=new JPanel();f.setLayout(new FlowLayout());f.setDefaultCloseOperation(EXIT_ON_CLOSE);f.setSize(500,200);f.setVisible(true);f.setLocationRelativeTo(null);Button btnrt=new Button("返回");btnrt.setFont(f3);btnrt.setBackground(new Color(131,175,155));//btnrt.setSize(20,10);this.connDB();ArrayList list=new ArrayList();try {rs=stmt.executeQuery("select * from grade where 学号="+str);while(rs.next()) {list.add(rs.getString("学号"));list.add(rs.getString("姓名"));list.add(rs.getString("课程名称"));list.add(rs.getString("成绩"));i++;}}catch(Exception e) {e.printStackTrace();}arr=new Object[i][4];try {rs=stmt.executeQuery("select * from grade");while(rs.next()) {arr[j][0]=rs.getString("学号");arr[j][1]=rs.getString("姓名");arr[j][2]=rs.getString("课程名称");arr[j][3]=rs.getString("成绩");j++;}}catch(Exception e){e.printStackTrace();}String[] listname={"学号","姓名","课程名称","成绩"};tb1=new JTable(arr,listname); //创建表格jsp1=new JScrollPane(tb1);//f.add(btnrt,BorderLayout.NORTH);f.add(btnrt);f.add(jsp1);  //此处有个小bug,只有添加进滚轮面板,才能将头部名称显示出来//f1.add(btnrt); btnrt.addMouseListener(new MouseAdapter() {public void mouseClicked(MouseEvent e) {f.dispose();new StudentFrame(str);}});       }public void display(String str) {   //用来显示个人信息JFrame f1=new JFrame("个人信息");f1.setLayout(new FlowLayout());f1.setSize(500,150);f1.setVisible(true);f1.setLocationRelativeTo(null);Button btnchange=new Button("修改密码");Button btnrt=new Button("返回");btnchange.setFont(f3);btnchange.setBackground(new Color(131,175,155));btnrt.setFont(f3);btnrt.setBackground(new Color(131,175,155));this.connDB();arr=new Object[1][3];try {rs=stmt.executeQuery("select * from student where 学号="+str);while(rs.next()) {arr[0][0]=rs.getString("学号");arr[0][1]=rs.getString("姓名");arr[0][2]=rs.getString("班级");}}catch(Exception e){e.printStackTrace();}String[] columnNames= {"学号","姓名","班级"};stable1=new JTable(arr,columnNames); //创建表格scroll1=new JScrollPane(stable1);f1.add(btnchange);f1.add(btnrt);f1.add(scroll1);  //此处有个小bug,只有添加进滚轮面板,才能将头部名称显示出来//f1.add(btnrt);btnrt.addMouseListener(new MouseAdapter() {public void mouseClicked(MouseEvent e) {f1.dispose();new StudentFrame(sno);}});btnchange.addMouseListener(new MouseAdapter() {public void mouseClicked(MouseEvent e) {f1.dispose();change();}});}public void change() {  //创建修改密码页面,新窗口用于修改密码this.connDB();JFrame f=new JFrame("修改密码");f.setLayout(new FlowLayout());JPanel p=new JPanel();JPanel p1=new JPanel();p.setLayout(new GridLayout(3,2));JLabel btn1=new JLabel("初始密码:");btn1.setFont(f3);JTextField tf1=new JTextField(10);JLabel btn2=new JLabel("修改密码:");btn2.setFont(f3);JTextField tf2=new JTextField(10);Button ok=new Button("确定");ok.setBackground(new Color(131,175,155));Button cancel=new Button("取消");cancel.setBackground(new Color(131,175,155));p.add(btn1);p.add(tf1);p.add(btn2);p.add(tf2);p1.add(ok);p1.add(cancel);f.add(p);f.add(p1);f.setDefaultCloseOperation(EXIT_ON_CLOSE);f.setLocation(530,300);//f.setLocationRelativeTo(null);f.setSize(300,150);f.setVisible(true);ok.addMouseListener(new MouseAdapter() {public void mouseClicked(MouseEvent e) {try {   //获取初始密码rs=stmt.executeQuery("select * from studentpswd where 学号="+sno);while(rs.next()) {spwd=rs.getString("密码").trim();  //!!!对于数据库中传回来的值一定要用trim();因为后面会有多余的空格!!!/*bug:数据库中数据会有空格,mmp,记于12/10/凌晨1:56,耻!*///spwd="123";}}catch(Exception e1) {e1.printStackTrace();}if(tf1.getText().equals("") || tf2.getText().equals("")) {JOptionPane.showMessageDialog(null, "密码不能为空!请重新修改!");}else {if(!spwd.equals(tf1.getText())) {     //spwd.equals(tf1.getText().trim())JOptionPane.showMessageDialog(null,"初始密码错误,请重新输入密码!");  //"原密码是"+spwd+"输入密码是"+tf1.getText();tf1.setText("");tf2.setText("");}else {try {stmt.executeUpdate("update studentpswd set 密码 ="+"'"+tf2.getText().trim()+"'"+"where 学号="+sno);}catch(Exception e1) {e1.printStackTrace();}JOptionPane.showMessageDialog(null,"密码修改成功!请重新登录!");f.dispose();new DLFrame();}}}});cancel.addMouseListener(new MouseAdapter() {public void mouseClicked(MouseEvent e) {f.dispose();new StudentFrame(sno);}});}public void actionPerformed(ActionEvent e) {//按钮为“个人信息修改”,跳转页面if(e.getSource().equals(btnSelf)) {this.dispose();this.display(sno);}//按钮为“成绩信息查询”,跳转页面if(e.getSource().equals(btnSCsh)) {//new SM().display();this.dispose();this.scsearch(sno);}//按钮为“课程信息查询”,跳转页面if(e.getSource().equals(btnCsh)) {this.dispose();this.csearch(sno);}//按钮为“退出管理系统”,程序退出if(e.getSource().equals(btnEXIT)) {//System.exit(0);this.dispose();new DLFrame();}}public static void main(String[] args) {// TODO Auto-generated method stubnew StudentFrame("201903060401");}}

TeacherFrame.java

package sqlyog;
import java.awt.*;
import java.awt.event.*;
import java.sql.*;
import javax.swing.*;
import java.util.*;public class TeacherFrame extends JFrame implements ActionListener{JLabel l=new JLabel("--教师页面--");//定义面板容器JPanel p1=new JPanel();JPanel p2=new JPanel();//设置字体类型Font f1=new Font("宋体",Font.BOLD,30);Font f2=new Font("幼圆",Font.ITALIC,30);Font f3=new Font("楷体",Font.BOLD,17);Font f4=new Font("隶书",Font.PLAIN,40);//设置6个按钮,以便管理员操作JButton btnTmg=new JButton("个人信息管理");JButton btnSsh=new JButton("学生信息查询");JButton btnCsh=new JButton("课程信息查询");JButton btnSCmg=new JButton("成绩信息管理");JButton btnEXIT=new JButton("退出");String tno;String dbURL="jdbc:mysql://localhost:3306/教学信息管理系统";String useName="root";String usePwd="123456";Connection conn;Statement stmt;ResultSet rs;Object[][] arr;String tpwd;JScrollPane scroll1,scroll2,scroll3,scroll4,scroll5;JTable tb1,tb2,tb3,tb4,tb5; int row;public TeacherFrame(String str) {   //构造方法super("教师页面");setLayout(new FlowLayout());//设置标签的颜色l.setFont(f1);l.setForeground(Color.blue);//设置按钮字体和颜色btnTmg.setFont(f3);btnTmg.setContentAreaFilled(false);//btnTM.setBackground(Color.blue);btnSsh.setFont(f3);btnSsh.setContentAreaFilled(false);btnCsh.setFont(f3);btnCsh.setContentAreaFilled(false);btnSCmg.setFont(f3);btnSCmg.setContentAreaFilled(false);btnEXIT.setFont(f3);btnEXIT.setContentAreaFilled(false);this.tno=str;p1.add(l);   p1.setOpaque(false);p2.setOpaque(false);p2.setLayout(new GridLayout(3,2,10,10));p2.add(btnTmg);p2.add(btnSsh);p2.add(btnCsh);p2.add(btnSCmg);p2.add(btnEXIT);//布局管理器this.add(p1);this.add(p2);//设置顶层容器的大小、位置、可见性及close功能setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);setSize(350,300);setLocationRelativeTo(null);setVisible(true);this.connDB();btnTmg.addActionListener(this);btnSsh.addActionListener(this);btnCsh.addActionListener(this);btnSCmg.addActionListener(this);btnEXIT.addActionListener(this);}public void connDB() {try {conn=DriverManager.getConnection(dbURL,useName,usePwd);stmt=conn.createStatement();}catch(Exception e) {e.printStackTrace();}}public void closeDB() {try {rs.close();stmt.close();conn.close();}catch(Exception e) {e.printStackTrace();}}public void tmg(String str) {   //个人信息管理方法JFrame f=new JFrame("个人信息");//JPanel p=new JPanel();f.setLayout(new FlowLayout());f.setDefaultCloseOperation(EXIT_ON_CLOSE);f.setSize(500,150);f.setVisible(true);f.setLocationRelativeTo(null);Button btnchange=new Button("修改密码");Button btnrt=new Button("返回");btnchange.setFont(f3);btnchange.setBackground(new Color(131,175,155));btnrt.setFont(f3);btnrt.setBackground(new Color(131,175,155));//btnrt.setSize(20,10);this.connDB();arr=new Object[1][7];try {rs=stmt.executeQuery("select * from teacher where 职工号="+str);while(rs.next()) {arr[0][0]=rs.getString("课程名称");arr[0][1]=rs.getString("课程备注");arr[0][2]=rs.getString("班级");arr[0][3]=rs.getString("任课教师");arr[0][4]=rs.getString("职工号");}}catch(Exception e){e.printStackTrace();}String[] list= {"课程名称","课程备注","班级","任课教师","职工号"};tb1=new JTable(arr,list); //创建表格scroll1=new JScrollPane(tb1);//f.add(btnrt,BorderLayout.NORTH);f.add(btnchange);f.add(btnrt);f.add(scroll1);  //此处有个小bug,只有添加进滚轮面板,才能将头部名称显示出来//f1.add(btnrt); btnrt.addMouseListener(new MouseAdapter() {public void mouseClicked(MouseEvent e) {f.dispose();new TeacherFrame(tno);}});btnchange.addMouseListener(new MouseAdapter() {public void mouseClicked(MouseEvent e) {f.dispose();change();}});}public void change() {  //创建修改密码页面,新窗口用于修改密码this.connDB();JFrame f=new JFrame("修改密码");f.setLayout(new FlowLayout());JPanel p=new JPanel();JPanel p1=new JPanel();p.setLayout(new GridLayout(3,2));JLabel btn1=new JLabel("初始密码:");btn1.setFont(f3);JTextField tf1=new JTextField(10);JLabel btn2=new JLabel("修改密码:");btn2.setFont(f3);JTextField tf2=new JTextField(10);Button ok=new Button("确定");ok.setBackground(new Color(131,175,155));Button cancel=new Button("取消");cancel.setBackground(new Color(131,175,155));p.add(btn1);p.add(tf1);p.add(btn2);p.add(tf2);p1.add(ok);p1.add(cancel);f.add(p);f.add(p1);f.setDefaultCloseOperation(EXIT_ON_CLOSE);f.setLocation(530,300);//f.setLocationRelativeTo(null);f.setSize(300,150);f.setVisible(true);ok.addMouseListener(new MouseAdapter() {public void mouseClicked(MouseEvent e) {try {   //获取初始密码rs=stmt.executeQuery("select * from teacherpswd where 职工号="+tno);while(rs.next()) {tpwd=rs.getString("密码").trim();}}catch(Exception e1) {e1.printStackTrace();}if(tf2.getText().equals("") || tf1.getText().equals("")) {JOptionPane.showMessageDialog(null, "密码不能为空!请重新修改!");}else{if(!tpwd.equals(tf1.getText().trim())) {     //spwd.equals(tf1.getText().trim())JOptionPane.showMessageDialog(null,"初始密码错误,请重新输入密码!");  //"原密码是"+spwd+"输入密码是"+tf1.getText();tf1.setText("");tf2.setText("");}else {try {stmt.executeUpdate("update teacherpswd set 密码 ="+tf2.getText().trim()+"where 职工号="+tno);}catch(Exception e1) {e1.printStackTrace();}JOptionPane.showMessageDialog(null,"密码修改成功!请重新登录!");f.dispose();new DLFrame();}}}});cancel.addMouseListener(new MouseAdapter() {public void mouseClicked(MouseEvent e) {f.dispose();new TeacherFrame(tno);}});}public void ssh(String str) {  //学生信息查询JFrame f=new JFrame("学生信息");//JPanel p=new JPanel();f.setLayout(new FlowLayout());f.setDefaultCloseOperation(EXIT_ON_CLOSE);f.setSize(500,150);f.setVisible(true);f.setLocationRelativeTo(null);Button btnrt=new Button("返回");btnrt.setFont(f3);btnrt.setBackground(new Color(131,175,155));//btnrt.setSize(20,10);this.connDB();int i=0,j=0;ArrayList al=new ArrayList();try {rs=stmt.executeQuery("select * from student");while(rs.next()) {al.add(rs.getString("学号"));al.add(rs.getString("姓名"));al.add(rs.getString("班级"));i++;}}catch(Exception e){e.printStackTrace();}arr=new Object[i][3];try {rs=stmt.executeQuery("select * from student");while(rs.next()) {arr[j][0]=rs.getString("学号");arr[j][1]=rs.getString("姓名");arr[j][2]=rs.getString("班级");j++;}}catch(Exception e){e.printStackTrace();}String[] list= {"学号","姓名","班级"};tb2=new JTable(arr,list); //创建表格scroll2=new JScrollPane(tb2);//f.add(btnrt,BorderLayout.NORTH);f.add(btnrt);f.add(scroll2);  //此处有个小bug,只有添加进滚轮面板,才能将头部名称显示出来//f1.add(btnrt); btnrt.addMouseListener(new MouseAdapter() {public void mouseClicked(MouseEvent e) {f.dispose();new TeacherFrame(tno);}});}public void csh(String str) {  //课程信息查询JFrame f=new JFrame("课程信息");//JPanel p=new JPanel();f.setLayout(new FlowLayout());f.setDefaultCloseOperation(EXIT_ON_CLOSE);f.setSize(500,150);f.setVisible(true);f.setLocationRelativeTo(null);Button btnrt=new Button("返回");btnrt.setFont(f3);btnrt.setBackground(new Color(131,175,155));//btnrt.setSize(20,10);this.connDB();int i=0,j=0;ArrayList al=new ArrayList();try {rs=stmt.executeQuery("select  distinct 课程名称,考核模式,学分,学时  from course");while(rs.next()) {al.add(rs.getString("课程名称"));al.add(rs.getString("考核模式"));al.add(rs.getInt("学分"));al.add(rs.getInt("学时"));i++;}}catch(Exception e){e.printStackTrace();}arr=new Object[i][4];try {rs=stmt.executeQuery("select  distinct 课程名称,考核模式,学分,学时  from course");while(rs.next()) {arr[j][0]=rs.getString("课程名称");arr[j][1]=rs.getString("考核模式");arr[j][2]=rs.getInt("学分");arr[j][3]=rs.getInt("学时");j++;}}catch(Exception e){e.printStackTrace();}String[] list= {"课程名称","考核模式","学分","学时"};tb3=new JTable(arr,list); //创建表格scroll3=new JScrollPane(tb3);//f.add(btnrt,BorderLayout.NORTH);f.add(btnrt);f.add(scroll3);  //此处有个小bug,只有添加进滚轮面板,才能将头部名称显示出来//f1.add(btnrt); btnrt.addMouseListener(new MouseAdapter() {public void mouseClicked(MouseEvent e) {f.dispose();new TeacherFrame(tno);}});}public void scmg(String str) {  //成绩信息管理JFrame f=new JFrame("学生成绩信息");//JPanel p=new JPanel();f.setLayout(new FlowLayout());f.setDefaultCloseOperation(EXIT_ON_CLOSE);f.setSize(500,150);f.setVisible(true);f.setLocationRelativeTo(null);Button btnrt=new Button("返回");Button btnchange=new Button("修改成绩");btnchange.setFont(f3);btnchange.setBackground(new Color(131,175,155));btnrt.setFont(f3);btnrt.setBackground(new Color(131,175,155));//btnrt.setSize(20,10);this.connDB();int i=0,j=0;ArrayList al=new ArrayList();try {rs=stmt.executeQuery("select * from grade");while(rs.next()) {al.add(rs.getString("学号"));al.add(rs.getString("姓名"));al.add(rs.getString("课程名称"));al.add(rs.getString("成绩"));i++;}}catch(Exception e){e.printStackTrace();}arr=new Object[i][4];try {rs=stmt.executeQuery("select * from grade");while(rs.next()) {arr[j][0]=rs.getString("学号");arr[j][1]=rs.getString("姓名");arr[j][2]=rs.getString("课程名称");arr[j][3]=rs.getString("成绩");j++;}}catch(Exception e){e.printStackTrace();}String[] list= {"学号","姓名","课程名称","成绩"};tb4=new JTable(arr,list); //创建表格scroll4=new JScrollPane(tb4);//f.add(btnrt,BorderLayout.NORTH);f.add(btnchange);f.add(btnrt);f.add(scroll4);  //此处有个小bug,只有添加进滚轮面板,才能将头部名称显示出来//f1.add(btnrt); btnrt.addMouseListener(new MouseAdapter() {public void mouseClicked(MouseEvent e) {f.dispose();new TeacherFrame(tno);}});btnchange.addMouseListener(new MouseAdapter() {public void mouseClicked(MouseEvent e) {f.dispose();scchange();}});}public void scchange() {  //修改成绩页面JFrame f=new JFrame("修改成绩");f.setLayout(new FlowLayout());JLabel l=new JLabel("输入学号");JTextField tf=new JTextField(12);JButton jb1=new JButton("确定");jb1.setFont(f3);jb1.setBackground(new Color(131,175,155));JButton jb2=new JButton("返回");jb2.setFont(f3);jb2.setBackground(new Color(131,175,155));f.add(l);f.add(tf);f.add(jb1);f.add(jb2);jb1.addMouseListener(new MouseAdapter() {public void mouseClicked(MouseEvent e) {f.dispose();new SCmg(tno,tf.getText());  //重新刷新成绩页面,为班级成绩}});jb2.addMouseListener(new MouseAdapter() {public void mouseClicked(MouseEvent e) {f.dispose();scmg(tno);  //取消按钮,重新显示页面}});f.setSize(200,150);f.setLocationRelativeTo(null);f.setDefaultCloseOperation(EXIT_ON_CLOSE);f.setVisible(true);}//bug方法  只能新建类来解决/*public void scmg(String s1,String s2) { //精确到班级成绩的页面String csname=null;this.connDB();try {rs=stmt.executeQuery("select distinct 班级号 ,班级名称 from 学生信息表 where 班级号="+"'"+s2+"'");while(rs.next()) {csname=rs.getString("班级名称").trim();}}catch(Exception e) {e.printStackTrace();}JFrame f=new JFrame(csname+" --学生成绩信息");//JPanel p=new JPanel();f.setLayout(new FlowLayout());f.setDefaultCloseOperation(EXIT_ON_CLOSE);f.setSize(500,150);f.setVisible(true);f.setLocationRelativeTo(null);Button btnrt=new Button("返回");Button btnchange=new Button("修改");btnchange.setFont(f3);btnchange.setBackground(new Color(131,175,155));btnrt.setFont(f3);btnrt.setBackground(new Color(131,175,155));//btnrt.setSize(20,10);this.connDB();int i=0,j=0;ArrayList al=new ArrayList();try {rs=stmt.executeQuery("select * from 学生信息表  where 教师号="+s1+"and 班级号='"+s2+"'");while(rs.next()) {al.add(rs.getString("学号"));al.add(rs.getString("姓名"));al.add(rs.getString("性别"));al.add(rs.getInt("年龄"));al.add(rs.getString("班级名称"));al.add(rs.getString("课程号"));al.add(rs.getString("课程名"));al.add(rs.getString("成绩"));i++;}}catch(Exception e){e.printStackTrace();}arr=new Object[i][8];try {rs=stmt.executeQuery("select * from 学生信息表  where 教师号="+s1+"and 班级号= '"+s2+"'");while(rs.next()) {arr[j][0]=rs.getString("学号");arr[j][1]=rs.getString("姓名");arr[j][2]=rs.getString("性别");arr[j][3]=rs.getString("年龄");arr[j][4]=rs.getString("班级名称");arr[j][5]=rs.getString("课程号");arr[j][6]=rs.getString("课程名");arr[j][7]=rs.getString("成绩");j++;}}catch(Exception e){e.printStackTrace();}String[] list= {"学号","姓名","性别","年龄","班级名称","课程号","课程名","成绩"};tb5=new JTable(arr,list); //创建表格scroll5=new JScrollPane(tb5);//f.add(btnrt,BorderLayout.NORTH);f.add(btnchange);f.add(btnrt);f.add(scroll5);  //此处有个小bug,只有添加进滚轮面板,才能将头部名称显示出来//String sno1=null; //局部变量一定要初始化,获取学生学号String cno=null; //获取课程号///*Bug:千万不要在同一个方法里定义获取行数!!!!!!!!!!!无法执行                    ////row=tb5.getSelectedRow();  //代表鼠标选定的行数                                              ///btnrt.addMouseListener(new MouseAdapter() {public void mouseClicked(MouseEvent e) {f.dispose();scmg(tno);}});btnchange.addMouseListener(new MouseAdapter() {public void mouseClicked(MouseEvent e) {if(row==-1) {  //表示未被选中JOptionPane.showMessageDialog(null,"请选择要修改的信息!");}else {//int x=0;try {//String sno1=(String)tb1.getModel().getValueAt(row,column);f.dispose();//scchange1(sno1,cno);}catch(Exception e1) {e1.printStackTrace();}}//f.dispose();//scchange();}});}public void test() {  //用来获取行数}*/public void actionPerformed(ActionEvent e) {//按钮为“教师信息管理”,跳转页面if(e.getSource().equals(btnTmg)) {this.dispose();this.tmg(tno);}//按钮为“学生信息管理”,跳转页面if(e.getSource().equals(btnSsh)) {//new SM().display();this.dispose();this.ssh(tno);}//按钮为“课程信息管理”,跳转页面if(e.getSource().equals(btnCsh)) {this.dispose();this.csh(tno);}//按钮为“成绩信息管理”,跳转页面if(e.getSource().equals(btnSCmg)) {this.dispose();this.scmg(tno);}//按钮为“退出管理系统”,程序退出if(e.getSource().equals(btnEXIT)) {//System.exit(0);this.dispose();new DLFrame();}}public static void main(String[] args) {// TODO Auto-generated method stubnew TeacherFrame("20050002");}}

TM.java

package sqlyog;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.sql.*;
import java.util.*;public class TM extends JFrame implements ActionListener,ItemListener{//定义按钮,添加进JMenuBarButton btnadd=new Button("添加");Button btndelete=new Button("删除");Button btnupdate=new Button("修改");Button btnsearch=new Button("查询");Button btndisplay=new Button("刷新显示");Button btnreturn=new Button("返回");//定义菜单栏JMenuBar mb=new JMenuBar();//定义字体Font f1=new Font("行楷",Font.BOLD,15);Font f2=new Font("楷体",Font.ITALIC,24);//定义表格JTable tb;JTable tb1;//定义滚轮面板JScrollPane jsp;JScrollPane scroll1;//定义数据库对象,定义连接变量Connection conn;Statement stmt;ResultSet rs;//定义连接字符串String dbURL="jdbc:mysql://localhost:3306/教学信息管理系统";String username="root";String userpwd="123456";Object[][] arr;//连接数据库public void connDB(){  //连接数据库方法try {conn=DriverManager.getConnection(dbURL,username,userpwd);stmt=conn.createStatement();}catch(Exception e) {e.printStackTrace();}}public void closeDB() {  //关闭数据库方法try {rs.close();stmt.close();conn.close();}catch(Exception e) {e.printStackTrace();}}public TM() {   //构造方法super("教师信息管理系统");//设置按钮颜色和字体btnadd.setFont(f1);btnadd.setBackground(new Color(131,175,155));btndelete.setFont(f1);btndelete.setBackground(new Color(131,175,155));btnupdate.setFont(f1);btnupdate.setBackground(new Color(131,175,155));btnsearch.setFont(f1);btnsearch.setBackground(new Color(131,175,155));btndisplay.setFont(f1);btndisplay.setBackground(new Color(131,175,155));btnreturn.setFont(f1);btnreturn.setBackground(new Color(131,175,155));//将按钮添加进菜单栏中mb.add(btnadd);mb.add(btndelete);mb.add(btnupdate);mb.add(btnsearch);mb.add(btndisplay);mb.add(btnreturn);//连接数据库this.connDB();//给按钮注册监听器btnadd.addActionListener(this);btndelete.addActionListener(this);btnupdate.addActionListener(this);btnsearch.addActionListener(this);btndisplay.addActionListener(this);btnreturn.addActionListener(this);setSize(500,300);setLocationRelativeTo(null);setDefaultCloseOperation(EXIT_ON_CLOSE);setVisible(true);this.setJMenuBar(mb);}public void display() {//this.connDB();String sql="select * from teacher";ArrayList ar=new ArrayList();  //建立数列进行存储数据库成员信息int i=0,j=0;try {rs=stmt.executeQuery(sql);while(rs.next()) {ar.add(rs.getString("课程名称"));ar.add(rs.getString("课程备注"));ar.add(rs.getString("班级"));ar.add(rs.getString("任课教师"));ar.add(rs.getString("职工号"));i++;}}catch(Exception e) {e.printStackTrace();}//创建二维数组进行存储arr=new Object[i][7];String[] list= {"课程名称","课程备注","班级","任课教师","职工号"};try {rs=stmt.executeQuery("select * from teacher order by 课程名称");while(rs.next()) {arr[j][0]=rs.getString("课程名称");arr[j][1]=rs.getString("课程备注");arr[j][2]=rs.getString("班级");arr[j][3]=rs.getString("任课教师");arr[j][4]=rs.getString("职工号");j++;}}catch(Exception e) {e.printStackTrace();}tb=new JTable(arr,list);jsp=new JScrollPane(tb);this.add(jsp);}public void delete() {   //删除信息方法String tno=null;  //定义字符变量,来接收教师号   int row=tb.getSelectedRow();  //代表鼠标选定的行数if(row==-1) {  //表示未被选中JOptionPane.showMessageDialog(null,"请选择要删除的记录!");}else {int x=0;try {rs=stmt.executeQuery("select * from teacher");while(rs.next() && x<=row) {tno=rs.getString("职工号");x++;}stmt.executeUpdate("delete from teacher where 职工号="+tno);   //局部变量一定要初始化JOptionPane.showMessageDialog(null,"删除成功!");this.dispose();new TM().display();}catch(Exception e) {e.printStackTrace();}}}public void update() {String sno1=null; //局部变量一定要初始化int row=tb.getSelectedRow();  //代表鼠标选定的行数if(row==-1) {  //表示未被选中JOptionPane.showMessageDialog(null,"请选择要修改的信息!");}else {int x=0;try {rs=stmt.executeQuery("select * from teacher");while(rs.next() && x<=row) {sno1=rs.getString("职工号");x++;}this.dispose();new TMupdate(sno1);}catch(Exception e) {e.printStackTrace();}}}public void show(String str) {   //查询结果方法JFrame f=new JFrame("查询结果");//JPanel p=new JPanel();f.setLayout(new FlowLayout());f.setDefaultCloseOperation(EXIT_ON_CLOSE);f.setSize(500,150);f.setVisible(true);f.setLocationRelativeTo(null);Button btnrt=new Button("返回");btnrt.setFont(f1);btnrt.setBackground(new Color(131,175,155));//btnrt.setSize(20,10);this.connDB();arr=new Object[1][5];try {rs=stmt.executeQuery("select * from teacher where 职工号="+str);while(rs.next()) {arr[0][0]=rs.getString("课程名称");arr[0][1]=rs.getString("课程备注");arr[0][2]=rs.getString("班级");arr[0][3]=rs.getString("任课教师");arr[0][4]=rs.getString("职工号");}}catch(Exception e){e.printStackTrace();}String[] list= {"课程名称","课程备注","班级","任课教师","职工号"};tb1=new JTable(arr,list); //创建表格scroll1=new JScrollPane(tb1);//f.add(btnrt,BorderLayout.NORTH);f.add(btnrt);f.add(scroll1);  //此处有个小bug,只有添加进滚轮面板,才能将头部名称显示出来//f1.add(btnrt); btnrt.addMouseListener(new MouseAdapter() {public void mouseClicked(MouseEvent e) {f.dispose();}});}//该方法用来确认是否在数据库中找到教师号public boolean searchtest(String str) {boolean x=false;this.connDB();try {rs=stmt.executeQuery("select * from teacher");while(rs.next()) {if(rs.getString("职工号").trim().equals(str)) {  //在java中,判断字符串是否相同,一定要使用equals函数!!!!!!!!x=true;}}//return x;}catch(Exception e) {e.printStackTrace();}return x;}//查找方法public void search() {   //等效于将一个窗口写在方法里面JFrame f=new JFrame("查询");f.setLayout(new FlowLayout());f.setSize(240,180);f.setVisible(true);f.setLocationRelativeTo(null);JPanel p1=new JPanel();JPanel p2=new JPanel();JLabel stuno=new JLabel("输入教师职工号:");JTextField stuno1=new JTextField(10);Button ok=new Button("确定");Button cancel=new Button("取消");p1.add(stuno);p1.add(stuno1);p2.add(ok);p2.add(cancel);f.add(p1);f.add(p2);//为组件注册监听器ok.addMouseListener(new MouseAdapter() {public void mouseClicked(MouseEvent e) {if(stuno1.getText().equals("")) {JOptionPane.showMessageDialog(null,"请输入教师职工号");}else {if(!searchtest(stuno1.getText().trim())) {f.dispose();JOptionPane.showMessageDialog(null,"对不起,该教师不存在!");}else {f.dispose();//new SM(stuno1.getText());show(stuno1.getText());}}}});cancel.addMouseListener(new MouseAdapter() {public void mouseClicked(MouseEvent e) {f.dispose();//}});f.addWindowListener(new WindowAdapter() {public void windowClosing(WindowEvent e) {Window w=(Window)e.getComponent();  w.dispose();}});}public void itemStateChanged(ItemEvent e) {}public void actionPerformed(ActionEvent e) {if(e.getSource()==btnadd) {this.dispose();new TMadd();}if(e.getSource()==btndelete) {JOptionPane.showMessageDialog(null,"删除操作,谨慎操作!");this.delete();}if(e.getSource()==btnupdate) {//this.dispose();this.update(); }if(e.getSource()==btnsearch) {this.search();}if(e.getSource()==btndisplay) {this.dispose();new TM().display();}if(e.getSource()==btnreturn) {this.dispose();new GLFrame();}}public static void main(String[] args) {new TM();}}

TMadd.java

package sqlyog;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.sql.*;public class TMadd extends JFrame implements ActionListener,ItemListener{//定义面板Panel p=new Panel();Panel p1=new Panel();Panel p2=new Panel();/*Panel psno=new Panel();Panel psname=new Panel();Panel pssex=new Panel();Panel psage=new Panel();Panel psclass=new Panel();Panel pspwd=new Panel();*///定义属性标签及文本框JLabel name=new JLabel("课程名称:");JTextField tname=new JTextField(20);JLabel bz=new JLabel("课程备注:");JTextField tbz=new JTextField(20);//JTextField ssex=new JTextField(4);JLabel xl=new JLabel("班级:");JTextField txl=new JTextField(20);JLabel zc=new JLabel("任课教师:");JTextField tzc=new JTextField(10);JLabel no=new JLabel("职工号:");JTextField tno=new JTextField(12);//定义字体Font f1=new Font("宋体",Font.BOLD,20);Font f2=new Font("幼圆",Font.ITALIC,30);Font f3=new Font("楷体",Font.BOLD,18);Font f4=new Font("隶书",Font.PLAIN,40);//定义按钮Button btnsure=new Button("确定");Button btnagain=new Button("重置");Button btncancel=new Button("取消");//定义连接字符String dbURL = "jdbc:mysql://localhost:3306/教学信息管理系统";String userName = "root";String userPwd = "123456";Connection conn;Statement stmt;ResultSet rs;public void connDB() {   //连接数据库方法try {//连接数据库conn=DriverManager.getConnection(dbURL,userName,userPwd);stmt=conn.createStatement();}catch(Exception e) {e.printStackTrace();//System.out.println("连接失败!");}}public void closeDB() {  //关闭数据库方法try {rs.close();stmt.close();conn.close();}catch(Exception e) {e.printStackTrace();//System.out.println("关闭失败!");}}public TMadd() {  //构造方法super("教师信息添加页面");setLayout(new FlowLayout());//设置字体name.setFont(f3);bz.setFont(f3);xl.setFont(f3);zc.setFont(f3);no.setFont(f3);//按钮字体、颜色btnsure.setFont(f1);btnsure.setBackground(new Color(131,175,155)); //淡草色btncancel.setFont(f1);btncancel.setBackground(new Color(131,175,155)); //淡草色btnagain.setFont(f1);btnagain.setBackground(new Color(131,175,155)); //淡草色//设置面板的网格布局管理p1.setLayout(new GridLayout(8,2));//p2.setLayout(new GridLayout(4,2));//将标签和文本框添加进面板p1.add(name);p1.add(tname);p1.add(bz);p1.add(tbz);p1.add(xl);p1.add(txl);p1.add(zc);p1.add(tzc);p1.add(no);p1.add(tno);//将按钮添加进面板p.add(btnsure);p.add(btnagain);p.add(btncancel);//添加面板this.add(p1);//this.add(p2);this.add(p);//设置顶层容器的大小、位置、可见性及close功能setDefaultCloseOperation(EXIT_ON_CLOSE);setSize(500,350);setLocationRelativeTo(null);setVisible(true);//注册监听器btnsure.addActionListener(this);btnagain.addActionListener(this);btncancel.addActionListener(this);}public void itemStateChanged(ItemEvent e) {if(e.getStateChange()==ItemEvent.SELECTED) {JComboBox j=(JComboBox)e.getSource();}}//该方法用来确认是否在数据库中找到教师职工号public boolean searchtest(String str) {boolean x=false;this.connDB();try {rs=stmt.executeQuery("select * from teacher");while(rs.next()) {if(rs.getString("职工号").trim().equals(str)) {  //在java中,判断字符串是否相同,一定要使用equals函数!!!!!!!!x=true;}}//return x;}catch(Exception e) {e.printStackTrace();}return x;}public void insert() {  //插入方法try {this.connDB();//插入数据stmt.executeUpdate("insert into teacher  values('"+tname.getText().trim()+"'"+","+"'"+tbz.getText().trim()+"'"+","+"'"+txl.getText().trim()+"'"+","+"'"+tzc.getText().trim()+"'"+","+"'"+tno.getText().trim()+"'"+")");//System.out.println("insert success!");JOptionPane.showMessageDialog(null,"信息添加成功!");this.dispose();new TM().display();;}catch(Exception e) {e.printStackTrace();}}public void actionPerformed(ActionEvent e) {if(e.getSource()==btnsure) {if(tname.getText().equals("") || tbz.getText().equals("") || txl.getText().equals("") || tzc.getText().equals("") || tno.getText().equals("") ) {JOptionPane.showMessageDialog(null,"信息不能为空!");}else if(searchtest(tno.getText())) {JOptionPane.showMessageDialog(null,"该教师已存在!请重新添加!");tname.setText("");tbz.setText("");txl.setText("");tzc.setText("");tno.setText("");}else {this.insert();}}if(e.getSource()==btnagain) {tname.setText("");tbz.setText("");txl.setText("");tzc.setText("");tno.setText("");}if(e.getSource()==btncancel) {this.dispose();new TM().display();;//System.exit(0);}}public static void main(String[] args) {// TODO Auto-generated method stubnew TMadd();}}

TMupdate.java

//更新类
package sqlyog;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.sql.*;public class TMupdate extends JFrame implements ActionListener,ItemListener{//定义面板Panel p=new Panel();Panel p1=new Panel();Panel p2=new Panel();//定义属性标签及文本框JLabel name=new JLabel("课程名称:");JTextField tname=new JTextField(20);JLabel bz=new JLabel("课程备注:");JTextField tbz=new JTextField(20);//JTextField ssex=new JTextField(4);JLabel xl=new JLabel("班级:");JTextField txl=new JTextField(20);JLabel zc=new JLabel("任课教师:");JTextField tzc=new JTextField(10);JLabel no=new JLabel("职工号:");JTextField tno=new JTextField(12);//定义字体Font f1=new Font("宋体",Font.BOLD,20);Font f2=new Font("幼圆",Font.ITALIC,30);Font f3=new Font("楷体",Font.BOLD,18);Font f4=new Font("隶书",Font.PLAIN,40);//定义按钮Button btnsure=new Button("确定");Button btnagain=new Button("重置");Button btncancel=new Button("取消");//定义连接字符String dbURL = "jdbc:mysql://localhost:3306/教学信息管理系统";String userName = "root";String userPwd = "123456";Connection conn;Statement stmt;ResultSet rs;public void connDB() {   //连接数据库方法try {//连接数据库conn=DriverManager.getConnection(dbURL,userName,userPwd);stmt=conn.createStatement();}catch(Exception e) {e.printStackTrace();//System.out.println("连接失败!");}}public void closeDB() {  //关闭数据库方法try {rs.close();stmt.close();conn.close();}catch(Exception e) {e.printStackTrace();//System.out.println("关闭失败!");}}public TMupdate(String str) {  //含参构造方法super("教师信息修改页面");setLayout(new FlowLayout());tno.setText(str);tno.setEditable(false);//设置字体name.setFont(f3);bz.setFont(f3);xl.setFont(f3);zc.setFont(f3);no.setFont(f3);//按钮字体、颜色btnsure.setFont(f1);btnsure.setBackground(new Color(131,175,155)); //淡草色btncancel.setFont(f1);btncancel.setBackground(new Color(131,175,155)); //淡草色btnagain.setFont(f1);btnagain.setBackground(new Color(131,175,155)); //淡草色//设置面板的网格布局管理p1.setLayout(new GridLayout(8,2));//p2.setLayout(new GridLayout(4,2));//将标签和文本框添加进面板p1.add(name);p1.add(tname);p1.add(bz);p1.add(tbz);p1.add(xl);p1.add(txl);p1.add(zc);p1.add(tzc);p1.add(no);p1.add(tno);//将按钮添加进面板p.add(btnsure);p.add(btnagain);p.add(btncancel);//添加面板this.add(p1);//this.add(p2);this.add(p);//设置顶层容器的大小、位置、可见性及close功能setDefaultCloseOperation(EXIT_ON_CLOSE);setSize(500,350);setLocationRelativeTo(null);setVisible(true);//注册监听器btnsure.addActionListener(this);btnagain.addActionListener(this);btncancel.addActionListener(this);}public void itemStateChanged(ItemEvent e) {if(e.getStateChange()==ItemEvent.SELECTED) {JComboBox j=(JComboBox)e.getSource();}}public void delete(String s) {   //删除信息方法try {stmt.executeUpdate("delete from teacher where 职工号="+s);   //局部变量一定要初始化JOptionPane.showMessageDialog(null,"删除成功!");}catch(Exception e) {e.printStackTrace();}
}public void insert() {  //插入方法try {this.connDB();//插入数据stmt.executeUpdate("insert into teacher values("+tname.getText().trim()+","+"'"+tbz.getText().trim()+"'"+","+"'"+txl.getText().trim()+"'"+","+"'"+tzc.getText().trim()+"'"+","+"'"+tno.getText().trim()+"'"+")");//System.out.println("insert success!");JOptionPane.showMessageDialog(null,"信息修改成功!");this.dispose();new TM().display();}catch(Exception e) {e.printStackTrace();}}public void actionPerformed(ActionEvent e) {if(e.getSource()==btnsure) {if(tname.getText().equals("") || tbz.getText().equals("") || txl.getText().equals("") || tzc.getText().equals("") || tno.getText().equals("") ) {JOptionPane.showMessageDialog(null,"信息不能为空!");}else {//确认时,将删除信息再重新添加this.delete(tno.getText());this.insert();}}if(e.getSource()==btnagain) {//tno.setText("");tname.setText("");tbz.setText("");txl.setText("");tzc.setText("");tno.setText("");}if(e.getSource()==btncancel) {this.dispose();new TM().display();;//System.exit(0);}}public static void main(String[] args) {// TODO Auto-generated method stubnew TMupdate(" ");}
}

还有些许代码问题,但是无伤大雅。

教学信息管理系统+SQL相关推荐

  1. 创业教学信息管理系统

    摘  要 计算机网络如果结合使用信息管理系统,能够提高管理员管理的效率,改善服务质量.优秀的创业教学信息管理系统能够更有效管理用户学习创业业务规范,帮助管理者更加有效管理用户学习创业,可以帮助提高克服 ...

  2. C++实现教学信息管理系统

    教学信息管理系统 实现以下功能 完整源代码实现如下 实现以下功能 1.录入教师信息 2.查找教师信息 3.删除教师信息 4.修改教师信息 5.插入教师信息 按教学效果评分排序 7.显示所有教师信息 8 ...

  3. 教学信息管理系统c语言编程,C语言课程设计【学院教学信息管理系统】.doc

    每个小组选择一个题目.总分100分,PPT+大作业报告+演讲,程序运行成功40分,ppt10分,演讲演示20分,大作业报告30分.第十九周进行报告演示.两组报告雷同,或者源代码雷同将同时扣分. C语言 ...

  4. C语言学院教学信息管理系统

    C语言学院教学信息管理系统 30.某学院教学信息管理系统 功能: 1. 每一条记录包括一位教师的职工号.姓名.职称.性别.3门主讲课程(课程名称.开课学期.课程性质(学位与非学位课)和教学效果),教学 ...

  5. android实现教学界面,基于Android的教学信息管理系统的设计与实现

    摘要: 伴随着智能终端和移动通信技术的迅猛发展,智能设备逐渐在人们生活中普及,成为人们获取工作.学习.生活和娱乐等各方面信息的重要途径.由于Android智能操作系统的开源性,自发布以来,Androi ...

  6. Java课程设计源码——学生信息管理系统 SQL

    2022.6.9 更新 这个课程作业的代码有小伙伴反馈有问题(数据库无法连接等),但博主最近学业不用Java了,暂时没时间看,如果有同学有解决方案,可以在下面评论帮助一下其他同学.实在抱歉啦. pac ...

  7. 学生信息管理系统总体设计说明书

    第一章引言 1.1编写目的 为开发人员.维护人员.客户之间提供共同的协议而创立基础,对学生信息管理软件功能的实现作使命描述. 本说明书的预期读者为客户.业务或需求分析人员.测试人员.用户文档编写者.项 ...

  8. 病人信息管理html,基于JSP住院病人信息管理系统本科.doc

    基于JSP住院病人信息管理系统本科 本科毕业论文(设计) (2014届) 院 系 计算机科学与技术专 业 计算机 姓 名 指导教师 职 称 摘 要发展,医学领域内,,提高整个医院的工作效率和工作质量. ...

  9. 学生信息管理系统测试计划大纲

    第一章引言 1.1编写目的 本测试计划是为了使系统功能模块是否与系统需求规格说明书中所描述的功能一致,并且检验该功能是否运行稳定 1.2背景 A:本测试计划是学生信息管理系统开发文档的组成部分. B: ...

最新文章

  1. 11月11日截止报名!快来参加顶尖极客汇聚的“AI Challenger 全球AI挑战赛”!
  2. 源码分析 @angular/cdk 之 Portal
  3. 当年轻人开始谈论AI伦理
  4. 百度蜘蛛动态网页ajax,百度SEO优化百度蜘蛛可读内容
  5. python for-Python for windows官方版
  6. Gym导入Atari游戏环境报错
  7. oracle10g的silent,Linux 静默(Silent) 安装Oracle 10g
  8. Flink专题-BaseTransform
  9. uboot启动流程概述_Alibaba Cloud Linux 2 LTS OS 启动优化实践
  10. PCB走线角度为90度到底行不行?
  11. Java 8:再见手册SQL,您好!
  12. java 定时执行任务
  13. 基于redis和R语言构建并行计算平台(yiyou)
  14. 核函数(Kernel Function)与SVM
  15. EMC相关标准 GB IEC EN对照(持续添加中……)
  16. docker 安装oracle11g 并配置数据持久化
  17. 短链接生成接口、长链接转换短链接,可根据ip归属地个性化跳转、随机跳转
  18. WordPress插件 UberGrid 高级相册图片分享中文插件[更新至v2.0.13]
  19. STM32开发项目:ADS1115的驱动与使用
  20. 港股通接口获取香港交易所上市股票的基本信息

热门文章

  1. Edge检查更新时出错:无法连接到Internet。如果使用防火墙,请将 MicrosoftEdgeUpdate.exe 加入允许列表中。
  2. 黑鲨手机计算机科学技术器,黑鲨4Pro将PC的SSD存储科技带到手机端,真技术革命还假营销噱头?...
  3. 小白笔记——异常处理基础
  4. Windows系统盘瘦身
  5. Gradle使用Junit5进行test
  6. 旅游网站首页——html
  7. Java实现 LeetCode 93 复原IP地址
  8. AndroidStudio:The application‘s minSdkVersion is newer than the device API level.
  9. 函数的 smoothness 和 convexity
  10. NetSuite 设置库存盘点