学生选课系统
这个小项目是课程设计的题目,当时课程设计是用Python写的,但是有瑕疵不满意,而且界面很简陋,说白了是为了应付差事,其实当时已经用Java写了一些了,但是代码量有点多,主要是快放假了懒得敲的,后来不得已放弃了。暑假回去实在无聊(本来要去打工,因为某些原因就回家了)白天给我父帮忙干活 晚上坐在电脑前喝着啤酒🍺敲着代码,简直了#哈哈。一共敲了五个晚上; 最终只完成了一半,是个半成品,但是后面基本都一样,框架已经搭好,可以根据前面思路继续做下去。
时隔三个多月想着写篇博客叭,分享一下。话不多说了,正题吧。
题目要求
学生选课管理是高等教学管理系统中很重要的功能之一。选课系统主要满足三类用户的要求,这三类用户分别是教务处的系统管理员、教师和学生,他们所具有的操作权限以及操作内容是不同的。具体要求如下:
系统管理员:实现对学生、教师、课程、授课信息的增加、删除、查询、修改等。 学生的信息包括学号,姓名,性别,出生日期,专业,电话,EMAIL地址等。教师的信息包括教师号,姓名,性别,职称,电话,EMAIL地址等。课程信息包括课程号,课程名,学分等。
学生:只能查看和修改自己个人信息、已选课程成绩及总学分;修改自己密码;可以查看课程信息、授课及教师信息;可以进行选课操作。
教师:只能查看和修改自己个人信息;修改自己的密码;只能录入自己所授课程的学生成绩,一旦提交将不能修改,否则必须通过管理员授权才可修改学生成绩。能够输出所授课程的成绩单。
思路
设计四个主要的大页面:
- 登录页面
- 学生页面
- 教师页面
- 管理员页面
登录页面展示:
没错,本人西科的,
(二维码可扫 但是涉及鄙人个人信息 此处不可扫)
登录页面设置三个JRadioButton 分别为学生、教师、管理员供选择,给各自的JRadioButton添加监听再在JRadioButton 的监听里面添加登录按钮的监听,然后里面就是得到输入的用户名、密码什么的在通过两信息访问数据库学生信息表操作来判定该账号密码是否正确。若正确则打开学生页面,
代码如下:
package 学生选课;
//import JDBC.MyConnection;
import javax.swing.*;
import java.awt.*;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
public class Login extends JFrame {
public int width = Toolkit.getDefaultToolkit().getScreenSize().width;//获取屏幕的宽
public int height = Toolkit.getDefaultToolkit().getScreenSize().height;//获取屏幕的高
// TextField tf = new TextField(20);
JPanel jp = new JPanel();
ImageIcon image = new ImageIcon("out/production/学生选课系统/学生选课/Image/img.png");
// ImageIcon image2 = new ImageIcon("out/production/学生选课系统/学生选课/Image/Z.png");
// ImageIcon image2 = new ImageIcon("out/production/学生选课系统/学生选课/Image/erweiam.png");
ImageIcon image2 =new ImageIcon("out/production/学生选课系统/学生选课/Image/erweima.jpg");
private JLabel JLtitle;
private JLabel JLname;
private JLabel JLpwd;
static JTextField JTname;
static JPasswordField JTpwd;
private JButton JBsure;
private JRadioButton Jrb1,Jrb2,Jrb3; //定义单选按钮
ButtonGroup group=new ButtonGroup(); //按钮组 实现单选
// private static LoginListener ll = null;
// private JButton JBexit;
public Login() {
this.setTitle("学生选课系统");
this.setBounds((width - 800) / 2, (height - 600) / 2 - 20, 800, 600); //使窗体居中
this.setResizable(false);
JLtitle = new JLabel("用户登录界面");
JLtitle.setFont(new java.awt.Font("宋体", 4, 50));
JLtitle.setForeground(Color.black);
JLname = new JLabel("用户名:");//设置Label和按钮名
JLname.setForeground(Color.yellow);
JLpwd = new JLabel("密 码:");
JLpwd.setForeground(Color.yellow);
JTname = new JTextField(20);
JTpwd = new JPasswordField(20);
JBsure = new JButton("登录");
Jrb1 = new JRadioButton("学生");
Jrb2 = new JRadioButton("老师");
Jrb3 = new JRadioButton("管理员");
JLabel background =new JLabel(image); //将背景图片封装为一个JLable
JLabel erweima = new JLabel(image2); //二维码封装为JLable
// Jrb1.setFocusPainted(false);
// Jrb2.setFocusPainted(false);
// Jrb3.setFocusPainted(false);
Jrb3.setBounds(430,330,65,25);
Jrb2.setBounds(375,330,55,25);
Jrb1.setBounds(320,330,55,25);
JLtitle.setBounds(250,50,300,100);
JLname.setBounds(260, 240, 60, 25);//设置Label和按钮大小
JTname.setBounds(320, 240, 170, 25);
JLpwd.setBounds(260,280,60,25);
JTpwd.setBounds(320,280,170,25);
JBsure.setBounds(320,380,170,25);
erweima.setBounds(680,440,100,100);
// JBexit.setBounds(180,130,60,25);
background.setBounds(0,0,800,image.getIconHeight());
background.setOpaque(false); //背景透明
background.setLayout(null);
group.add(Jrb1);
group.add(Jrb2);
group.add(Jrb3); //将单选组件放在一个组件组里从而实现单选
background.add(this.Jrb1);
background.add(this.Jrb2);
background.add(this.Jrb3);
background.add(JLtitle);
background.add(JLname);
background.add(JTname);
background.add(JLpwd);
background.add(JTpwd);
background.add(JBsure);
background.add(erweima);
//
jp.add(background);
this.add(jp);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
this.setVisible(true);
// //添加按钮组件监听
Jrb1.addActionListener(e -> { //单选按钮选择学生时的监听
JBsure.addActionListener(a->{
String name = JTname.getText();
// String pwd = new String(JTpwd.getPassword());
String pwd = String.valueOf(JTpwd.getPassword());
//判断验证学生登录信息,
if("".equals(name)) {
//弹出消息对话框
JOptionPane.showMessageDialog(null,"账号不能为空","警告⚠",JOptionPane.ERROR_MESSAGE);
}
if("".equals(pwd)) {
//弹出消息对话框
JOptionPane.showMessageDialog(null,"密码不能为空","警告⚠",JOptionPane.ERROR_MESSAGE);
}
try{
new My_Connection(); //连接数据库
Connection con = My_Connection.getConnection();
Statement stmt=con.createStatement(); //statement声明
String sql = "select * from s where SNO = '" + name + "' and PSWD = '" + pwd + "'"; //SQL选择查询语句以该账号密码为条件查询该表
ResultSet rs=stmt.executeQuery(sql); //执行查询 ResultSet对象存放操作结果,一次只能看到一行数据
if (rs.next()) {
this.dispose();
new JStudent();
}
else {
//弹出消息对话框
JOptionPane.showMessageDialog(null,"账号或者密码错误","警告⚠",JOptionPane.ERROR_MESSAGE);
JTpwd.requestFocus();
}
}catch(Exception e1){
e1.printStackTrace();
}
// if (name.equals("123") && pwd.equals("456")){
// this.dispose(); //关闭当前的窗口
//
// //调用学生页面的显示窗口
// new JStudent();
// }
// else {
// //弹出消息对话框
// JOptionPane.showMessageDialog(null,"账号或者密码错误","警告⚠",JOptionPane.ERROR_MESSAGE);
// }
});
});
Jrb2.addActionListener(e -> { //单选按钮选择教师时的监听
JBsure.addActionListener(a->{
String name = JTname.getText();
// String pwd = new String(JTpwd.getPassword());
String pwd = String.valueOf(JTpwd.getPassword());
//判断验证学生登录信息,
if("".equals(name)) {
//弹出消息对话框
JOptionPane.showMessageDialog(null,"账号不能为空","警告⚠",JOptionPane.ERROR_MESSAGE);
}
if("".equals(pwd)) {
//弹出消息对话框
JOptionPane.showMessageDialog(null,"密码不能为空","警告⚠",JOptionPane.ERROR_MESSAGE);
}
try{
new My_Connection(); //连接数据库
Connection con = My_Connection.getConnection();
Statement stmt=con.createStatement(); //statement声明
String sql = "select * from t where TNO = '" + name + "' and PSWD = '" + pwd + "'"; //SQL选择查询语句以该账号密码为条件查询该表
ResultSet rs=stmt.executeQuery(sql); //执行查询
if (rs.next()) {
this.dispose();
new JTeacher();
}
else {
//弹出消息对话框
JOptionPane.showMessageDialog(null,"账号或者密码错误","警告⚠",JOptionPane.ERROR_MESSAGE);
JTpwd.requestFocus();
}
}catch(Exception e1){
e1.printStackTrace();
}
});
});
Jrb3.addActionListener(e -> { //单选按钮选择管理员时的监听
JBsure.addActionListener(a->{
String name = JTname.getText();
// String pwd = new String(JTpwd.getPassword());
String pwd = String.valueOf(JTpwd.getPassword());
//判断验证学生登录信息,
if("".equals(name)) {
//弹出消息对话框
JOptionPane.showMessageDialog(null,"账号不能为空","警告⚠",JOptionPane.ERROR_MESSAGE);
}
if("".equals(pwd)) {
//弹出消息对话框
JOptionPane.showMessageDialog(null,"密码不能为空","警告⚠",JOptionPane.ERROR_MESSAGE);
}
try{
new My_Connection(); //连接数据库
Connection con = My_Connection.getConnection();
Statement stmt=con.createStatement(); //statement声明
String sql = "select * from m where MNO = '" + name + "' and PSWD = '" + pwd + "'"; //SQL选择查询语句以该账号密码为条件查询该表
ResultSet rs=stmt.executeQuery(sql); //执行查询
if (rs.next()) {
this.dispose();
new JManager();
}
else {
//弹出消息对话框
JOptionPane.showMessageDialog(null,"账号或者密码错误","警告⚠",JOptionPane.ERROR_MESSAGE);
JTpwd.requestFocus();
}
}catch(Exception e1){
e1.printStackTrace();
}
});
});
}
public static void main(String[] args) {
new Login();
}
}
学生页面:(JStudent.java)
也不多赘述什么了,一切都在代码里:
package 学生选课;
import javax.swing.*;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;
import java.awt.*;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Vector;
public class JStudent extends JFrame {
JLabel JLmessage1,JLmessage2,JLmessage3,JLmessage4,JLmessage5;
String name = Login.JTname.getText();
JPanel panel1 = new JPanel();
JPanel panel2 = new JPanel();
JPanel panel3 = new JPanel();
// JPanel panel2_1 = new JPanel();
// JPanel panel2_2 = new JPanel();
ImageIcon image = new ImageIcon("out/production/学生选课系统/学生选课/Image/img.png");
private JLabel background;
private JButton JBsure1,JBsure2,JBsure3,JBsure4,JBsure5,JBsure6,JBsure7,JBsure8;
private JTextArea JTarea;
private JTable table1;
public JStudent(){
this.setTitle("学生界面");
int width = Toolkit.getDefaultToolkit().getScreenSize().width;//获取屏幕的宽
int height = Toolkit.getDefaultToolkit().getScreenSize().height;//获取屏幕的高
this.setBounds((width - 800) / 2, (height - 600) / 2 - 20, 800, 600); //使窗体居中
this.setResizable(false);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
this.setLayout(new FlowLayout());
background =new JLabel(image);
panel1.setPreferredSize(new Dimension(800,100)); //面板尺寸
panel1.add(background);
JBsure1 = new JButton("个人成绩");
JBsure1.setFont(new java.awt.Font("宋体", 3, 15));
JBsure1.setForeground(new Color(208, 16, 16));
JBsure2 = new JButton("选课");
JBsure2.setFont(new java.awt.Font("宋体", 3, 15));
JBsure2.setForeground(new Color(208, 16, 16));
JBsure3 = new JButton("退课");
JBsure3.setFont(new java.awt.Font("宋体", 3, 15));
JBsure3.setForeground(new Color(208, 16, 16));
JBsure4 = new JButton("修改密码");
JBsure4.setFont(new java.awt.Font("宋体", 3, 15));
JBsure4.setForeground(new Color(208, 16, 16));
JBsure5 = new JButton("老师信息");
JBsure5.setFont(new java.awt.Font("宋体", 3, 15));
JBsure5.setForeground(new Color(208, 16, 16));
JBsure6 = new JButton("课程信息");
JBsure6.setFont(new java.awt.Font("宋体", 3, 15));
JBsure6.setForeground(new Color(208, 16, 16));
JBsure7 = new JButton("返回");
JBsure7.setFont(new java.awt.Font("宋体", 3, 15));
JBsure7.setForeground(new Color(208, 16, 16));
// JBsure8 = new JButton("返回");
JTarea = new JTextArea(27,45);
panel3.setPreferredSize(new Dimension(500,500)); //面板尺寸
// panel2.setLayout(new GridLayout(4,1));//面板2的布局管理器为空
panel2.setLayout(null);
panel2.setPreferredSize(new Dimension(200,500)); //面板尺寸
JBsure1.setBounds(1,40,170,25);
JBsure2.setBounds(1,80,170,25);
JBsure3.setBounds(1,120,170,25);
JBsure4.setBounds(1,160,170,25);
JBsure5.setBounds(1,200,170,25);
JBsure6.setBounds(1,240,170,25);
JBsure7.setBounds(1,280,170,25);
// background.add(JBsure1);
// background.add(JBsure2);
// background.add(JBsure3);
// background.add(JBsure4);
panel2.add(JBsure1);
panel2.add(JBsure2);
panel2.add(JBsure3);
panel2.add(JBsure4);
panel2.add(JBsure5);
panel2.add(JBsure6);
panel2.add(JBsure7);
// panel3.add(JTarea);
// panel2.setPreferredSize(new Dimension(200,450)); //面板尺寸
// this.add(panel1,BorderLayout.PAGE_START);
this.add(panel1,BorderLayout.WEST);
this.add(panel2,BorderLayout.LINE_END);
this.add(panel3,BorderLayout.LINE_END);
this.setVisible(true);
//个人信息
try {
new My_Connection(); //连接数据库
Connection con = My_Connection.getConnection();
Statement stmt = con.createStatement(); //statement声明
String sql = "select SNO,SNAME,SEX,AGE,SDEPT from s where SNO = '" + name + "' "; //SQL选择查询语句以该账号为条件查询该表
ResultSet rs = stmt.executeQuery(sql); //执行查询 ResultSet对象存放操作结果,一次只能看到一行数据
while (rs.next()) {
// JTarea.append("学号: "+rs.getString("SNO"));
// JTarea.append("\n");
// JTarea.append("姓名: "+rs.getString("SNAME"));
// JTarea.append("\n");
// JTarea.append("性别: "+rs.getString("SEX"));
// JTarea.append("\n");
// JTarea.append("年龄: "+rs.getString("AGE"));
// JTarea.append("\n");
// JTarea.append("专业: "+rs.getString("SDEPT"));
JLmessage1 = new JLabel("学号:"+rs.getString("SNO"));
JLmessage1.setFont(new java.awt.Font("宋体", 3, 30));
JLmessage1.setForeground(Color.black);
JLmessage2 = new JLabel("姓名:"+rs.getString("SNAME"));
JLmessage2.setFont(new java.awt.Font("宋体", 3, 30));
JLmessage2.setForeground(Color.black);
JLmessage3 = new JLabel("性别:"+rs.getString("SEX"));
// JLmessage3 = new JLabel("性别:"+"男");
JLmessage3.setFont(new java.awt.Font("宋体", 3, 30));
JLmessage3.setForeground(Color.black);
JLmessage4 = new JLabel("年龄:"+rs.getString("AGE"));
// JLmessage4 = new JLabel("年龄:"+"20");
JLmessage4.setFont(new java.awt.Font("宋体", 3, 30));
JLmessage4.setForeground(Color.black);
JLmessage5 = new JLabel("专业:"+rs.getString("SDEPT"));
// JLmessage5 = new JLabel("专业:"+"软件工程");
JLmessage5.setFont(new java.awt.Font("宋体", 3, 30));
JLmessage5.setForeground(Color.black);
//组件布局
JLmessage1.setBounds(150,40,300,50);
JLmessage2.setBounds(150,100,250,50);
JLmessage3.setBounds(150,160,200,50);
JLmessage4.setBounds(150,220,200,50);
JLmessage5.setBounds(150,280,250,50);
panel3.setLayout(null);
panel3.add(JLmessage1);
panel3.add(JLmessage2);
panel3.add(JLmessage3);
panel3.add(JLmessage4);
panel3.add(JLmessage5);
}
}catch(Exception e){
System.out.println(e);
}
//个人成绩
JBsure1.addActionListener(e -> {
// String name = Login.JTname.getText();
Vector titlename = new Vector();//{"学号","姓名","性别","年龄","专业"};
titlename.add("学号");
titlename.add("姓名");
titlename.add("课程");
titlename.add("课程号");
titlename.add("成绩");
Vector rowdata = new Vector(); //数据
ResultSet rs = null;
try {
new My_Connection(); //连接数据库
Connection con = My_Connection.getConnection();
Statement stmt = con.createStatement(); //statement声明
String sql = "select S.SNO,SNAME,CNAME,C.CNO,GRADE from s,c,sc where S.SNO=SC.SNO and C.CNO=SC.CNO and S.SNO = '" + name + "' "; //SQL选择查询语句以该账号为条件查询该表
rs = stmt.executeQuery(sql);
} catch (Exception e1) {
System.out.println(e1);
System.out.println("查询出错");
}
try{
while (rs.next()) {
Vector hang = new Vector();
hang.add(rs.getString("S.SNO"));
hang.add(rs.getString("SNAME"));
hang.add(rs.getString("CNAME"));
hang.add(rs.getString("C.CNO"));
hang.add(rs.getString("GRADE"));
rowdata.add(hang);
}
}catch(Exception e1){
System.out.println("添加错误");
}
table1 = new JTable(rowdata, titlename);
table1.setEnabled(false); //表格不能编辑
JScrollPane sroll = new JScrollPane(table1);
JFrame jf = new JFrame();
jf.setTitle("成绩表");
jf.setBounds((width - 600) / 2, (height - 600) / 2 - 20, 600, 200); //使窗体居中
jf.setResizable(false);
// jf.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
jf.add(sroll);
jf.setVisible(true);
});
//选课
JBsure2.addActionListener(e -> {
new xuanke();
});
//退课
JBsure3.addActionListener(e -> {
new tuike();
});
//修改密码
JBsure4.addActionListener(e -> {
new change_pswd();
// this.dispose();
});
//教师信息
JBsure5.addActionListener(e -> {
Fuction.teach_infor();
});
//课程信息
JBsure6.addActionListener(e -> {
Fuction.courese_infor();
});
//返回
JBsure7.addActionListener(e -> {
this.dispose();
new Login();
});
}
// public static void main(String[] args) {
new JStudent();
// }
}
class xuanke extends JFrame {
String name = Login.JTname.getText(); //学号或账号
private JButton Jbu1;
static JTextField JTname;
private JLabel JLname;
ImageIcon image = new ImageIcon("out/production/学生选课系统/学生选课/Image/xuanke.png");
public xuanke() {
this.setTitle("选课");
int width = Toolkit.getDefaultToolkit().getScreenSize().width;//获取屏幕的宽
int height = Toolkit.getDefaultToolkit().getScreenSize().height;//获取屏幕的高
this.setBounds((width - 600) / 2, (height - 600) / 2 - 20, 400, 250); //使窗体居中
this.setResizable(false);
JLabel background =new JLabel(image); //将背景图片封装为一个JLable
JTname = new JTextField(20);
JLname = new JLabel("课程号:");
JLname.setFont(new java.awt.Font("宋体", 4, 15));
JLname.setForeground(new Color(19, 79, 50));
Jbu1 = new JButton("选该课");
Jbu1.setFont(new java.awt.Font("宋体", 3, 15));
Jbu1.setForeground(new Color(208, 16, 16));
JTname.setBounds(175,50,110,25);
JLname.setBounds(115,50,60,25);
Jbu1.setBounds(115,130,170,25);
background.setLayout(null);
background.add(JTname);
background.add(JLname);
background.add(Jbu1);
this.add(background);
this.setVisible(true);
Fuction.choosable_courese(); //显示可选课程 暂时有问题???
Jbu1.addActionListener(e -> { //暂未实现不可重复选即目前可以选择已选的课程
String CNO = JTname.getText();
//语句
if(CNO.equals("")) {
JOptionPane.showMessageDialog(null,"课程号不能为空!","警号⚠~",JOptionPane.ERROR_MESSAGE);
JTname.requestFocus();
}
else { //课程号不为空
//SQL语句实现退课
try {
new My_Connection(); //连接数据库
Connection con = My_Connection.getConnection();
Statement stmt = con.createStatement(); //statement声明
// String sql = "select * from s where SNO = '" + name + "' and PSWD = '" + pwd + "'"; //SQL选择查询语句以该账号密码为条件查询该表
String sql = " insert into sc(SNO,CNO,GRADE) values('"+name+"','"+CNO+"','') "; //学生选课,没有初始成绩
stmt.execute(sql); //执行
JOptionPane.showMessageDialog(null,"选课成功!","恭喜~",JOptionPane.INFORMATION_MESSAGE);
this.dispose();
}catch(Exception e1) {
System.out.println("选课失败~");
}
}
});
}
}
class tuike extends JFrame {
String name = Login.JTname.getText();
private JButton Jbu1;
static JTextField JTname;
private JLabel JLname;
ImageIcon image = new ImageIcon("out/production/学生选课系统/学生选课/Image/xuanke.png");
public tuike() {
this.setTitle("退课");
int width = Toolkit.getDefaultToolkit().getScreenSize().width;//获取屏幕的宽=1536
int height = Toolkit.getDefaultToolkit().getScreenSize().height;//获取屏幕的高=864
this.setBounds((width - 600) / 2, (height - 600) / 2 - 20, 400, 250); //使窗体居中
this.setResizable(false);
JLabel background =new JLabel(image); //将背景图片封装为一个JLable
JTname = new JTextField(20);
JLname = new JLabel("课程号:");
JLname.setFont(new java.awt.Font("宋体", 4, 15));
JLname.setForeground(new Color(19, 79, 50));
Jbu1 = new JButton("退该课!");
Jbu1.setFont(new java.awt.Font("宋体", 3, 15));
Jbu1.setForeground(new Color(208, 16, 16));
JTname.setBounds(175,50,110,25);
JLname.setBounds(115,50,60,25);
Jbu1.setBounds(115,130,170,25);
background.setLayout(null);
background.add(JTname);
background.add(JLname);
background.add(Jbu1);
this.add(background);
this.setVisible(true);
Fuction.choosed_courese(); //显示已选课程
Jbu1.addActionListener(e -> {
String CNO = JTname.getText();
if(CNO.equals("")) {
JOptionPane.showMessageDialog(null,"课程号不能为空!","警号⚠~",JOptionPane.ERROR_MESSAGE);
JTname.requestFocus();
}
else { //课程号不为空
//SQL语句实现退课
try {
new My_Connection(); //连接数据库
Connection con = My_Connection.getConnection();
Statement stmt = con.createStatement(); //statement声明
// String sql = "select * from s where SNO = '" + name + "' and PSWD = '" + pwd + "'"; //SQL选择查询语句以该账号密码为条件查询该表
String sql = " delete from SC where SNO= '"+name+"' and CNO = '"+CNO+"' "; //该语句存在问题 ???
stmt.execute(sql); //执行
JOptionPane.showMessageDialog(null,"退课成功!","恭喜~",JOptionPane.INFORMATION_MESSAGE);
this.dispose();
}catch(Exception e1) {
System.out.println("退课失败~");
}
}
});
}
// public static void main(String[] args) {
// new tuike();
// }
}
//改密码
class change_pswd extends JFrame {
String name = Login.JTname.getText();
// String pwd = String.valueOf(Login.JTpwd.getPassword());
private JButton Jbu1;
// static JTextField JTname;
private JPasswordField JTpwd1,JTpwd2;
private JLabel JLname1,JLname2;
ImageIcon image = new ImageIcon("out/production/学生选课系统/学生选课/Image/xuanke.png");
public change_pswd() {
this.setTitle("更改密码");
int width = Toolkit.getDefaultToolkit().getScreenSize().width;//获取屏幕的宽=1536
int height = Toolkit.getDefaultToolkit().getScreenSize().height;//获取屏幕的高=864
this.setBounds((width - 600) / 2, (height - 600) / 2 - 20, 400, 250); //使窗体居中
this.setResizable(false);
JLabel background =new JLabel(image); //将背景图片封装为一个JLable
// JTname = new JTextField(20);
JTpwd1 = new JPasswordField(20);
JTpwd2 = new JPasswordField(20);
JLname1 = new JLabel("旧密码:");
JLname1.setFont(new java.awt.Font("宋体", 4, 15));
JLname1.setForeground(new Color(19, 79, 50));
JLname2 = new JLabel("新密码:");
JLname2.setFont(new java.awt.Font("宋体", 4, 15));
JLname2.setForeground(new Color(19, 79, 50));
Jbu1 = new JButton("确认!");
Jbu1.setFont(new java.awt.Font("宋体", 3, 15));
Jbu1.setForeground(new Color(208, 16, 16));
JTpwd1.setBounds(175,50,110,25);//旧密码
JLname1.setBounds(115,50,60,25);
JTpwd2.setBounds(175,90,110,25);//新密码
JLname2.setBounds(115,90,60,25);
Jbu1.setBounds(115,130,170,25);
background.setLayout(null);
background.add(JTpwd1);
background.add(JLname1);
background.add(JTpwd2);
background.add(JLname2);
background.add(Jbu1);
this.add(background);
this.setVisible(true);
Jbu1.addActionListener(e -> {
String old_pwd = String.valueOf(JTpwd1.getPassword()); //键盘上输入旧密码
String pwd = null;
try {
new My_Connection(); //连接数据库
Connection con = My_Connection.getConnection();
Statement stmt = con.createStatement(); //statement声明
String sql = "select PSWD from s where SNO = '" + name + "' "; //SQL选择查询语句以该账号为条件查询该表
ResultSet rs = stmt.executeQuery(sql); //执行查询 ResultSet对象存放操作结果,一次只能看到一行数据
while (rs.next()) {
pwd = rs.getString("PSWD");
System.out.println(pwd);
}
if (old_pwd.equals(pwd)) { //实现不退出学生页面而能一直进行改密;
String new_pwd = String.valueOf(JTpwd2.getPassword());
// Statement stmt=null;
// Connection con=null;
// 连接数据库实现改密码
try{
new My_Connection(); //连接数据库
Connection con1 = My_Connection.getConnection();
Statement stmt1 =con1.createStatement(); //statement声明
String sql1 = "update S set PSWD ='" +new_pwd+ "' where SNO = '" +name+ "'"; //SQL选择查询语句以该账号密码为条件查询该表
stmt1.execute(sql1); //执行更新语句
JOptionPane.showMessageDialog(null,"改密成功!","恭喜~",JOptionPane.ERROR_MESSAGE);
this.dispose();
// JStudent.dispose();
// new Login(); //重新登录
}catch(Exception e1){
e1.printStackTrace();
JOptionPane.showMessageDialog(null,"改密失败!","很遗憾~",JOptionPane.ERROR_MESSAGE);
}
//弹出消息对话框
}
else {
//弹出消息对话框
JOptionPane.showMessageDialog(null,"旧密码填写错误!","警告⚠",JOptionPane.ERROR_MESSAGE);
JTpwd1.requestFocus();
}
}catch(Exception e2) {
System.out.println("查旧密码出错");
}
});
}
// public static void main(String[] args) {
// new change_pswd();
// }
}
鄙人在此声明一下:数据库学的不太好,某些数据建库的查询语句有问题 #惭愧惭愧#
大佬勿怪!
一级页面都放在对应的身份的页面里,二级页面代码放在 Fuction.java 里面,在需要的地方里面调用就行。这也是受了当时Python版的启发。
说说其中一个修改密码的二级页面叭
修改成功后:
如果旧密码输入不正确的话则不能修改成功:
毕竟不是Css界面美化不好做 只能能力有限,只能做成这个样子了 将就就这看叭~
教师页面:(JTeacher.java)
注意:本模块以后的功能均未实现~
package 学生选课;
import javax.swing.*;
import java.awt.*;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
public class JTeacher extends JFrame {
JLabel JLmessage1,JLmessage2,JLmessage3,JLmessage4,JLmessage5;
String name = Login.JTname.getText();
JPanel panel1 = new JPanel();
JPanel panel2 = new JPanel();
JPanel panel3 = new JPanel();
// JPanel panel2_1 = new JPanel();
// JPanel panel2_2 = new JPanel();
ImageIcon image = new ImageIcon("out/production/学生选课系统/学生选课/Image/img.png");
private JLabel background;
private JButton JBsure1,JBsure2,JBsure3,JBsure4,JBsure5;
private JTextArea JTarea;
public JTeacher() {
this.setTitle("教师界面");
int width = Toolkit.getDefaultToolkit().getScreenSize().width;//获取屏幕的宽
int height = Toolkit.getDefaultToolkit().getScreenSize().height;//获取屏幕的高
this.setBounds((width - 800) / 2, (height - 600) / 2 - 20, 800, 600); //使窗体居中
this.setResizable(false);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
this.setLayout(new FlowLayout());
background =new JLabel(image);
panel1.setPreferredSize(new Dimension(800,100)); //面板尺寸
panel1.add(background);
// JBsure1 = new JButton("个人信息");
JBsure2 = new JButton("录入成绩");
JBsure3 = new JButton("成绩单");
JBsure4 = new JButton("修改密码");
JBsure5 = new JButton("返回");
JTarea = new JTextArea(27,45);
panel3.setPreferredSize(new Dimension(500,500)); //面板尺寸
// panel2.setLayout(new GridLayout(4,1));//面板2的布局管理器为空
panel2.setLayout(null);
panel2.setPreferredSize(new Dimension(200,500)); //面板尺寸
// JBsure1.setBounds(1,40,170,25);
JBsure2.setBounds(1,80,170,25);
JBsure3.setBounds(1,120,170,25);
JBsure4.setBounds(1,160,170,25);
JBsure5.setBounds(1,200,170,25);
// background.add(JBsure1);
// background.add(JBsure2);
// background.add(JBsure3);
// background.add(JBsure4);
// panel2.add(JBsure1);
panel2.add(JBsure2);
panel2.add(JBsure3);
panel2.add(JBsure4);
panel2.add(JBsure5);
// panel3.add(JTarea);
// panel2.setPreferredSize(new Dimension(200,450)); //面板尺寸
// this.add(panel1,BorderLayout.PAGE_START);
this.add(panel1,BorderLayout.WEST);
this.add(panel2,BorderLayout.LINE_END);
this.add(panel3,BorderLayout.LINE_END);
// this.add(panel2,BorderLayout.PAGE_END);
this.setVisible(true);
try {
new My_Connection(); //连接数据库
Connection con = My_Connection.getConnection();
Statement stmt = con.createStatement(); //statement声明
String sql = "select TNO,TNAME,SEX,TITLE from t where TNO = '" + name + "' "; //SQL选择查询语句以该账号为条件查询该表
ResultSet rs = stmt.executeQuery(sql); //执行查询 ResultSet对象存放操作结果,一次只能看到一行数据
while (rs.next()) {
JLmessage1 = new JLabel("工号:"+rs.getString("TNO"));
// JLmessage1 = new JLabel("学号:"+"19408010113");
JLmessage1.setFont(new java.awt.Font("宋体", 3, 30));
JLmessage1.setForeground(Color.black);
JLmessage2 = new JLabel("姓名:"+rs.getString("TNAME"));
// JLmessage2 = new JLabel("姓名:"+"翟明飞");
JLmessage2.setFont(new java.awt.Font("宋体", 3, 30));
JLmessage2.setForeground(Color.black);
JLmessage3 = new JLabel("性别:"+rs.getString("SEX"));
// JLmessage3 = new JLabel("性别:"+"男");
JLmessage3.setFont(new java.awt.Font("宋体", 3, 30));
JLmessage3.setForeground(Color.black);
JLmessage4 = new JLabel("职称:"+rs.getString("TITLE"));
// JLmessage4 = new JLabel("年龄:"+"20");
JLmessage4.setFont(new java.awt.Font("宋体", 3, 30));
JLmessage4.setForeground(Color.black);
JLmessage1.setBounds(150,40,300,50);
JLmessage2.setBounds(150,100,250,50);
JLmessage3.setBounds(150,160,200,50);
JLmessage4.setBounds(150,220,200,50);
// JLmessage5.setBounds(150,280,250,50);
panel3.setLayout(null);
panel3.add(JLmessage1);
panel3.add(JLmessage2);
panel3.add(JLmessage3);
panel3.add(JLmessage4);
// panel3.add(JLmessage5);
}
}catch(Exception e){
System.out.println(e);
System.out.println("出错");
}
// JBsure1.addActionListener(e -> {
// //
// });
//录入成绩
JBsure2.addActionListener(e -> {
//
new add_Grade();
});
//成绩单
JBsure3.addActionListener(e -> {
//
new list_Grade();
});
//修改密码
JBsure4.addActionListener(e -> {
//
new update_pswd();
});
//返回
JBsure5.addActionListener(e -> {
//
this.dispose();
new Login();
});
}
// public static void main(String[] args) {
// new JTeacher();
// }
}
//录入成绩
class add_Grade extends JFrame {
public add_Grade() {
}
}
//成绩单
class list_Grade extends JFrame {
public list_Grade() {
}
}
//修改密码
class update_pswd extends JFrame {
String name = Login.JTname.getText();
// String pwd = String.valueOf(Login.JTpwd.getPassword());
private JButton Jbu1;
// static JTextField JTname;
private JPasswordField JTpwd1,JTpwd2;
private JLabel JLname1,JLname2;
ImageIcon image = new ImageIcon("out/production/学生选课系统/学生选课/Image/xuanke.png");
public update_pswd() {
this.setTitle("更改密码");
int width = Toolkit.getDefaultToolkit().getScreenSize().width;//获取屏幕的宽=1536
int height = Toolkit.getDefaultToolkit().getScreenSize().height;//获取屏幕的高=864
this.setBounds((width - 600) / 2, (height - 600) / 2 - 20, 400, 250); //使窗体居中
this.setResizable(false);
JLabel background =new JLabel(image); //将背景图片封装为一个JLable
// JTname = new JTextField(20);
JTpwd1 = new JPasswordField(20);
JTpwd2 = new JPasswordField(20);
JLname1 = new JLabel("旧密码:");
JLname1.setFont(new java.awt.Font("宋体", 4, 15));
JLname1.setForeground(new Color(19, 79, 50));
JLname2 = new JLabel("新密码:");
JLname2.setFont(new java.awt.Font("宋体", 4, 15));
JLname2.setForeground(new Color(19, 79, 50));
Jbu1 = new JButton("确认!");
Jbu1.setFont(new java.awt.Font("宋体", 3, 15));
Jbu1.setForeground(new Color(208, 16, 16));
JTpwd1.setBounds(175,50,110,25);//旧密码
JLname1.setBounds(115,50,60,25);
JTpwd2.setBounds(175,90,110,25);//新密码
JLname2.setBounds(115,90,60,25);
Jbu1.setBounds(115,130,170,25);
background.setLayout(null);
background.add(JTpwd1);
background.add(JLname1);
background.add(JTpwd2);
background.add(JLname2);
background.add(Jbu1);
this.add(background);
this.setVisible(true);
Jbu1.addActionListener(e -> {
String old_pwd = String.valueOf(JTpwd1.getPassword()); //键盘上输入旧密码
String pwd = null;
try {
new My_Connection(); //连接数据库
Connection con = My_Connection.getConnection();
Statement stmt = con.createStatement(); //statement声明
String sql = "select PSWD from t where TNO = '" + name + "' "; //SQL选择查询语句以该账号为条件查询该表
ResultSet rs = stmt.executeQuery(sql); //执行查询 ResultSet对象存放操作结果,一次只能看到一行数据
while (rs.next()) {
pwd = rs.getString("PSWD");
System.out.println(pwd);
}
if (old_pwd.equals(pwd)) { //实现不退出教师页面而能一直进行改密;
String new_pwd = String.valueOf(JTpwd2.getPassword());
// Statement stmt=null;
// Connection con=null;
// 连接数据库实现改密码
try{
new My_Connection(); //连接数据库
Connection con1 = My_Connection.getConnection();
Statement stmt1 =con1.createStatement(); //statement声明
String sql1 = "update T set PSWD ='" +new_pwd+ "' where TNO = '" +name+ "'"; //SQL选择查询语句以该账号密码为条件查询该表
stmt1.execute(sql1); //执行更新语句
JOptionPane.showMessageDialog(null,"改密成功!","恭喜~",JOptionPane.ERROR_MESSAGE);
this.dispose();
// JStudent.dispose();
// new Login(); //重新登录
}catch(Exception e1){
e1.printStackTrace();
JOptionPane.showMessageDialog(null,"改密失败!","很遗憾~",JOptionPane.ERROR_MESSAGE);
}
//弹出消息对话框
}
else {
//弹出消息对话框
JOptionPane.showMessageDialog(null,"旧密码填写错误!","警告⚠",JOptionPane.ERROR_MESSAGE);
JTpwd1.requestFocus();
}
}catch(Exception e2) {
System.out.println("查旧密码出错");
}
});
}
}
管理员:(JManager.java)
此处又分为三个子页面:
-
学生管理、(JSM.java)
-
教师管理、(JTM/java)
-
课程管理。(JCM.java)
JManager.java管理员界面:
package 学生选课;
import javax.swing.*;
import java.awt.*;
public class JManager extends JFrame {
JPanel panel1 = new JPanel();
JPanel panel2 = new JPanel();
JPanel panel3 = new JPanel();
// JPanel panel2_1 = new JPanel();
// JPanel panel2_2 = new JPanel();
ImageIcon image = new ImageIcon("out/production/学生选课系统/学生选课/Image/img.png");
private JLabel background;
private JButton JBsure1;
private JButton JBsure2;
private JButton JBsure3;
// private JButton JBsure4;
// private JTextArea JTarea;
public JManager() {
this.setTitle("管理员界面");
int width = Toolkit.getDefaultToolkit().getScreenSize().width;//获取屏幕的宽
int height = Toolkit.getDefaultToolkit().getScreenSize().height;//获取屏幕的高
this.setBounds((width - 800) / 2, (height - 600) / 2 - 20, 400, 300); //使窗体居中
this.setResizable(false);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
this.setLayout(new FlowLayout());
background =new JLabel(image);
panel1.setPreferredSize(new Dimension(800,600)); //面板尺寸
panel1.add(background);
JLabel JLtitle = new JLabel("管理员界面");
JLtitle.setFont(new java.awt.Font("宋体", 3, 30));
JLtitle.setForeground(Color.black);
JBsure1 = new JButton("学生管理");
JBsure2 = new JButton("教师管理");
JBsure3 = new JButton("课程管理");
// JBsure4 = new JButton("个人信息");
// JTarea = new JTextArea(12,14);
// panel2.setLayout(null);//面板2的布局管理器为空
JLtitle.setBounds(320,10,200,50);
JBsure1.setBounds(320,80,170,25);
JBsure2.setBounds(320,120,170,25);
JBsure3.setBounds(320,160,170,25);
// background.setLayout(null);
background.add(JLtitle);
background.add(JBsure1);
background.add(JBsure2);
background.add(JBsure3);
// background.add(JBsure4);
// panel2.add(JBsure1);
// panel2.add(JBsure2);
// panel2.add(JBsure3);
panel2.add(JBsure4);
// panel3.add(JTarea);
// panel2.setPreferredSize(new Dimension(200,500)); //面板尺寸
// this.add(panel1,BorderLayout.PAGE_START);
// this.add(panel2,BorderLayout.LINE_END);
// this.add(panel3,BorderLayout.LINE_END);
// this.add(panel2,BorderLayout.PAGE_END);
this.add(panel1);
this.setVisible(true);
JBsure1.addActionListener(e -> {
new JSM(); //调用学生管理界面
});
JBsure2.addActionListener(e -> {
new JTM();
});
JBsure3.addActionListener(e -> {
new JCM();
});
}
public static void main(String[] args) {
new JManager();
}
}
学生管理(JSM.java):
package 学生选课;
import javax.swing.*;
import java.awt.*;
public class JSM extends JFrame {
private JButton but1,but2,but3,but4,but5;
public JSM () {
this.setTitle("学生管理");
int width = Toolkit.getDefaultToolkit().getScreenSize().width;//获取屏幕的宽
int height = Toolkit.getDefaultToolkit().getScreenSize().height;//获取屏幕的高
this.setBounds((width - 800) / 2, (height - 600) / 2 - 20, 800, 600); //使窗体居中
this.setResizable(false);
// this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
ImageIcon image = new ImageIcon("out/production/学生选课系统/学生选课/Image/JSM.png");
JLabel background = new JLabel(image);
background.setOpaque(false); //背景透明
but1 = new JButton("添加学生");
but2 = new JButton("删除学生");
but3 = new JButton("修改学生");
but4 = new JButton("查询学生");
but5 = new JButton("返回");
but1.setBounds(30,40,170,25);
but2.setBounds(30,120,170,25);
but3.setBounds(30,200,170,25);
but4.setBounds(30,280,170,25);
but5.setBounds(30,350,170,25);
background.add(but1);
background.add(but2);
background.add(but3);
background.add(but4);
background.add(but5);
this.add(background);
this.setVisible(true);
}
public static void main(String[] args) {
new JSM();
}
}
教师管理(JTM/java):
package 学生选课;
import javax.swing.*;
import java.awt.*;
public class JTM extends JFrame {
private JButton but1,but2,but3,but4,but5;
public JTM () {
this.setTitle("教师管理");
int width = Toolkit.getDefaultToolkit().getScreenSize().width;//获取屏幕的宽
int height = Toolkit.getDefaultToolkit().getScreenSize().height;//获取屏幕的高
this.setBounds((width - 800) / 2, (height - 600) / 2 - 20, 800, 600); //使窗体居中
this.setResizable(false);
//
ImageIcon image = new ImageIcon("out/production/学生选课系统/学生选课/Image/JTM.png");
JLabel background = new JLabel(image);
background.setOpaque(false); //背景透明
but1 = new JButton("添加教师");
but2 = new JButton("删除教师");
but3 = new JButton("修改教师");
but4 = new JButton("查询教师");
but5 = new JButton("返回");
but1.setBounds(30,40,170,25);
but2.setBounds(30,120,170,25);
but3.setBounds(30,200,170,25);
but4.setBounds(30,280,170,25);
but5.setBounds(30,350,170,25);
background.add(but1);
background.add(but2);
background.add(but3);
background.add(but4);
background.add(but5);
this.add(background);
this.setVisible(true);
}
public static void main(String[] args) {
new JTM();
}
}
课程管理(JCM.java)
package 学生选课;
import javax.swing.*;
import java.awt.*;
public class JCM extends JFrame {
private JButton but1,but2,but3,but4,but5;
public JCM () {
this.setTitle("课程管理");
int width = Toolkit.getDefaultToolkit().getScreenSize().width;//获取屏幕的宽
int height = Toolkit.getDefaultToolkit().getScreenSize().height;//获取屏幕的高
this.setBounds((width - 800) / 2, (height - 600) / 2 - 20, 800, 600); //使窗体居中
this.setResizable(false);
// this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
ImageIcon image = new ImageIcon("out/production/学生选课系统/学生选课/Image/JCM.png");
JLabel background = new JLabel(image);
background.setOpaque(false); //背景透明
but1 = new JButton("添加课程");
but2 = new JButton("删除课程");
but3 = new JButton("修改课程");
but4 = new JButton("查询课程");
but5 = new JButton("返回");
but1.setBounds(30,40,170,25);
but2.setBounds(30,120,170,25);
but3.setBounds(30,200,170,25);
but4.setBounds(30,280,170,25);
but5.setBounds(30,350,170,25);
background.add(but1);
background.add(but2);
background.add(but3);
background.add(but4);
background.add(but5);
this.add(background);
this.setVisible(true);
}
public static void main(String[] args) {
new JCM();
}
}
还有同样重要的 Fuction.java:
package 学生选课;
import javax.swing.*;
import java.awt.*;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.Vector;
public class Fuction {
// static Login l = new Login();
// //可选课程
public static void choosable_courese() {
String name = Login.JTname.getText();
JTable table;
JFrame jf = new JFrame();
jf.setTitle("可选课程");
int width = Toolkit.getDefaultToolkit().getScreenSize().width;//获取屏幕的宽=1536
int height = Toolkit.getDefaultToolkit().getScreenSize().height;//获取屏幕的高=864
jf.setBounds((width - 600) / 2, (height - 300) / 2+100 ,400,300);
Vector titlename1 = new Vector();//{"学号","姓名","性别","年龄","专业"};
titlename1.add("课程号");
titlename1.add("课程名");
titlename1.add("教师名");
Vector rowdata1 = new Vector(); //数据
ResultSet rs = null;
try {
new My_Connection(); //连接数据库
Connection con = My_Connection.getConnection();
Statement stmt = con.createStatement(); //statement声明
String sql = "select C.CNO,CNAME,TNAME from c,sc where C.CNO=sc.CNO and sc.CNO not in (select CNO from sc where SNO='" + name + "')"; //SQL选择查询语句以该账号为条件查询该表
rs = stmt.executeQuery(sql);
} catch (Exception e1) {
System.out.println(e1);
System.out.println("查询可选课程出错");
}
try{
while (rs.next()) {
Vector hang1 = new Vector();
hang1.add(rs.getString("CNO"));
hang1.add(rs.getString("CNAME"));
hang1.add(rs.getString("TNAME"));
rowdata1.add(hang1);
}
}catch(Exception e1){
System.out.println("添加错误");
}
table = new JTable(rowdata1, titlename1);
table.setEnabled(false); //表格不能编辑
JScrollPane sroll1 = new JScrollPane(table);
jf.add(sroll1);
jf.setVisible(true);
}
//已选课程
public static void choosed_courese() {
String name = Login.JTname.getText();
JTable table;
JFrame jf = new JFrame();
jf.setTitle("已选课程");
int width = Toolkit.getDefaultToolkit().getScreenSize().width;//获取屏幕的宽=1536
int height = Toolkit.getDefaultToolkit().getScreenSize().height;//获取屏幕的高=864
jf.setBounds((width - 600) / 2, (height - 300) / 2+100 ,400,300);
Vector titlename1 = new Vector();//{"学号","姓名","性别","年龄","专业"};
titlename1.add("课程号");
titlename1.add("课程名");
Vector rowdata1 = new Vector(); //数据
ResultSet rs = null;
try {
new My_Connection(); //连接数据库
Connection con = My_Connection.getConnection();
Statement stmt = con.createStatement(); //statement声明
String sql = "select C.CNO,CNAME from sc,c where C.CNO=sc.CNO and SNO='" + name + "'"; //SQL选择查询语句以该账号为条件查询该表
rs = stmt.executeQuery(sql);
} catch (Exception e1) {
System.out.println(e1);
System.out.println("查询已选课程出错");
}
try{
while (rs.next()) {
Vector hang1 = new Vector();
hang1.add(rs.getString("CNO"));
hang1.add(rs.getString("CNAME"));
rowdata1.add(hang1);
}
}catch(Exception e1){
System.out.println("添加错误");
}
table = new JTable(rowdata1, titlename1);
table.setEnabled(false); //表格不能编辑
JScrollPane sroll1 = new JScrollPane(table);
jf.add(sroll1);
jf.setVisible(true);
}
public static void teach_infor() {
JTable table;
JFrame jf = new JFrame();
jf.setTitle("老师信息");
int width = Toolkit.getDefaultToolkit().getScreenSize().width;//获取屏幕的宽=1536
int height = Toolkit.getDefaultToolkit().getScreenSize().height;//获取屏幕的高=864
jf.setBounds((width - 600) / 2, (height - 300) / 2+100 ,500,300);
Vector titlename1 = new Vector();//{"学号","姓名","性别","年龄","专业"};
titlename1.add("教工号");
titlename1.add("教师名");
titlename1.add("性别");
titlename1.add("职称");
titlename1.add("电话");
titlename1.add("Email");
Vector rowdata1 = new Vector(); //数据
ResultSet rs = null;
try {
new My_Connection(); //连接数据库
Connection con = My_Connection.getConnection();
Statement stmt = con.createStatement(); //statement声明
String sql = "select TNO,TNAME,SEX,TITLE,PHONE,EMAIL from t "; //SQL选择查询语句以该账号为条件查询该表
rs = stmt.executeQuery(sql);
} catch (Exception e1) {
System.out.println(e1);
System.out.println("查询教师信息出错");
}
try{
while (rs.next()) {
Vector hang1 = new Vector();
hang1.add(rs.getString("TNO"));
hang1.add(rs.getString("TNAME"));
hang1.add(rs.getString("SEX"));
hang1.add(rs.getString("TITLE"));
hang1.add(rs.getString("PHONE"));
hang1.add(rs.getString("EMAIL"));
rowdata1.add(hang1);
}
}catch(Exception e1){
System.out.println("添加错误");
}
table = new JTable(rowdata1, titlename1);
table.setEnabled(false); //表格不能编辑
JScrollPane sroll1 = new JScrollPane(table);
jf.add(sroll1);
jf.setVisible(true);
}
public static void courese_infor() {
JTable table;
JFrame jf = new JFrame();
jf.setTitle("课程信息");
int width = Toolkit.getDefaultToolkit().getScreenSize().width;//获取屏幕的宽=1536
int height = Toolkit.getDefaultToolkit().getScreenSize().height;//获取屏幕的高=864
jf.setBounds((width - 600) / 2, (height - 300) / 2+100 ,500,300);
Vector titlename1 = new Vector();//{"学号","姓名","性别","年龄","专业"};
titlename1.add("课程号");
titlename1.add("课程名");
titlename1.add("学分");
titlename1.add("专业");
titlename1.add("教师");
titlename1.add("教工号");
Vector rowdata1 = new Vector(); //数据
ResultSet rs = null;
try {
new My_Connection(); //连接数据库
Connection con = My_Connection.getConnection();
Statement stmt = con.createStatement(); //statement声明
String sql = "select CNO,CNAME,CREDI,CDEPT,TNAME,TNO from c "; //SQL选择查询语句以该账号为条件查询该表
rs = stmt.executeQuery(sql);
} catch (Exception e1) {
System.out.println(e1);
System.out.println("查询课程信息出错");
}
try{
while (rs.next()) {
Vector hang1 = new Vector();
hang1.add(rs.getString("CNO"));
hang1.add(rs.getString("CNAME"));
hang1.add(rs.getString("CREDI"));
hang1.add(rs.getString("CDEPT"));
hang1.add(rs.getString("TNAME"));
hang1.add(rs.getString("TNO"));
rowdata1.add(hang1);
}
}catch(Exception e1){
System.out.println("添加错误");
}
table = new JTable(rowdata1, titlename1);
table.setEnabled(false); //表格不能编辑
JScrollPane sroll1 = new JScrollPane(table);
jf.add(sroll1);
jf.setVisible(true);
}
public static void main(String[] args) {
Fuction.courese_infor();
}
}
My_Connection.java 数据库链接部分:
package 学生选课;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class My_Connection {
public static Connection getConnection() {
String driver = "com.mysql.cj.jdbc.Driver";//使用Jar包驱动
String url = "jdbc:mysql://localhost:3306/student?serverTimezone=UTC";//链接数据库端口,数据库名
String username = "root";//数据库用户名
String password = "root";//数据库密码
Connection conn = null;
try {
Class.forName(driver); //classLoader,加载对应驱动
conn = (Connection) DriverManager.getConnection(url, username, password);
System.out.println("数据库已连接");
} catch (ClassNotFoundException e) {//异常捕获
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
return conn;
}
public static void main(String[] args) {
My_Connection.getConnection();
}
}
//public class My_Connection {
// public static Connection getConnection() {
// String driver = "com.mysql.cj.jdbc.Driver";//使用Jar包驱动
// String url = "jdbc:mysql://localhost:3306/学生选课?serverTimezone=UTC";//链接数据库端口,数据库名
// String username = "root";//数据库用户名
// String password = "root";//数据库密码
// Connection conn = null;
// try {
// Class.forName(driver); //classLoader,加载对应驱动
// conn = (Connection) DriverManager.getConnection(url, username, password);
// System.out.println("连接成功");
// } catch (ClassNotFoundException e) {//异常捕获
// e.printStackTrace();
// } catch (SQLException e) {
// e.printStackTrace();
// }
// return conn;
// }
//
// public static void main(String[] args) {
// My_Connection.getConnection();
// }
//}
数据库:
结束了。。。
里面的东西有兴趣自己去填充叭~