利用java和mysql实现教学管理系统。
教学管理系统主要是在eclipse利用java代码完成编写,并与数据库进行连接,使得两边都能控制数据库的数据变化。
仅供初学者参考。
每学期学校都要开设—定的课程,提供给学生根据自己的情况来选择。最后的选课结果 不但需要给出学生的课表,也要把选课名单提供给任课教师进行期末成绩的评定。
1 系统需求分析
1.1系统功能分析
(1)课程设置,包括:增加、删除、查询和修改课程信息。
(2)课程时间和教室的安排,以及课程清单打印输出。
(3)学生选课和学生课表的打印输出。
(4)课程学生名单的打印输出和期末成绩输入。
(5)成绩查询、报表。
1.2系统功能模块设计(划分)
系统各功能模块的关系如图 1 所示。
1.3 与其它系统的关系
教学管理信息系统同样是校园信息管理系统的一个有机组成部分。需要从学生信息管理 系统获取学生的个人信息,从教师信息管理系统获取任课教师的相关信息。同时,提供的成绩单为其他系统提供必要的参考。
2:课程设计目的
2.1:建立一个教学管理系统,更多的是实现教学系统的信息共享,使用者不仅仅是教务管理人员,还能是老师和学生,老师可以查看自己的教授课程和课程安排;学生可以通过此系统完成个人信息,选课信息的输入,查看期末考试成绩排名等。系统提供了一个平台使得不同角色能查找到自己的信息,却又没有侵犯到他人信息;
2.2:锻炼学生java知识和数据库相结合的综合运用能力,将书本上的知识与现实联系起来,吸引学生的兴趣,将学习融会贯通。
3、总体设计(含背景知识或基本原理与算法、或模块介绍、设
计步骤等)
3.1:总体模块:教学管理系统是将老师,学生,课程信息紧密结合起来,我设置了7个数据表,分别是 代表用户登录界面的userpass表,学生信息student表,老师信息teacher表,课程信息course表,课程状态coursestate表,学生选课信息selectcourse表,学生成绩score表;
3.2:子类模块:每个数据表均实现了一定的功能,最开始是一个用户账号密码登录页面,只有登进账户才能对数据进行操作,学生老师课程表等数据表包括数据的添加,修改,删除和查找等操作。在学生成绩的表里面加上了一个总分排名的程序,将同学的分数进行相加并自动进行总分排名;
3.3:组件模块:利用java知识点创造组件,添加组件,加载驱动,进行连接,添加监视动作,如:添加学生信息按钮等,每个数据表都添加了很多按钮组件,不同的功能还会跳出不同的子界面,如点击添加学生信息按钮就会出现学生信息子界面,可对学生信息进行输入如学号,姓名,班级。
3.4:驱动连接模块:
Class.forName(“com.mysql.jdbc.Driver”);此语句适用于mysql5.5版本的情况,高版本的mysql没有测试过,不知道可不可以。
并使用以下语句进行连接:jdbc:mysql://localhost:3306/jiaoxueguanlixit?user="+userMySql+"&password="+passwordMySql+"&useUnicode=true&characterEncoding=gbk&useSSL=false&serverTimezone=GMT;3306是表示连接地址,基本上连接地址不要更改;jiaoxueguanlixit表示你所要连接的数据库名;笔者后面增加了一个用户名和密码,创造了一个登陆界面,后面会提及。
4:详细的代码及展示图:
一:登录界面
利用eclipse做出登录界面,然后在数据库里面进行用户名和密码的定义,笔者创造了一个用户名为zhangsan,密码为12345678的合法用户。
只要登录成功就会出现“登录成功界面”。
以下是控制登录代码:
package 教学管理系统;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JLayeredPane;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.JTextField;
public class Login extends JFrame implements ActionListener {
private static final long serialVersionUID = 5252772035964963789L;
private JPanel pan = new JPanel();
private JLabel namelab = new JLabel("用户名");
private JLabel passlab = new JLabel("密码");
private JTextField nametext = new JTextField();
private JPasswordField passtext = new JPasswordField();
public JButton denglu = new JButton("登录");
public JButton chongzhi = new JButton("重置");
ImageIcon image;
public Login(){
this.setLocation(550, 250);
Font font = new Font("宋体",Font.BOLD,12);
super.setTitle("欢迎登录教务系统");
pan.setLayout(null);
namelab.setBounds(20,20,60,30);
nametext.setBounds(90,20,140,30);
passlab.setBounds(20,60,60,30);
passtext.setBounds(90,60,140,30);
denglu.setBounds(30,120,90,20);
chongzhi.setBounds(140,120,90,20);
pan.add(namelab);
pan.add(nametext);
pan.add(passlab);
pan.add(passtext);
pan.add(denglu);
pan.add(chongzhi);
passtext.setFont(font);
chongzhi.setFont(font);
denglu.addActionListener(this);
chongzhi.addActionListener(this);
super.add(pan);
super.setSize(300,200);
super.setVisible(true);
passtext.addKeyListener(new KeyAdapter() {
public void keyPressed(KeyEvent e2) {
if(e2.getKeyChar()==KeyEvent.VK_ENTER) {//如果密码是enter键
denglu.doClick();//点击登录按钮
} }
});
}
public static void main(String []args){
new Login();
}
public void actionPerformed(ActionEvent arg0) {
if(arg0.getSource()==denglu){
denglu();
}else if (arg0.getSource()==chongzhi){
chongzhi();
}
}
//登录按钮的事件处理函数
public void denglu(){
Jdbc d = new Jdbc();
String username = nametext.getText();
char[] password = passtext.getPassword();
if(d.compare(username, password)){
JOptionPane.showMessageDialog(null, "登陆成功!");
dispose();
new SystemUI();
}
}
//重置按钮触发后的事件处理函数
public void chongzhi() {
nametext.setText("");
passtext.setText("");
}
}
class Jdbc {
Connection con = null;
Statement statement = null;
ResultSet res = null;
String driver = "com.mysql.jdbc.Driver";
String url = "jdbc:mysql://127.0.0.1:3306/jiaoxueguanlixit?serverTimezone=UTC&useUnicode=true&characterEncoding=UTF-8";
String name = "zhangsan";
String passwd = "12345678";
public Jdbc(){
try{
Class.forName(driver);
Connection con = DriverManager.getConnection(url,name,passwd);//与数据库建立连接;
statement = con.createStatement();
}catch(ClassNotFoundException e){
System.out.println("对不起,找不到这个Driver");
e.printStackTrace();
}catch(SQLException e){
e.printStackTrace();
}catch(Exception e){
e.printStackTrace();
}
}
//对比用户名和密码是不匹配
public boolean compare(String username,char[] password){
String pwd = String.valueOf(password);
boolean m = false;
String sql = "select userpass from userpass where username='"+username+"'";
try{
res = statement.executeQuery(sql);
if(res.next()){
String pa = res.getString(1);
System.out.println(pa+" " +pwd);
if(pa.equals(pwd)){
m = true;
}else {
JOptionPane.showMessageDialog(null, "密码错误!");
}
}else {
JOptionPane.showMessageDialog(null, "用户名不存在!");
}
}catch(SQLException e){
e.printStackTrace();
}
return m;
}
}
二:登录进去之后就会出现一个信息界面:
里面包含了老师信息,学生信息,课程信息,选课信息和成绩管理。
2.1教师信息控制代码:
包括添加,修改,删除,搜索,刷新操作。
添加一个老师信息:
更改老师的信息:
删除老师信息:选中删除的一行:
搜索教师信息:
package 教学管理系统;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Vector;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.ListSelectionModel;
import javax.swing.table.DefaultTableCellRenderer;
import java.util.Date;
public class Teacher {
Vector<Object> columnNames;
private Vector<Vector<Object>> rowData = new Vector<Vector<Object>>();
static JTable table1=null;
//定义数据库需要的全局变量
PreparedStatement ps=null;
Connection ct=null;
ResultSet rs=null;//将对数据库查询的结果保存进resultset的对象rs中
public Teacher(){
columnNames=new Vector<Object>();
//设置列名
columnNames.add("工号");
columnNames.add("姓名");
columnNames.add("部门");
//rowData可以存放多行,开始从数据库里取
try {
String userMySql="zhangsan";
String passwordMySql="12345678";
//加载驱动
Class.forName("com.mysql.jdbc.Driver");
//得到连接,并获取连接对象ct
ct=DriverManager.getConnection("jdbc:mysql://localhost:3306/jiaoxueguanlixit?user="
+userMySql+"&password="+passwordMySql + "&useUnicode=true&characterEncoding=gbk&useSSL=false&serverTimezone=GMT");
ps=ct.prepareStatement("select * from teacher");//向mysql发送动态的sql语句
rs=ps.executeQuery();//使用select语句时用executeQuery执行,executeupdate用于执行insert,update,delete,execute用于返回多个结果集的语句。
while(rs.next()){
//rowData可以存放多行
Vector<Object> hang=new Vector<Object>();
hang.add(rs.getInt(1));
hang.add(rs.getString(2));
hang.add(rs.getString(3));
//加入到rowData
rowData.add(hang);
}
} catch (Exception e) {
e.printStackTrace();
} finally{
try {
if(rs!=null){
rs.close();
}
if(ps!=null){
ps.close();
}
if(ct!=null){
ct.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
table1=new JTable(rowData,columnNames) {
private static final long serialVersionUID=-3229560868878458304L;
public boolean isCellEditable(int row,int column) {
return false;
}
};
table1.getSelectionModel().setSelectionMode(ListSelectionModel.SINGLE_SELECTION);//只允许选中一行
DefaultTableCellRenderer tcr = new DefaultTableCellRenderer();// 设置table内容居中
tcr.setHorizontalAlignment(JLabel.CENTER);// 这句和上句作用一样
table1.setDefaultRenderer(Object.class, tcr);
}
//添加教师的信息;
public void addTeacher() {
// TODO 自动生成的方法存根
SystemUI.unclickable();
JFrame AddTea = new JFrame("添加教师信息");
AddTea.setSize(250, 270);
AddTea.setLocation(600, 300);
JPanel addtea = new JPanel();
JLabel teacherid = new JLabel("工号");
JLabel teachername = new JLabel("姓名");
JLabel teacherdepartment = new JLabel("部门");
JTextField teacheridtext = new JTextField();
JTextField teachernametext = new JTextField();
JTextField teacherdepartmenttext = new JTextField();
JButton ok = new JButton("确定");
JButton reset = new JButton("重置");
addtea.setLayout(null);
teacherid.setBounds(5,5,70,20);
teacheridtext.setBounds(80,5,120,20);
teachername.setBounds(5,30,70,20);
teachernametext.setBounds(80,30,120,20);
teacherdepartment.setBounds(5,60,70,20);
teacherdepartmenttext.setBounds(80,60,120,20);
ok.setBounds(50,190,60,20);
reset.setBounds(130,190,60,20);
addtea.add(teacherid);
addtea.add(teacheridtext);
addtea.add(teachername);
addtea.add(teachernametext);
addtea.add(teacherdepartment);
addtea.add(teacherdepartmenttext);
addtea.add(ok);
addtea.add(reset);
AddTea.add(addtea);
AddTea.setVisible(true);
AddTea.addWindowListener(new WindowAdapter(){
@Override
public void windowClosing(WindowEvent e) {
SystemUI.clickable();
}
});
ok.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent arg0) {
try {
String userMySql="zhangsan";
String passwordMySql="12345678";
String name = teachernametext.getText();
String department = teacherdepartmenttext.getText();
int id = Integer.valueOf(teacheridtext.getText()).intValue();
//加载驱动
Class.forName("com.mysql.jdbc.Driver");
//得到连接
ct=DriverManager.getConnection("jdbc:mysql://localhost:3306/jiaoxueguanlixit?user="
+userMySql+"&password="+passwordMySql + "&useUnicode=true&characterEncoding=gbk&useSSL=false&serverTimezone=GMT");
ps=ct.prepareStatement("insert into teacher values('"+id+"','"+name+"','"+department+"')");
ps.executeUpdate();
JOptionPane.showMessageDialog(null, "添加成功!");
AddTea.dispose();
SystemUI.clickable();
addTeacher();
} catch (Exception e) {
e.printStackTrace();
JOptionPane.showMessageDialog(null, "添加失败!");
SystemUI.unclickable();
} finally{
try {
if(rs!=null){
rs.close();
}
if(ps!=null){
ps.close();
}
if(ct!=null){
ct.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
});
reset.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent arg0) {
teacheridtext.setText("");
teachernametext.setText("");
teacherdepartmenttext.setText("");
}
});
}
//修改教师的信息;
public void updateTeacher() {
// TODO 自动生成的方法存根
SystemUI.unclickable();
JFrame UpdateTea = new JFrame("修改教师信息");
UpdateTea.setSize(250, 290);
UpdateTea.setLocation(600, 300);
JPanel updatetea = new JPanel();
JLabel upid = new JLabel("要修改的工号");
JLabel upname = new JLabel("要改成的姓名");
JLabel updepartment = new JLabel("要改成的部门");
JTextField upidtext = new JTextField();
JTextField upnametext = new JTextField();
JTextField updepartmenttext = new JTextField();
JButton ok = new JButton("确定");
JButton reset = new JButton("重置");
updatetea.setLayout(null);
upid.setBounds(5,30,100,20);
upidtext.setBounds(110,30,120,20);
upname.setBounds(5,60,100,20);
upnametext.setBounds(110,60,120,20);
updepartment.setBounds(5,90,100,20);
updepartmenttext.setBounds(110,90,120,20);
ok.setBounds(50,210,60,20);
reset.setBounds(120,210,60,20);
updatetea.add(upid);
updatetea.add(upidtext);
updatetea.add(upname);
updatetea.add(upnametext);
updatetea.add(updepartment);
updatetea.add(updepartmenttext);
updatetea.add(ok);
updatetea.add(reset);
UpdateTea.add(updatetea);
UpdateTea.setVisible(true);
UpdateTea.addWindowListener(new WindowAdapter(){
@Override
public void windowClosing(WindowEvent e) {
SystemUI.clickable();
}
});
ok.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent arg0) {
try { String userMySql="zhangsan";
String passwordMySql="12345678";
//String id = upidtext.getText();
String name = upnametext.getText();
String department = updepartmenttext.getText();
int id = Integer.valueOf(upidtext.getText()).intValue();
//加载驱动
Class.forName("com.mysql.jdbc.Driver");
//得到连接
ct=DriverManager.getConnection("jdbc:mysql://localhost:3306/jiaoxueguanlixit?user="
+userMySql+"&password="+passwordMySql + "&useUnicode=true&characterEncoding=gbk&useSSL=false&serverTimezone=GMT");
ps=ct.prepareStatement("update teacher set teachername ='"+name+"',teacherdepartment ='"+department+"' where teacherid='"+id+"'");
ps.executeUpdate();
JOptionPane.showMessageDialog(null, "修改成功!");
UpdateTea.dispose();
SystemUI.clickable();
addTeacher();
} catch (Exception e) {
e.printStackTrace();
JOptionPane.showMessageDialog(null, "修改失败!");
SystemUI.unclickable();
} finally{
try {
if(rs!=null){
rs.close();
}
if(ps!=null){
ps.close();
}
if(ct!=null){
ct.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
});
reset.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent arg0) {
upidtext.setText("");
upnametext.setText("");
updepartmenttext.setText("");
}
});
}
//删除教师信息
public void deleteTeacher() {
// TODO 自动生成的方法存根
int row = table1.getSelectedRow();
if ( row >= 0 ) {
String string[] = new String[5];
string[0] = String.valueOf(table1.getValueAt(row, 0));
string[1] = String.valueOf(table1.getValueAt(row, 1));
try { String userMySql="zhangsan";
String passwordMySql="12345678";
//加载驱动
Class.forName("com.mysql.jdbc.Driver");
//得到连接
ct=DriverManager.getConnection("jdbc:mysql://localhost:3306/jiaoxueguanlixit?user="
+userMySql+"&password="+passwordMySql + "&useUnicode=true&characterEncoding=gbk&useSSL=false&serverTimezone=GMT");
ps=ct.prepareStatement("delete from teacher where teacherid='"+string[0]+"' and teachername='"+string[1]+"'");
ps.executeUpdate();
JOptionPane.showMessageDialog(null, "删除成功!");
refreshTeacher();
row=-1;
} catch (Exception e) {
e.printStackTrace();
JOptionPane.showMessageDialog(null, "删除失败!");
} finally{
try {
if(rs!=null){
rs.close();
}
if(ps!=null){
ps.close();
}
if(ct!=null){
ct.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
else {
JOptionPane.showMessageDialog(null, "请选中要删除的行!");
}
}
public void searchTeacher() {
// TODO 自动生成的方法存根
SystemUI.unclickable();
JFrame SeaTea = new JFrame("查找教师信息");
SeaTea.setSize(250, 100);
SeaTea.setLocation(600, 300);
JPanel seatea = new JPanel();
JLabel tno = new JLabel("请输入工号");
JTextField tnotext = new JTextField();
JButton ok = new JButton("确定");
JButton reset = new JButton("重置");
seatea.setLayout(null);
tno.setBounds(5,5,70,20);
tnotext.setBounds(80,5,120,20);
ok.setBounds(50,30,60,20);
reset.setBounds(130,30,60,20);
seatea.add(tno);
seatea.add(tnotext);
seatea.add(ok);
seatea.add(reset);
SeaTea.add(seatea);
SeaTea.setVisible(true);
SeaTea.addWindowListener(new WindowAdapter(){
@Override
public void windowClosing(WindowEvent e) {
SystemUI.clickable();
}
});
ok.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent arg0) {
columnNames=new Vector<Object>();
//设置列名
columnNames.add("工号");
columnNames.add("姓名");
columnNames.add("部门");
String str = tnotext.getText();
int count=0;
try { String userMySql="zhangsan";
String passwordMySql="12345678";
//加载驱动
Class.forName("com.mysql.jdbc.Driver");
//得到连接
ct=DriverManager.getConnection("jdbc:mysql://localhost:3306/jiaoxueguanlixit?user="
+userMySql+"&password="+passwordMySql + "&useUnicode=true&characterEncoding=gbk&useSSL=false&serverTimezone=GMT");
if(!tnotext.getText().trim().equals("")) {
ps=ct.prepareStatement("select * from teacher where teacherid='"+str+"'");
rs=ps.executeQuery();
}
if(tnotext.getText().trim().equals("")) {
ps=ct.prepareStatement("select * from teacher ");
rs=ps.executeQuery();
JOptionPane.showMessageDialog(null, "请输入查询信息!");
}
while(rs.next()){
//rowData可以存放多行
Vector<Object> hang=new Vector<Object>();
hang.add(rs.getInt(1));
hang.add(rs.getString(2));
hang.add(rs.getString(3));
//加入到rowData
rowData.add(hang);
count++;
}
} catch (Exception e) {
e.printStackTrace();
JOptionPane.showMessageDialog(null, "查询失败!");
} finally{
try {
if(rs!=null){
rs.close();
}
if(ps!=null){
ps.close();
}
if(ct!=null){
ct.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
if(count!=0) {
table1 = new JTable(rowData,columnNames){
private static final long serialVersionUID = -1545099432791772807L;
public boolean isCellEditable(int row, int column)
{return false;}//表格不允许被编辑
};
table1.getSelectionModel().setSelectionMode(ListSelectionModel.SINGLE_SELECTION);//只允许选中一行
DefaultTableCellRenderer tcr = new DefaultTableCellRenderer();// 设置table内容居中
tcr.setHorizontalAlignment(JLabel.CENTER);// 这句和上句作用一样
table1.setDefaultRenderer(Object.class, tcr);
SystemUI.scrollPane1.setViewportView(table1);
SeaTea.dispose();
SystemUI.clickable();}
if(count==0) {
JOptionPane.showMessageDialog(null, "查无此人!");
}
}
});
}
public void refreshTeacher() {
// TODO 自动生成的方法存根
new Teacher();
SystemUI.scrollPane1.setViewportView(Teacher.table1);
}
}
学生信息和老师信息的操作大致差不多:代码如下:
package 教学管理系统;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Vector;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.ListSelectionModel;
import javax.swing.table.DefaultTableCellRenderer;
public class Student {
private Vector<Vector<Object>> rowData = new Vector<Vector<Object>>();
String userMySql="zhangsan";
String passwordMySql="12345678";
//Vector<Object> rowData,columnNames;
Vector<Object> columnNames;
static JTable table2=null;
//定义数据库需要的全局变量
PreparedStatement ps=null;
Connection ct=null;
ResultSet rs=null;
public Student(){
columnNames=new Vector<Object>();
//设置列名
columnNames.add("学号");
columnNames.add("姓名");
columnNames.add("班级");
//rowData可以存放多行,开始从数据库里取
try {
String userMySql="zhangsan";
String passwordMySql="12345678";
//加载驱动
Class.forName("com.mysql.jdbc.Driver");
//得到连接
ct=DriverManager.getConnection("jdbc:mysql://localhost:3306/jiaoxueguanlixit?user="
+userMySql+"&password="+passwordMySql + "&useUnicode=true&characterEncoding=gbk&useSSL=false&serverTimezone=GMT");
ps=ct.prepareStatement("select * from student");
rs=ps.executeQuery();
while(rs.next()){
//rowData可以存放多行
Vector<Object> hang=new Vector<Object>();
hang.add(rs.getInt(1));
hang.add(rs.getString(2));
hang.add(rs.getString(3));
//加入到rowData
rowData.add(hang);
}
} catch (Exception e) {
e.printStackTrace();
} finally{
try {
if(rs!=null){
rs.close();
}
if(ps!=null){
ps.close();
}
if(ct!=null){
ct.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
table2 = new JTable(rowData,columnNames){
private static final long serialVersionUID = -4333506764666173598L;
public boolean isCellEditable(int row, int column)
{return false;}//表格不允许被编辑
};
table2.getSelectionModel().setSelectionMode(ListSelectionModel.SINGLE_SELECTION);//只允许选中一行
DefaultTableCellRenderer tcr = new DefaultTableCellRenderer();// 设置table内容居中
tcr.setHorizontalAlignment(JLabel.CENTER);// 这句和上句作用一样
table2.setDefaultRenderer(Object.class, tcr);
}
public void refreshStudent() {
// TODO 自动生成的方法存根
new Student();
SystemUI.scrollPane2.setViewportView(Student.table2);
}
public void searchStudent() {
// TODO 自动生成的方法存根
SystemUI.unclickable();
JFrame SeaStu = new JFrame("查找学生信息");
SeaStu.setSize(250, 100);
SeaStu.setLocation(600, 300);
JPanel seastu = new JPanel();
JLabel sno = new JLabel("请输入学号");
JTextField snotext = new JTextField();
JButton ok = new JButton("确定");
JButton reset = new JButton("重置");
seastu.setLayout(null);
sno.setBounds(5,5,70,20);
snotext.setBounds(80,5,120,20);
ok.setBounds(50,30,60,20);
reset.setBounds(130,30,60,20);
seastu.add(sno);
seastu.add(snotext);
seastu.add(ok);
seastu.add(reset);
SeaStu.add(seastu);
SeaStu.setVisible(true);
SeaStu.addWindowListener(new WindowAdapter(){
@Override
public void windowClosing(WindowEvent e) {
SystemUI.clickable();
}
});
ok.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent arg0) {
columnNames=new Vector<Object>();
//设置列名
columnNames.add("学号");
columnNames.add("姓名");
columnNames.add("班级");
String str = snotext.getText();
//rowData = new Vector<Object>();
int count=0;
//rowData可以存放多行,开始从数据库里取
try {
//加载驱动
Class.forName("com.mysql.jdbc.Driver");
//得到连接
ct=DriverManager.getConnection("jdbc:mysql://localhost:3306/jiaoxueguanlixit?user="
+userMySql+"&password="+passwordMySql + "&useUnicode=true&characterEncoding=gbk&useSSL=false&serverTimezone=GMT");
if(!snotext.getText().trim().equals("")) {
ps=ct.prepareStatement("select * from student where studentid='"+str+"'");
rs=ps.executeQuery();
}
if(snotext.getText().trim().equals("")) {
ps=ct.prepareStatement("select * from student ");
JOptionPane.showMessageDialog(null, "请输入查询信息!");
rs=ps.executeQuery();
}
while(rs.next()){
//rowData可以存放多行
Vector<Object> hang=new Vector<Object>();
hang.add(rs.getInt(1));
hang.add(rs.getString(2));
hang.add(rs.getString(3));
//加入到rowData
rowData.add(hang);
count++;
}
} catch (Exception e) {
e.printStackTrace();
JOptionPane.showMessageDialog(null, "查询失败!");
} finally{
try {
if(rs!=null){
rs.close();
}
if(ps!=null){
ps.close();
}
if(ct!=null){
ct.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
if(count!=0) {
table2 = new JTable(rowData,columnNames){
private static final long serialVersionUID = -8131400305374145108L;
public boolean isCellEditable(int row, int column)
{return false;}//表格不允许被编辑
};
table2.getSelectionModel().setSelectionMode(ListSelectionModel.SINGLE_SELECTION);//只允许选中一行
DefaultTableCellRenderer tcr = new DefaultTableCellRenderer();// 设置table内容居中
tcr.setHorizontalAlignment(JLabel.CENTER);// 这句和上句作用一样
table2.setDefaultRenderer(Object.class, tcr);
SystemUI.scrollPane2.setViewportView(table2);
SeaStu.dispose();
SystemUI.clickable();}
if(count==0) {
JOptionPane.showMessageDialog(null, "查无此人!");
}
}
});
reset.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent arg0) {
snotext.setText("");
}
});
}
public void deleteStudent() {
// TODO 自动生成的方法存根
int row = table2.getSelectedRow();
if ( row >= 0 ) {
String string[] = new String[3];
string[0] = String.valueOf(table2.getValueAt(row, 0));
string[1] = String.valueOf(table2.getValueAt(row, 1));
try {
//加载驱动
Class.forName("com.mysql.jdbc.Driver");
//得到连接
ct=DriverManager.getConnection("jdbc:mysql://localhost:3306/jiaoxueguanlixit?user="
+userMySql+"&password="+passwordMySql + "&useUnicode=true&characterEncoding=gbk&useSSL=false&serverTimezone=GMT");
ps=ct.prepareStatement("delete from student where studentid='"+string[0]+"' and studentname='"+string[1]+"'");
ps.executeUpdate();
JOptionPane.showMessageDialog(null, "删除成功!");
refreshStudent();
row=-1;
} catch (Exception e) {
e.printStackTrace();
JOptionPane.showMessageDialog(null, "删除失败!");
} finally{
try {
if(rs!=null){
rs.close();
}
if(ps!=null){
ps.close();
}
if(ct!=null){
ct.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
else {
JOptionPane.showMessageDialog(null, "请选中要删除的行!");
}
}
public void addStudent() {
// TODO 自动生成的方法存根
SystemUI.unclickable();
JFrame AddStu = new JFrame("添加学生信息");
AddStu.setSize(250, 270);
AddStu.setLocation(600, 300);
JPanel addStu = new JPanel();
JLabel sno = new JLabel("学号");
JLabel sname = new JLabel("姓名");
JLabel sclass = new JLabel("班级");
JTextField snotext = new JTextField();
JTextField snametext = new JTextField();
JTextField sclasstext = new JTextField();
JButton ok = new JButton("确定");
JButton reset = new JButton("重置");
addStu.setLayout(null);
sno.setBounds(5,5,70,20);
snotext.setBounds(80,5,120,20);
sname.setBounds(5,30,70,20);
snametext.setBounds(80,30,120,20);
sclass.setBounds(5,60,70,20);
sclasstext.setBounds(80,60,120,20);
ok.setBounds(50,190,60,20);
reset.setBounds(130,190,60,20);
addStu.add(sno);
addStu.add(snotext);
addStu.add(sname);
addStu.add(snametext);
addStu.add(sclass);
addStu.add(sclasstext);
addStu.add(ok);
addStu.add(reset);
AddStu.add(addStu);
AddStu.setVisible(true);
AddStu.addWindowListener(new WindowAdapter(){
@Override
public void windowClosing(WindowEvent e) {
SystemUI.clickable();
}
});
ok.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent arg0) {
try {
String userMySql="zhangsan";
String passwordMySql="12345678";
String name = snametext.getText();
String sclass = sclasstext.getText();
int no = Integer.valueOf(snotext.getText()).intValue();
//加载驱动
Class.forName("com.mysql.jdbc.Driver");
//得到连接
ct=DriverManager.getConnection("jdbc:mysql://localhost:3306/jiaoxueguanlixit?user="
+userMySql+"&password="+passwordMySql + "&useUnicode=true&characterEncoding=gbk&useSSL=false&serverTimezone=GMT");
ps=ct.prepareStatement("insert into student values('"+no+"','"+name+"','"+sclass+"')");
ps.executeUpdate();
JOptionPane.showMessageDialog(null, "添加成功!");
AddStu.dispose();
SystemUI.clickable();
refreshStudent();
} catch (Exception e) {
e.printStackTrace();
JOptionPane.showMessageDialog(null, "添加失败!");
SystemUI.unclickable();
} finally{
try {
if(rs!=null){
rs.close();
}
if(ps!=null){
ps.close();
}
if(ct!=null){
ct.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
});
reset.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent arg0) {
snotext.setText("");
snametext.setText("");
sclasstext.setText("");
}
});
}
//修改学生信息;
public void updateStudent() {
// TODO 自动生成的方法存根
SystemUI.unclickable();
JFrame UpdateStu = new JFrame("修改学生信息");
UpdateStu.setSize(250, 250);
UpdateStu.setLocation(600, 300);
JPanel updatestu = new JPanel();
JLabel upno = new JLabel("要修改的学号");
JLabel ssex = new JLabel("要改成的姓名");
JLabel upclass = new JLabel("要改成的班级");
JTextField upnotext = new JTextField();
JTextField ssextext = new JTextField();
JTextField upclasstext = new JTextField();
JButton ok = new JButton("确定");
JButton reset = new JButton("重置");
updatestu.setLayout(null);
upno.setBounds(5,5,100,20);
upnotext.setBounds(110,5,120,20);
ssex.setBounds(5,30,100,20);
ssextext.setBounds(110,30,120,20);
upclass.setBounds(5,60,100,20);
upclasstext.setBounds(110,60,120,20);
ok.setBounds(50,170,60,20);
reset.setBounds(120,170,60,20);
updatestu.add(upno);
updatestu.add(upnotext);
updatestu.add(ssex);
updatestu.add(ssextext);
updatestu.add(upclass);
updatestu.add(upclasstext);
updatestu.add(ok);
updatestu.add(reset);
UpdateStu.add(updatestu);
UpdateStu.setVisible(true);
UpdateStu.addWindowListener(new WindowAdapter(){
@Override
public void windowClosing(WindowEvent e) {
SystemUI.clickable();
}
});
ok.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent arg0) {
try {
String sclass = upclasstext.getText();
String sname=ssextext.getText();
int yno = Integer.valueOf(upnotext.getText()).intValue();
//加载驱动
Class.forName("com.mysql.jdbc.Driver");
//得到连接
ct=DriverManager.getConnection("jdbc:mysql://localhost:3306/jiaoxueguanlixit?user="
+userMySql+"&password="+passwordMySql + "&useUnicode=true&characterEncoding=gbk&useSSL=false&serverTimezone=GMT");
ps=ct.prepareStatement("update student set class ='"+sclass+"',studentname='"+sname+"' where studentid='"+yno+"'");
ps.executeUpdate();
JOptionPane.showMessageDialog(null, "修改成功!");
UpdateStu.dispose();
SystemUI.clickable();
refreshStudent();
} catch (Exception e) {
e.printStackTrace();
JOptionPane.showMessageDialog(null, "修改失败!");
SystemUI.unclickable();
} finally{
try {
if(rs!=null){
rs.close();
}
if(ps!=null){
ps.close();
}
if(ct!=null){
ct.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
});
reset.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent arg0) {
upclasstext.setText("");
}
});
}
}
课程信息和老师信息操作差不多,代码如下:
package 教学管理系统;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Vector;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.ListSelectionModel;
import javax.swing.table.DefaultTableCellRenderer;
public class Course {
private Vector<Vector<Object>> rowData = new Vector<Vector<Object>>();
String userMySql="zhangsan";
String passwordMySql="12345678";
Vector<Object> columnNames;
//定义数据库需要的全局变量
PreparedStatement ps=null;
Connection ct=null;
ResultSet rs=null;
static JTable table3=null;
public Course(){
columnNames=new Vector<Object>();
//设置列名
columnNames.add("课程号");
columnNames.add("课程名");
columnNames.add("先修课课程号");
columnNames.add("学分");
columnNames.add("类型");
//rowData = new Vector<Object>();
//rowData可以存放多行,开始从数据库里取
try {
String userMySql="zhangsan";
String passwordMySql="12345678";
//加载驱动
Class.forName("com.mysql.jdbc.Driver");
//得到连接
ct=DriverManager.getConnection("jdbc:mysql://localhost:3306/jiaoxueguanlixit?user="
+userMySql+"&password="+passwordMySql + "&useUnicode=true&characterEncoding=gbk&useSSL=false&serverTimezone=GMT");
ps=ct.prepareStatement("select * from course");
rs=ps.executeQuery();
while(rs.next()){
//rowData可以存放多行
Vector<Object> hang=new Vector<Object>();
hang.add(rs.getInt(1));
hang.add(rs.getString(2));
hang.add(rs.getString(3));
hang.add(rs.getInt(4));
hang.add(rs.getString(5));
//加入到rowData
rowData.add(hang);
}
} catch (Exception e) {
e.printStackTrace();
} finally{
try {
if(rs!=null){
rs.close();
}
if(ps!=null){
ps.close();
}
if(ct!=null){
ct.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
table3 = new JTable(rowData,columnNames){
private static final long serialVersionUID = 6843971351064905400L;
public boolean isCellEditable(int row, int column)
{return false;}//表格不允许被编辑
};
table3.getSelectionModel().setSelectionMode(ListSelectionModel.SINGLE_SELECTION);//只允许选中一行
DefaultTableCellRenderer tcr = new DefaultTableCellRenderer();// 设置table内容居中
tcr.setHorizontalAlignment(JLabel.CENTER);// 这句和上句作用一样
table3.setDefaultRenderer(Object.class, tcr);
}
public void addCourse() { //增加课程
// TODO 自动生成的方法存根
SystemUI.unclickable();
JFrame AddCou = new JFrame("添加课程信息");
AddCou.setSize(250, 250);
AddCou.setLocation(600, 300);
JPanel addCou = new JPanel();
JLabel cno = new JLabel("课程号");
JLabel cname = new JLabel("课程名");
JLabel cpno = new JLabel("先修课");
JLabel credit = new JLabel("学分");
JLabel cremark = new JLabel("类型");
JTextField cnotext = new JTextField();
JTextField cnametext = new JTextField();
JTextField cpnotext = new JTextField();
JTextField credittext = new JTextField();
JTextField cremarktext = new JTextField();
JButton ok = new JButton("确定");
JButton reset = new JButton("重置");
addCou.setLayout(null);
cno.setBounds(5,5,70,20);
cnotext.setBounds(80,5,120,20);
cname.setBounds(5,30,70,20);
cnametext.setBounds(80,30,120,20);
cpno.setBounds(5,60,70,20);
cpnotext.setBounds(80,60,120,20);
credit.setBounds(5,90,70,20);
credittext.setBounds(80,90,120,20);
cremark.setBounds(5,120,70,20);
cremarktext.setBounds(80,120,120,20);
ok.setBounds(50,150,60,20);
reset.setBounds(130,150,60,20);
addCou.add(cno);
addCou.add(cnotext);
addCou.add(cname);
addCou.add(cnametext);
addCou.add(cpno);
addCou.add(cpnotext);
addCou.add(credit);
addCou.add(credittext);
addCou.add(cremark);
addCou.add(cremarktext);
addCou.add(ok);
addCou.add(reset);
AddCou.add(addCou);
AddCou.setVisible(true);
AddCou.addWindowListener(new WindowAdapter(){
@Override
public void windowClosing(WindowEvent e) {
SystemUI.clickable();
}
});
ok.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent arg0) {
try {
String userMySql="zhangsan";
String passwordMySql="12345678";
String no = cnotext.getText();
String name = cnametext.getText();
String pno = cpnotext.getText();
String remark = cremarktext.getText();
int credit = Integer.valueOf(credittext.getText()).intValue();
//加载驱动
Class.forName("com.mysql.jdbc.Driver");
//得到连接
ct=DriverManager.getConnection("jdbc:mysql://localhost:3306/jiaoxueguanlixit?user="
+userMySql+"&password="+passwordMySql + "&useUnicode=true&characterEncoding=gbk&useSSL=false&serverTimezone=GMT");
String sql;
sql = "insert into course values('"+no+"','"+name+"','"+pno+"',"+credit+",'"+remark+"')";
ps=ct.prepareStatement(sql);
ps.executeUpdate();
JOptionPane.showMessageDialog(null, "添加成功!");
AddCou.dispose();
SystemUI.clickable();
refreshCourse();
} catch (Exception e) {
e.printStackTrace();
JOptionPane.showMessageDialog(null, "添加失败!");
SystemUI.unclickable();
} finally{
try {
if(rs!=null){
rs.close();
}
if(ps!=null){
ps.close();
}
if(ct!=null){
ct.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
});
reset.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent arg0) {
cnotext.setText("");
cnametext.setText("");
cpnotext.setText("");
credittext.setText("");
cremarktext.setText("");
}
});
}
public void updateCourse() {
// TODO 自动生成的方法存根
SystemUI.unclickable();
JFrame UpdateCou = new JFrame("修改课程信息");
UpdateCou.setSize(250, 200);
UpdateCou.setLocation(600, 300);
JPanel updatecou = new JPanel();
JLabel ycno = new JLabel("要修改的课程号");
JLabel cpno = new JLabel("要改成的先修课");
JLabel credit = new JLabel("要改成的学分");
JLabel cremark = new JLabel("要改成的类型");
JTextField ycnotext = new JTextField();
JTextField cpnotext = new JTextField();
JTextField credittext = new JTextField();
JTextField cremarktext = new JTextField();
JButton ok = new JButton("确定");
JButton reset = new JButton("重置");
updatecou.setLayout(null);
ycno.setBounds(5,5,100,20);
ycnotext.setBounds(110,5,120,20);
cpno.setBounds(5,30,100,20);
cpnotext.setBounds(110,30,120,20);
credit.setBounds(5,60,100,20);
credittext.setBounds(110,60,120,20);
cremark.setBounds(5,90,100,20);
cremarktext.setBounds(110,90,120,20);
ok.setBounds(50,130,60,20);
reset.setBounds(120,130,60,20);
updatecou.add(ycno);
updatecou.add(ycnotext);
updatecou.add(cpno);
updatecou.add(cpnotext);
updatecou.add(credit);
updatecou.add(credittext);
updatecou.add(cremark);
updatecou.add(cremarktext);
updatecou.add(ok);
updatecou.add(reset);
UpdateCou.add(updatecou);
UpdateCou.setVisible(true);
UpdateCou.addWindowListener(new WindowAdapter(){
@Override
public void windowClosing(WindowEvent e) {
SystemUI.clickable();
}
});
ok.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent arg0) {
try {
String yno = ycnotext.getText();
String pno = cpnotext.getText();
String remark = cremarktext.getText();
int credit = Integer.valueOf(credittext.getText()).intValue();
//加载驱动
Class.forName("com.mysql.jdbc.Driver");
//得到连接
ct=DriverManager.getConnection("jdbc:mysql://localhost:3306/jiaoxueguanlixit?user="
+userMySql+"&password="+passwordMySql + "&useUnicode=true&characterEncoding=gbk&useSSL=false&serverTimezone=GMT");
String sql;
sql = "update course set beforecourse ='"+pno+"', xuefen ='"+credit+"', leixing ='"+remark+"' where courseid='"+yno+"'";
ps=ct.prepareStatement(sql);
ps.executeUpdate();
JOptionPane.showMessageDialog(null, "修改成功!");
UpdateCou.dispose();
SystemUI.clickable();
refreshCourse();
} catch (Exception e) {
e.printStackTrace();
JOptionPane.showMessageDialog(null, "修改失败!");
SystemUI.unclickable();
} finally{
try {
if(rs!=null){
rs.close();
}
if(ps!=null){
ps.close();
}
if(ct!=null){
ct.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
});
reset.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent arg0) {
ycnotext.setText("");
cpnotext.setText("");
credittext.setText("");
cremarktext.setText("");
}
});
}
public void deleteCourse() {
// TODO 自动生成的方法存根
int row = table3.getSelectedRow();
if ( row >= 0 ) {
String string[] = new String[3];
string[0] = String.valueOf(table3.getValueAt(row, 0));
string[1] = String.valueOf(table3.getValueAt(row, 1));
try {
//加载驱动
Class.forName("com.mysql.jdbc.Driver");
//得到连接
ct=DriverManager.getConnection("jdbc:mysql://localhost:3306/jiaoxueguanlixit?user="
+userMySql+"&password="+passwordMySql + "&useUnicode=true&characterEncoding=gbk&useSSL=false&serverTimezone=GMT");
ps=ct.prepareStatement("delete from course where courseid='"+string[0]+"' and coursename='"+string[1]+"'");
ps.executeUpdate();
JOptionPane.showMessageDialog(null, "删除成功!");
refreshCourse();
row=-1;
} catch (Exception e) {
e.printStackTrace();
JOptionPane.showMessageDialog(null, "删除失败!");
} finally{
try {
if(rs!=null){
rs.close();
}
if(ps!=null){
ps.close();
}
if(ct!=null){
ct.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
else {
JOptionPane.showMessageDialog(null, "请选中要删除的行!");
}
}
public void searchCourse() {
// TODO 自动生成的方法存根
SystemUI.unclickable();
JFrame SeaCou = new JFrame("查找课程信息");
SeaCou.setSize(250, 100);
SeaCou.setLocation(600, 300);
JPanel seacou = new JPanel();
JLabel cno = new JLabel("请输入课程号");
JTextField cnotext = new JTextField();
JButton ok = new JButton("确定");
JButton reset = new JButton("重置");
seacou.setLayout(null);
cno.setBounds(5,5,80,20);
cnotext.setBounds(90,5,120,20);
ok.setBounds(50,30,60,20);
reset.setBounds(130,30,60,20);
seacou.add(cno);
seacou.add(cnotext);
seacou.add(ok);
seacou.add(reset);
SeaCou.add(seacou);
SeaCou.setVisible(true);
SeaCou.addWindowListener(new WindowAdapter(){
@Override
public void windowClosing(WindowEvent e) {
SystemUI.clickable();
}
});
ok.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent arg0) {
columnNames=new Vector<Object>();
//设置列名
columnNames.add("课程号");
columnNames.add("课程名");
columnNames.add("先修课课程号");
columnNames.add("学分");
columnNames.add("类型");
String str = cnotext.getText();
//rowData = new Vector<Object>();
int count=0;
//rowData可以存放多行,开始从数据库里取
try {
//加载驱动
Class.forName("com.mysql.jdbc.Driver");
//得到连接
ct=DriverManager.getConnection("jdbc:mysql://localhost:3306/jiaoxueguanlixit?user="
+userMySql+"&password="+passwordMySql + "&useUnicode=true&characterEncoding=gbk&useSSL=false&serverTimezone=GMT");
if(!cnotext.getText().trim().equals("")) {
ps=ct.prepareStatement("select * from course where courseid='"+str+"'");
rs=ps.executeQuery();
}
else if(cnotext.getText().trim().equals("")) {
ps=ct.prepareStatement("select * from course ");
rs=ps.executeQuery();
JOptionPane.showMessageDialog(null, "请输入查询信息!");
}
while(rs.next()){
//rowData可以存放多行
Vector<Object> hang=new Vector<Object>();
hang.add(rs.getString(1));
hang.add(rs.getString(2));
hang.add(rs.getString(3));
hang.add(rs.getInt(4));
hang.add(rs.getString(5));
//加入到rowData
rowData.add(hang);
count++;
}
} catch (Exception e) {
e.printStackTrace();
JOptionPane.showMessageDialog(null, "查询失败!");
} finally{
try {
if(rs!=null){
rs.close();
}
if(ps!=null){
ps.close();
}
if(ct!=null){
ct.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
if(count!=0) {
table3 = new JTable(rowData,columnNames){
private static final long serialVersionUID = -8656035973692724899L;
public boolean isCellEditable(int row, int column)
{return false;}//表格不允许被编辑
};
table3.getSelectionModel().setSelectionMode(ListSelectionModel.SINGLE_SELECTION);//只允许选中一行
DefaultTableCellRenderer tcr = new DefaultTableCellRenderer();// 设置table内容居中
tcr.setHorizontalAlignment(JLabel.CENTER);// 这句和上句作用一样
table3.setDefaultRenderer(Object.class, tcr);
SystemUI.scrollPane3.setViewportView(table3);
SeaCou.dispose();
SystemUI.clickable();}
if(count==0) {
JOptionPane.showMessageDialog(null, "没有查询到相关消息!");
}
}
});
reset.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent arg0) {
cnotext.setText("");
}
});
}
public void refreshCourse() {
// TODO 自动生成的方法存根
new Course();
SystemUI.scrollPane3.setViewportView(Course.table3);
}
}
选课信息和老师信息差不多,代码如下:
package 教学管理系统;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Vector;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.ListSelectionModel;
import javax.swing.table.DefaultTableCellRenderer;
public class SelectCourse {
String userMySql="zhangsan";
String passwordMySql="12345678";
private Vector<Vector<Object>> rowData = new Vector<Vector<Object>>();
//Vector<Object> rowData,columnNames;
Vector<Object> columnNames;
//定义数据库需要的全局变量
PreparedStatement ps=null;
Connection ct=null;
ResultSet rs=null;
static JTable table5=null;
public SelectCourse(){
columnNames=new Vector<Object>();
//设置列名
columnNames.add("学号");
columnNames.add("姓名");
columnNames.add("课程号");
columnNames.add("课程名");
// rowData = new Vector<Object>();
//rowData可以存放多行,开始从数据库里取
try {
String userMySql="zhangsan";
String passwordMySql="12345678";
//加载驱动
Class.forName("com.mysql.jdbc.Driver");
System.out.println("加载成功!");
//得到连接
ct=DriverManager.getConnection("jdbc:mysql://localhost:3306/jiaoxueguanlixit?user="
+userMySql+"&password="+passwordMySql + "&useUnicode=true&characterEncoding=gbk&useSSL=false&serverTimezone=GMT");
ps=ct.prepareStatement("select * from selectcourse");
rs=ps.executeQuery();
while(rs.next()){
//rowData可以存放多行
Vector<Object> hang=new Vector<Object>();
hang.add(rs.getString(1));
hang.add(rs.getString(2));
hang.add(rs.getString(3));
hang.add(rs.getString(4));
//加入到rowData
rowData.add(hang);
}
} catch (Exception e) {
e.printStackTrace();
} finally{
try {
if(rs!=null){
rs.close();
}
if(ps!=null){
ps.close();
}
if(ct!=null){
ct.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
table5= new JTable(rowData,columnNames){
private static final long serialVersionUID = -8736911822989769111L;
public boolean isCellEditable(int row, int column)
{return false;}//表格不允许被编辑
};
table5.getSelectionModel().setSelectionMode(ListSelectionModel.SINGLE_SELECTION);//只允许选中一行
DefaultTableCellRenderer tcr = new DefaultTableCellRenderer();// 设置table内容居中
tcr.setHorizontalAlignment(JLabel.CENTER);// 这句和上句作用一样
table5.setDefaultRenderer(Object.class, tcr);
}
public void addSelCou() {
// TODO 自动生成的方法存根
SystemUI.unclickable();
JFrame Addse = new JFrame("添加选课信息");
Addse.setSize(250, 150);
Addse.setLocation(600, 300);
JPanel addse = new JPanel();
JLabel sesno = new JLabel("学号");
JLabel secno = new JLabel("课程号");
JTextField sesnotext = new JTextField();
JTextField secnotext = new JTextField();
JButton ok = new JButton("确定");
JButton reset = new JButton("重置");
addse.setLayout(null);
sesno.setBounds(5,5,70,20);
sesnotext.setBounds(80,5,120,20);
secno.setBounds(5,30,70,20);
secnotext.setBounds(80,30,120,20);
ok.setBounds(50,70,60,20);
reset.setBounds(130,70,60,20);
addse.add(sesno);
addse.add(sesnotext);
addse.add(secno);
addse.add(secnotext);
addse.add(ok);
addse.add(reset);
Addse.add(addse);
Addse.setVisible(true);
Addse.addWindowListener(new WindowAdapter(){
@Override
public void windowClosing(WindowEvent e) {
SystemUI.clickable();
}
});
ok.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent arg0) {
try {
String sno = sesnotext.getText();
String cno = secnotext.getText();
//加载驱动
Class.forName("com.mysql.jdbc.Driver");
//得到连接
ct=DriverManager.getConnection("jdbc:mysql://localhost:3306/jiaoxueguanlixit?user="
+userMySql+"&password="+passwordMySql + "&useUnicode=true&characterEncoding=gbk&useSSL=false&serverTimezone=GMT");
ps=ct.prepareStatement("insert into selectcourse values('"+sno+"',(select studentname from student where studentid='"+sno+"'),'"+cno+"',(select coursename from course where courseid='"+cno+"'))");
ps.executeUpdate();
JOptionPane.showMessageDialog(null, "添加成功!");
Addse.dispose();
SystemUI.clickable();
refreshSelCou();
} catch (Exception e) {
e.printStackTrace();
JOptionPane.showMessageDialog(null, "添加失败!");
SystemUI.unclickable();
} finally{
try {
if(rs!=null){
rs.close();
}
if(ps!=null){
ps.close();
}
if(ct!=null){
ct.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
});
reset.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent arg0) {
sesnotext.setText("");
secnotext.setText("");
}
});
}
public void deleteSelCou() {
// TODO 自动生成的方法存根
int row = table5.getSelectedRow();
if ( row >= 0 ) {
String string[] = new String[4];
string[0] = String.valueOf(table5.getValueAt(row, 0));
string[1] = String.valueOf(table5.getValueAt(row, 1));
string[2] = String.valueOf(table5.getValueAt(row, 2));
string[3] = String.valueOf(table5.getValueAt(row, 3));
try {
//加载驱动
Class.forName("com.mysql.jdbc.Driver");
//得到连接
ct=DriverManager.getConnection("jdbc:mysql://localhost:3306/jiaoxueguanlixit?user="
+userMySql+"&password="+passwordMySql + "&useUnicode=true&characterEncoding=gbk&useSSL=false&serverTimezone=GMT");
ps=ct.prepareStatement("delete from selectcourse where studentid='"+string[0]+"' and studentname='"+string[1]+"' and courseid='"+string[2]+"' and coursename='"+string[3]+"'");
ps.executeUpdate();
JOptionPane.showMessageDialog(null, "删除成功!");
refreshSelCou();
row=-1;
} catch (Exception e) {
e.printStackTrace();
JOptionPane.showMessageDialog(null, "删除失败!");
} finally{
try {
if(rs!=null){
rs.close();
}
if(ps!=null){
ps.close();
}
if(ct!=null){
ct.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
else {
JOptionPane.showMessageDialog(null, "请选中要删除的行!");
}
}
public void searchSelCou() {
// TODO 自动生成的方法存根
SystemUI.unclickable();
JFrame SeaSelCou = new JFrame("查找课程信息");
SeaSelCou.setSize(250, 150);
SeaSelCou.setLocation(600, 300);
JPanel seaselcou = new JPanel();
JLabel cno = new JLabel("请输入课程号");
JTextField cnotext = new JTextField();
JLabel sno = new JLabel("请输入学号");
JTextField snotext = new JTextField();
JButton ok = new JButton("确定");
JButton reset = new JButton("重置");
seaselcou.setLayout(null);
cno.setBounds(5,5,80,20);
cnotext.setBounds(90,5,120,20);
sno.setBounds(5,30,80,20);
snotext.setBounds(90,30,120,20);
ok.setBounds(50,70,60,20);
reset.setBounds(130,70,60,20);
seaselcou.add(cno);
seaselcou.add(cnotext);
seaselcou.add(sno);
seaselcou.add(snotext);
seaselcou.add(ok);
seaselcou.add(reset);
SeaSelCou.add(seaselcou);
SeaSelCou.setVisible(true);
SeaSelCou.addWindowListener(new WindowAdapter(){
@Override
public void windowClosing(WindowEvent e) {
SystemUI.clickable();
}
});
ok.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent arg0) {
columnNames=new Vector<Object>();
//设置列名
columnNames.add("学号");
columnNames.add("姓名");
columnNames.add("课程号");
columnNames.add("课程名");
String str = cnotext.getText();
String str1 = snotext.getText();
// rowData = new Vector<Object>();
int count=0;
//rowData可以存放多行,开始从数据库里取
try {
//加载驱动
Class.forName("com.mysql.jdbc.Driver");
//得到连接
ct=DriverManager.getConnection("jdbc:mysql://localhost:3306/jiaoxueguanlixit","zhangsan","12345678");
if(snotext.getText().trim().equals("")&&!cnotext.getText().trim().equals("")){
ps=ct.prepareStatement("select * from selectcourse where courseid='"+str+"'");
rs=ps.executeQuery();
}
if(cnotext.getText().trim().equals("")&&!snotext.getText().trim().equals("")){
ps=ct.prepareStatement("select * from selectcourse where studentid='"+str1+"'");
rs=ps.executeQuery();
}
if(!cnotext.getText().trim().equals("")&&!snotext.getText().trim().equals("")){
ps=ct.prepareStatement("select * from selectcourse where studentid='"+str1+"' and courseid='"+str+"'");
rs=ps.executeQuery();
}
if(cnotext.getText().trim().equals("")&&snotext.getText().trim().equals("")){
ps=ct.prepareStatement("select * from selectcourse");
rs=ps.executeQuery();
JOptionPane.showMessageDialog(null, "请输入查询信息!");
}
while(rs.next()){
//rowData可以存放多行
Vector<Object> hang=new Vector<Object>();
hang.add(rs.getString(1));
hang.add(rs.getString(2));
hang.add(rs.getString(3));
hang.add(rs.getString(4));
//加入到rowData
rowData.add(hang);
count++;
}
} catch (Exception e) {
e.printStackTrace();
JOptionPane.showMessageDialog(null, "查询失败!");
} finally{
try {
if(rs!=null){
rs.close();
}
if(ps!=null){
ps.close();
}
if(ct!=null){
ct.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
if(count!=0) {
table5 = new JTable(rowData,columnNames){
private static final long serialVersionUID = -3692229347549819226L;
public boolean isCellEditable(int row, int column)
{return false;}//表格不允许被编辑
};
table5.getSelectionModel().setSelectionMode(ListSelectionModel.SINGLE_SELECTION);//只允许选中一行
DefaultTableCellRenderer tcr = new DefaultTableCellRenderer();// 设置table内容居中
tcr.setHorizontalAlignment(JLabel.CENTER);// 这句和上句作用一样
table5.setDefaultRenderer(Object.class, tcr);
SystemUI.scrollPane5.setViewportView(table5);
SeaSelCou.dispose();
SystemUI.clickable();}
if(count==0) {
JOptionPane.showMessageDialog(null, "没有查询到相关信息!");
}
}
});
reset.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent arg0) {
cnotext.setText("");
snotext.setText("");
}
});
}
public void refreshSelCou() {
// TODO 自动生成的方法存根
new SelectCourse();
SystemUI.scrollPane5.setViewportView(SelectCourse.table5);
}
}
成绩信息和老师信息也差不多,不过加了一个创新点就是可以对输入学生的总分成绩进行排名,代码如下:
package 教学管理系统;
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.DecimalFormat;
import java.util.Vector;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JInternalFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.JToolBar;
import javax.swing.ListSelectionModel;
import javax.swing.event.InternalFrameAdapter;
import javax.swing.event.InternalFrameEvent;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;
public class Score {
String userMySql="zhangsan";
String passwordMySql="12345678";
private Vector<Vector<Object>> rowData = new Vector<Vector<Object>>();
//Vector<Object> rowData;
Vector<Object> columnNames;
//定义数据库需要的全局变量
PreparedStatement ps=null;
Connection ct=null;
ResultSet rs=null;
static JTable table4=null;
static JTable table6=null;
static JTable table7=null;
public Score(){
columnNames=new Vector<Object>();
//设置列名
columnNames.add("学号");
columnNames.add("姓名");
columnNames.add("课程号");
columnNames.add("课程名");
columnNames.add("成绩");
//rowData = new Vector<Object>();
//rowData可以存放多行,开始从数据库里取
try {
//加载驱动
Class.forName("com.mysql.jdbc.Driver");
//得到连接
ct=DriverManager.getConnection("jdbc:mysql://localhost:3306/jiaoxueguanlixit?user="
+userMySql+"&password="+passwordMySql + "&useUnicode=true&characterEncoding=gbk&useSSL=false&serverTimezone=GMT");
ps=ct.prepareStatement("select * from score");
rs=ps.executeQuery();
while(rs.next()){
//rowData可以存放多行
Vector<Object> hang=new Vector<Object>();
hang.add(rs.getString(1));
hang.add(rs.getString(2));
hang.add(rs.getString(3));
hang.add(rs.getString(4));
hang.add(rs.getInt(5));
//加入到rowData
rowData.add(hang);
}
} catch (Exception e) {
e.printStackTrace();
} finally{
try {
if(rs!=null){
rs.close();
}
if(ps!=null){
ps.close();
}
if(ct!=null){
ct.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
table4 = new JTable(rowData,columnNames){
private static final long serialVersionUID = 1525421110274312344L;
public boolean isCellEditable(int row, int column)
{return false;}//表格不允许被编辑
};
table4.getSelectionModel().setSelectionMode(ListSelectionModel.SINGLE_SELECTION);//只允许选中一行
DefaultTableCellRenderer tcr = new DefaultTableCellRenderer();// 设置table内容居中
tcr.setHorizontalAlignment(JLabel.CENTER);// 这句和上句作用一样
table4.setDefaultRenderer(Object.class, tcr);
}
public void addScore() {
// TODO 自动生成的方法存根
SystemUI.unclickable();
JFrame Addsc = new JFrame("添加成绩信息");
Addsc.setSize(250, 180);
Addsc.setLocation(600, 300);
JPanel addsc = new JPanel();
JLabel scsno = new JLabel("学号");
JLabel sccno = new JLabel("课程号");
JLabel score = new JLabel("成绩");
JTextField scsnotext = new JTextField();
JTextField sccnotext = new JTextField();
JTextField scoretext = new JTextField();
JButton ok = new JButton("确定");
JButton reset = new JButton("重置");
addsc.setLayout(null);
scsno.setBounds(5,5,70,20);
scsnotext.setBounds(80,5,120,20);
sccno.setBounds(5,30,70,20);
sccnotext.setBounds(80,30,120,20);
score.setBounds(5,60,70,20);
scoretext.setBounds(80,60,120,20);
ok.setBounds(50,100,60,20);
reset.setBounds(130,100,60,20);
addsc.add(scsno);
addsc.add(scsnotext);
addsc.add(sccno);
addsc.add(sccnotext);
addsc.add(score);
addsc.add(scoretext);
addsc.add(ok);
addsc.add(reset);
Addsc.add(addsc);
Addsc.setVisible(true);
Addsc.addWindowListener(new WindowAdapter(){
@Override
public void windowClosing(WindowEvent e) {
SystemUI.clickable();
}
});
ok.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent arg0) {
try {
String sno = scsnotext.getText();
String cno = sccnotext.getText();
int score = Integer.valueOf(scoretext.getText()).intValue();
int count=0;
//加载驱动
Class.forName("com.mysql.jdbc.Driver");
//得到连接
ct=DriverManager.getConnection("jdbc:mysql://localhost:3306/jiaoxueguanlixit?user="
+userMySql+"&password="+passwordMySql + "&useUnicode=true&characterEncoding=gbk&useSSL=false&serverTimezone=GMT");
ps=ct.prepareStatement("select studentname from selectcourse where studentid='"+sno+"' and courseid='"+cno+"'");
rs=ps.executeQuery();
while(rs.next()){
count++;
}
if(count==0) {
JOptionPane.showMessageDialog(null, "此人没有选此课程!");
}
if(count!=0) {
ps=ct.prepareStatement("insert into score values('"+sno+"',(select studentname from selectcourse where studentid='"+sno+"' and courseid='"+cno+"'),'"+cno+"',(select coursename from selectcourse where courseid='"+cno+"' and studentid='"+sno+"'),"+score+")");
ps.executeUpdate();
JOptionPane.showMessageDialog(null, "添加成功!");
Addsc.dispose();
SystemUI.clickable();
refreshScore();
}
} catch (Exception e) {
e.printStackTrace();
JOptionPane.showMessageDialog(null, "添加失败!");
SystemUI.unclickable();
} finally{
try {
if(rs!=null){
rs.close();
}
if(ps!=null){
ps.close();
}
if(ct!=null){
ct.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
});
reset.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent arg0) {
scsnotext.setText("");
sccnotext.setText("");
scoretext.setText("");
}
});
}
public void updateScore() {
// TODO 自动生成的方法存根
SystemUI.unclickable();
JFrame UpdateSC = new JFrame("修改成绩信息");
UpdateSC.setSize(250, 240);
UpdateSC.setLocation(600, 300);
JPanel updatesc = new JPanel();
JLabel yscsno = new JLabel("要修改的学号");
JLabel scsno = new JLabel("要改成的学号");
JLabel ysccno = new JLabel("要修改的课程号");
JLabel sccno = new JLabel("要改成的课程号");
JLabel score = new JLabel("成绩");
JTextField yscsnotext = new JTextField();
JTextField scsnotext = new JTextField();
JTextField ysccnotext = new JTextField();
JTextField sccnotext = new JTextField();
JTextField scoretext = new JTextField();
JButton ok = new JButton("确定");
JButton reset = new JButton("重置");
updatesc.setLayout(null);
yscsno.setBounds(5,5,100,20);
yscsnotext.setBounds(110,5,120,20);
scsno.setBounds(5,30,100,20);
scsnotext.setBounds(110,30,120,20);
ysccno.setBounds(5,60,100,20);
ysccnotext.setBounds(110,60,120,20);
sccno.setBounds(5,90,100,20);
sccnotext.setBounds(110,90,120,20);
score.setBounds(5,120,100,20);
scoretext.setBounds(110,120,120,20);
ok.setBounds(50,160,60,20);
reset.setBounds(120,160,60,20);
updatesc.add(yscsno);
updatesc.add(yscsnotext);
updatesc.add(scsno);
updatesc.add(scsnotext);
updatesc.add(ysccno);
updatesc.add(ysccnotext);
updatesc.add(sccno);
updatesc.add(sccnotext);
updatesc.add(score);
updatesc.add(scoretext);
updatesc.add(ok);
updatesc.add(reset);
UpdateSC.add(updatesc);
UpdateSC.setVisible(true);
UpdateSC.addWindowListener(new WindowAdapter(){
@Override
public void windowClosing(WindowEvent e) {
SystemUI.clickable();
}
});
ok.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent arg0) {
try {
String ysno=yscsnotext.getText();
String ycno=ysccnotext.getText();
String sno = scsnotext.getText();
String cno = sccnotext.getText();
int score = Integer.valueOf(scoretext.getText()).intValue();
int count=0;
//加载驱动
Class.forName("com.mysql.jdbc.Driver");
//得到连接
ct=DriverManager.getConnection("jdbc:mysql://localhost:3306/jiaoxueguanlixit","zhangsan","12345678");
ps=ct.prepareStatement("select studentname from selectcourse where studentid='"+sno+"' and courseid='"+cno+"'");
rs=ps.executeQuery();
while(rs.next()){
count++;
}
if(count==0) {
JOptionPane.showMessageDialog(null, "此人没有选此课程!");
}
if(count!=0) {
ps=ct.prepareStatement("update score set studentid ='"+sno+"', courseid ='"+cno+"',studentname=(select studentname from selectcourse where studentid='"+sno+"'and courseid ='"+cno+"'), coursename =(select coursename from selectcourse where courseid='"+cno+"'and studentid='"+sno+"'), score ="+score+" where studentid='"+ysno+"'and courseid='"+ycno+"'");
ps.executeUpdate();
JOptionPane.showMessageDialog(null, "修改成功!");
UpdateSC.dispose();
SystemUI.clickable();
refreshScore();
}
} catch (Exception e) {
e.printStackTrace();
JOptionPane.showMessageDialog(null, "修改失败!");
SystemUI.unclickable();
} finally{
try {
if(rs!=null){
rs.close();
}
if(ps!=null){
ps.close();
}
if(ct!=null){
ct.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
});
reset.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent arg0) {
scsnotext.setText("");
sccnotext.setText("");
scoretext.setText("");
}
});
}
public void deleteScore() {
// TODO 自动生成的方法存根
int row = table4.getSelectedRow();
if ( row >= 0 ) {
String string[] = new String[4];
string[0] = (String) table4.getValueAt(row, 0);
string[1] = (String) table4.getValueAt(row, 1);
string[2] = (String) table4.getValueAt(row, 2);
string[3] = (String) table4.getValueAt(row, 3);
try {
//加载驱动
Class.forName("com.mysql.jdbc.Driver");
//得到连接
ct=DriverManager.getConnection("jdbc:mysql://localhost:3306/jiaoxueguanlixit?user="
+userMySql+"&password="+passwordMySql + "&useUnicode=true&characterEncoding=gbk&useSSL=false&serverTimezone=GMT");
ps=ct.prepareStatement("delete from score where studentid='"+string[0]+"' and studentname='"+string[1]+"' and courseid='"+string[2]+"' and coursename='"+string[3]+"'");
ps.executeUpdate();
JOptionPane.showMessageDialog(null, "删除成功!");
refreshScore();
row=-1;
} catch (Exception e) {
e.printStackTrace();
JOptionPane.showMessageDialog(null, "删除失败!");
} finally{
try {
if(rs!=null){
rs.close();
}
if(ps!=null){
ps.close();
}
if(ct!=null){
ct.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
else {
JOptionPane.showMessageDialog(null, "请选中要删除的行!");
}
}
public void searchScore() {
// TODO 自动生成的方法存根
SystemUI.unclickable();
JFrame SeaSC = new JFrame("查找成绩信息");
SeaSC.setSize(250, 150);
SeaSC.setLocation(600, 300);
JPanel seasc = new JPanel();
JLabel cno = new JLabel("请输入课程号");
JTextField cnotext = new JTextField();
JLabel sno = new JLabel("请输入学号");
JTextField snotext = new JTextField();
JButton ok = new JButton("确定");
JButton reset = new JButton("重置");
seasc.setLayout(null);
cno.setBounds(5,5,80,20);
cnotext.setBounds(90,5,120,20);
sno.setBounds(5,30,80,20);
snotext.setBounds(90,30,120,20);
ok.setBounds(50,70,60,20);
reset.setBounds(130,70,60,20);
seasc.add(cno);
seasc.add(cnotext);
seasc.add(sno);
seasc.add(snotext);
seasc.add(ok);
seasc.add(reset);
SeaSC.add(seasc);
SeaSC.setVisible(true);
SeaSC.addWindowListener(new WindowAdapter(){
@Override
public void windowClosing(WindowEvent e) {
SystemUI.clickable();
}
});
ok.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent arg0) {
columnNames=new Vector<Object>();
//设置列名
columnNames.add("学号");
columnNames.add("姓名");
columnNames.add("课程号");
columnNames.add("课程名");
columnNames.add("成绩");
String str = cnotext.getText();
String str1 = snotext.getText();
//rowData = new Vector<Object>();
int count=0;
//rowData可以存放多行,开始从数据库里取
try {
//加载驱动
Class.forName("com.mysql.jdbc.Driver");
//得到连接
ct=DriverManager.getConnection("jdbc:mysql://localhost:3306/jiaoxueguanlixit","zhangsan","12345678");
if(snotext.getText().trim().equals("")&&!cnotext.getText().trim().equals("")){
ps=ct.prepareStatement("select * from score where courseid='"+str+"'");
rs=ps.executeQuery();
}
if(cnotext.getText().trim().equals("")&&!snotext.getText().trim().equals("")){
ps=ct.prepareStatement("select * from score where studentid='"+str1+"'");
rs=ps.executeQuery();
}
if(!cnotext.getText().trim().equals("")&&!snotext.getText().trim().equals("")){
ps=ct.prepareStatement("select * from score where studentid='"+str1+"' and courseid='"+str+"'");
rs=ps.executeQuery();
}
if(cnotext.getText().trim().equals("")&&snotext.getText().trim().equals("")){
ps=ct.prepareStatement("select * from score order by score desc ");
rs=ps.executeQuery();
JOptionPane.showMessageDialog(null, "请输入查询信息!");
}
while(rs.next()){
//rowData可以存放多行
Vector<Object> hang=new Vector<Object>();
hang.add(rs.getString(1));
hang.add(rs.getString(2));
hang.add(rs.getString(3));
hang.add(rs.getString(4));
hang.add(rs.getInt(5));
//加入到rowData
rowData.add(hang);
count++;
}
} catch (Exception e) {
e.printStackTrace();
JOptionPane.showMessageDialog(null, "查询失败!");
} finally{
try {
if(rs!=null){
rs.close();
}
if(ps!=null){
ps.close();
}
if(ct!=null){
ct.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
if(count!=0) {
table4 = new JTable(rowData,columnNames){
private static final long serialVersionUID = -518524586936803727L;
public boolean isCellEditable(int row, int column)
{return false;}//表格不允许被编辑
};
table4.getSelectionModel().setSelectionMode(ListSelectionModel.SINGLE_SELECTION);//只允许选中一行
DefaultTableCellRenderer tcr = new DefaultTableCellRenderer();// 设置table内容居中
tcr.setHorizontalAlignment(JLabel.CENTER);// 这句和上句作用一样
table4.setDefaultRenderer(Object.class, tcr);
SystemUI.scrollPane4.setViewportView(table4);
SeaSC.dispose();
SystemUI.clickable();}
if(count==0) {
JOptionPane.showMessageDialog(null, "没有查询到相关消息!");
}
}
});
reset.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent arg0) {
cnotext.setText("");
snotext.setText("");
}
});
}
public void refreshScore() {
// TODO 自动生成的方法存根
new Score();
SystemUI.scrollPane4.setViewportView(Score.table4);
}
public void totalrank() {
// TODO 自动生成的方法存根
JFrame jf = new JFrame("总分排名");
jf.setVisible(true);
jf.setSize(250, 300);
jf.setLocation(500,300);
JScrollPane js=new JScrollPane();
jf.add(js,BorderLayout.CENTER);
columnNames=new Vector<Object>();
//设置列名
columnNames.add("名次");
columnNames.add("学号");
columnNames.add("总分");
//rowData = new Vector<Object>();
try {
//加载驱动
Class.forName("com.mysql.jdbc.Driver");
//得到连接
ct=DriverManager.getConnection("jdbc:mysql://localhost:3306/jiaoxueguanlixit?user="
+userMySql+"&password="+passwordMySql + "&useUnicode=true&characterEncoding=gbk&useSSL=false&serverTimezone=GMT");
ps=ct.prepareStatement("select studentid,sum(score) sum_grade from score group by score.studentid having count(courseid)>=1 order by sum_grade desc");
rs=ps.executeQuery();
int rank=0;
while(rs.next()){
//rowData可以存放多行
rank++;
Vector<Object> hang=new Vector<Object>();
hang.add(rank);
hang.add(rs.getString(1));
hang.add(rs.getString(2));
//加入到rowData
rowData.add(hang);
}
} catch (Exception e) {
e.printStackTrace();
} finally{
try {
if(rs!=null){
rs.close();
}
if(ps!=null){
ps.close();
}
if(ct!=null){
ct.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
table7 = new JTable(rowData,columnNames){
private static final long serialVersionUID = 3701216243676371199L;
public boolean isCellEditable(int row, int column)
{return false;}//表格不允许被编辑
};
table7.getSelectionModel().setSelectionMode(ListSelectionModel.SINGLE_SELECTION);//只允许选中一行
DefaultTableCellRenderer tcr = new DefaultTableCellRenderer();// 设置table内容居中
tcr.setHorizontalAlignment(JLabel.CENTER);// 这句和上句作用一样
table7.setDefaultRenderer(Object.class, tcr);
js.setViewportView(table7);
}
}
最后一个systemUI系统将各个信息表进行连接,使其具有先后顺序:
package 教学管理系统;
import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JToolBar;
import java.awt.Toolkit;
import javax.swing.JTabbedPane;
import javax.swing.JScrollPane;
public class SystemUI extends JFrame{
private static final long serialVersionUID = 4197017698513729527L;
Teacher t = new Teacher();
Course c = new Course();
Student st = new Student();
Score sc = new Score();
SelectCourse se = new SelectCourse();
static JTabbedPane tabbedPane;
static JToolBar toolBar_1;
static JToolBar toolBar_2;
static JToolBar toolBar_3;
static JToolBar toolBar_4;
static JToolBar toolBar_5;
static JButton AddTeacher;
static JButton UpdateTeacher;
static JButton DeleteTeacher;
static JButton SearchTeacher;
static JButton RefreshTeacher;
static JButton AddStudent;
static JButton UpdateStudent;
static JButton DeleteStudent;
static JButton SearchStudent;
static JButton RefreshStudent;
static JButton AddCourse;
static JButton UpdateCourse;
static JButton DeleteCourse;
static JButton SearchCourse;
static JButton RefreshCourse;
static JButton AddSelCou;
static JButton DeleteSelCou;
static JButton SearchSelCou;
static JButton RefreshSelCou;
static JButton AddScore;
static JButton UpdateScore;
static JButton DeleteScore;
static JButton SearchScore;
static JButton RefreshScore;
static JButton Statistical;
static JButton TotalRank;
static JScrollPane scrollPane1;
static JScrollPane scrollPane2;
static JScrollPane scrollPane3;
static JScrollPane scrollPane4;
static JScrollPane scrollPane5;
public SystemUI()
{
setIconImage(Toolkit.getDefaultToolkit().getImage(".jpg"));
this.setResizable(false);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setSize(565, 500);
this.setLocation(550, 0);
this.setVisible(true);
this.setTitle("欢迎来到教务系统");
getContentPane().setLayout(new BorderLayout(0, 0));
tabbedPane = new JTabbedPane(JTabbedPane.TOP);
getContentPane().add(tabbedPane, BorderLayout.CENTER);
JPanel TeacherInfo = new JPanel();
JPanel StudentInfo = new JPanel();
JPanel CourseInfo = new JPanel();
JPanel ScoreInfo = new JPanel();
JPanel SelectCourseInfo = new JPanel();
tabbedPane.add(TeacherInfo,"教师信息");
TeacherInfo.setLayout(new BorderLayout(0, 0));
toolBar_1 = new JToolBar();
TeacherInfo.add(toolBar_1, BorderLayout.NORTH);
AddTeacher = new JButton("添加教师信息");
AddTeacher.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
t.addTeacher();
}
});
toolBar_1.add(AddTeacher);
UpdateTeacher = new JButton("修改教师信息");
UpdateTeacher.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
t.updateTeacher();
}
});
toolBar_1.add(UpdateTeacher);
DeleteTeacher = new JButton("删除教师信息");
DeleteTeacher.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
t.deleteTeacher();
}
});
toolBar_1.add(DeleteTeacher);
SearchTeacher = new JButton("搜索教师信息");
SearchTeacher.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
t.searchTeacher();
}
});
toolBar_1.add(SearchTeacher);
RefreshTeacher = new JButton("刷新");
RefreshTeacher.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
t.refreshTeacher();
}
});
toolBar_1.add(RefreshTeacher);
scrollPane1 = new JScrollPane();
TeacherInfo.add(scrollPane1, BorderLayout.CENTER);
scrollPane1.setViewportView(Teacher.table1);
tabbedPane.add(StudentInfo,"学生信息");
StudentInfo.setLayout(new BorderLayout(0, 0));
toolBar_2 = new JToolBar();
StudentInfo.add(toolBar_2, BorderLayout.NORTH);
AddStudent = new JButton("添加学生信息");
AddStudent.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
st.addStudent();
}
});
toolBar_2.add(AddStudent);
UpdateStudent = new JButton("修改学生信息");
UpdateStudent.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
st.updateStudent();
}
});
toolBar_2.add(UpdateStudent);
DeleteStudent = new JButton("删除学生信息");
DeleteStudent.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
st.deleteStudent();
}
});
toolBar_2.add(DeleteStudent);
SearchStudent = new JButton("搜索学生信息");
SearchStudent.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
st.searchStudent();
}
});
toolBar_2.add(SearchStudent);
RefreshStudent = new JButton("刷新");
RefreshStudent.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
st.refreshStudent();
}
});
toolBar_2.add(RefreshStudent);
scrollPane2 = new JScrollPane();
StudentInfo.add(scrollPane2, BorderLayout.CENTER);
scrollPane2.setViewportView(Student.table2);
tabbedPane.add(CourseInfo,"课程信息");
CourseInfo.setLayout(new BorderLayout(0, 0));
toolBar_3 = new JToolBar();
CourseInfo.add(toolBar_3, BorderLayout.NORTH);
AddCourse = new JButton("添加课程信息");
AddCourse.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
c.addCourse();
}
});
toolBar_3.add(AddCourse);
UpdateCourse = new JButton("修改课程信息");
UpdateCourse.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
c.updateCourse();
}
});
toolBar_3.add(UpdateCourse);
DeleteCourse = new JButton("删除课程信息");
DeleteCourse.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
c.deleteCourse();
}
});
toolBar_3.add(DeleteCourse);
SearchCourse = new JButton("搜索课程信息");
SearchCourse.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
c.searchCourse();
}
});
toolBar_3.add(SearchCourse);
RefreshCourse = new JButton("刷新");
RefreshCourse.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
c.refreshCourse();
}
});
toolBar_3.add(RefreshCourse);
scrollPane3 = new JScrollPane();
CourseInfo.add(scrollPane3, BorderLayout.CENTER);
scrollPane3.setViewportView(Course.table3);
tabbedPane.add(SelectCourseInfo,"选课管理");
SelectCourseInfo.setLayout(new BorderLayout(0, 0));
toolBar_5 = new JToolBar();
SelectCourseInfo.add(toolBar_5, BorderLayout.NORTH);
AddSelCou = new JButton("添加选课信息");
AddSelCou.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
se.addSelCou();
}
});
toolBar_5.add(AddSelCou);
DeleteSelCou = new JButton("删除选课信息");
DeleteSelCou.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
se.deleteSelCou();
}
});
toolBar_5.add(DeleteSelCou);
SearchSelCou = new JButton("搜索选课信息");
SearchSelCou.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
se.searchSelCou();
}
});
toolBar_5.add(SearchSelCou);
RefreshSelCou = new JButton("刷新");
RefreshSelCou.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
se.refreshSelCou();
}
});
toolBar_5.add(RefreshSelCou);
scrollPane5 = new JScrollPane();
SelectCourseInfo.add(scrollPane5, BorderLayout.CENTER);
scrollPane5.setViewportView(SelectCourse.table5);
tabbedPane.add(ScoreInfo,"成绩管理");
ScoreInfo.setLayout(new BorderLayout(0, 0));
toolBar_4 = new JToolBar();
ScoreInfo.add(toolBar_4, BorderLayout.NORTH);
AddScore = new JButton("添加成绩信息");
AddScore.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
sc.addScore();
}
});
toolBar_4.add(AddScore);
UpdateScore = new JButton("修改成绩信息");
UpdateScore.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
sc.updateScore();
}
});
toolBar_4.add(UpdateScore);
DeleteScore = new JButton("删除成绩信息");
DeleteScore.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
sc.deleteScore();
}
});
toolBar_4.add(DeleteScore);
SearchScore = new JButton("搜索成绩信息");
SearchScore.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
sc.searchScore();
};
});
toolBar_4.add(SearchScore);
Statistical = new JButton("统计分析");
Statistical.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
//sc.statistical();
}
});
toolBar_4.add(SearchScore);
TotalRank = new JButton("总分排名");
TotalRank.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
sc.totalrank();
}
});
toolBar_4.add(TotalRank);
RefreshScore = new JButton("刷新");
RefreshScore.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
sc.refreshScore();
}
});
toolBar_4.add(RefreshScore);
scrollPane4 = new JScrollPane();
ScoreInfo.add(scrollPane4, BorderLayout.CENTER);
scrollPane4.setViewportView(Score.table4);
}
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
SystemUI frame = new SystemUI();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public static void clickable() {
tabbedPane.setEnabled(true);
AddTeacher.setEnabled(true);
DeleteTeacher.setEnabled(true);
RefreshTeacher.setEnabled(true);
SearchTeacher.setEnabled(true);
UpdateTeacher.setEnabled(true);
AddStudent.setEnabled(true);
DeleteStudent.setEnabled(true);
RefreshStudent.setEnabled(true);
SearchStudent.setEnabled(true);
UpdateStudent.setEnabled(true);
AddCourse.setEnabled(true);
DeleteCourse.setEnabled(true);
RefreshCourse.setEnabled(true);
SearchCourse.setEnabled(true);
UpdateCourse.setEnabled(true);
AddScore.setEnabled(true);
DeleteScore.setEnabled(true);
RefreshScore.setEnabled(true);
SearchScore.setEnabled(true);
UpdateScore.setEnabled(true);
Statistical.setEnabled(true);
AddSelCou.setEnabled(true);
DeleteSelCou.setEnabled(true);
SearchSelCou.setEnabled(true);
RefreshSelCou.setEnabled(true);
TotalRank.setEnabled(true);
}
public static void unclickable() {
tabbedPane.setEnabled(false);
AddTeacher.setEnabled(false);
DeleteTeacher.setEnabled(false);
RefreshTeacher.setEnabled(false);
SearchTeacher.setEnabled(false);
UpdateTeacher.setEnabled(false);
AddStudent.setEnabled(false);
DeleteStudent.setEnabled(false);
RefreshStudent.setEnabled(false);
SearchStudent.setEnabled(false);
UpdateStudent.setEnabled(false);
AddCourse.setEnabled(false);
DeleteCourse.setEnabled(false);
RefreshCourse.setEnabled(false);
SearchCourse.setEnabled(false);
UpdateCourse.setEnabled(false);
AddScore.setEnabled(false);
DeleteScore.setEnabled(false);
RefreshScore.setEnabled(false);
SearchScore.setEnabled(false);
UpdateScore.setEnabled(false);
Statistical.setEnabled(false);
AddSelCou.setEnabled(false);
DeleteSelCou.setEnabled(false);
SearchSelCou.setEnabled(false);
RefreshSelCou.setEnabled(false);
TotalRank.setEnabled(false);
}
}
最后效果图:
心得与体验:
1:权限还有待完善,此系统只针对管理人员对数据进行添加,修改,删除和查找操作,对于学生或者教师没有设置权限进行数据处理;学生用户只能看到与学生有关的信息而不能更改教师信息,老师用户也不可随意更改学生信息。
以下是对课设中遇到的错误的分析:
2:不同版本的连接与驱动问题:我所用到的版本是MySQL5.5,进行驱动时使用语句
Class.forName(“com.mysql.jdbc.Driver”);
如果MySQL版本超过了6.0,考虑用以下语句进行驱动 Class.forName(“com.mysql.cj.jdbc.Driver”);
连接时采用语句以下语句可能会造成连接失败的问题: ct=DriverManager.getConnection(“jdbc:mysql://localhost:3306 /jiaoxueguan lixit”,“zhangsan”,“12345678”);
将其改为: ct=DriverManager.getConnection(“jdbc:mysql://localhost:3306 /jiaoxueguanlixit?user=”+userMySql+"&password="+passwordMySql+"&useUnicode=true&characterEncoding=gbk&useSSL=false&serverTimezone=GMT");
3:中文乱码问题:MySQL出现的中文乱码问题以及eclipse出现的中文乱码问题;在MySQL中使用insert语句进行插入,并用select语句进行查询,发现中文插入后查询时显示??或者乱码文字,解决方法如下:
在MySQL页面插入以下语句来更改MySQL的编码格式:
Alter table +表名+convert to character set utf8mb4;
在eclipse语句中将连接语句中加入后缀characterEncoding=UTF-8将 eclipse编码格式转化为utf8。
4:MySQL建立的数据表太多,从而造成数据冗杂,不易分析清楚,利用了Navicat Premium软件,直接观察到MySQL的后台数据,提高效率。
5:界面与界面的连接问题:利用java语言创造的GUI图形界面,连接起来是一个问题,我单独创造了一个类SystemUI,利用toolBar_4.add(方法名);方法将各个界面或者各个按钮连接起来,如
toolBar_4.add(AddScore);
UpdateScore = new JButton(“修改成绩信息”);
UpdateScore.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
sc.updateScore();
}
});
toolBar_4.add(UpdateScore);
DeleteScore = new JButton("删除成绩信息");
DeleteScore.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
sc.deleteScore();
}});
6:布局管理器,监听动作,常用组件的匹配关系问题:对于一个教学管理系统,肯定会用到很多的组件,面板和监听动作,编写语句时遇到的编译问题,组件插入面板的插入问题,窗口添加面板的添加问题,都是一个个应该细心的挑战。
结束语:这篇文章是参考一篇博客:作者祭零小白,文章名:数据库小型教务系统(成绩管理系统),不懂的可以参考原作者文章。
解决java和mysql的连接问题建议参考这篇文章:https://blog.csdn.net/m0_46315342/article/details/105738470
好啦,制作不易,喜欢的希望点一个赞,希望这篇文章能有帮助。