一. 程序说明
1.程序使用JAVA程序连接MYSQL数据库。
2.学生信息管理系统分为三个表:
student,course,student_course
3.在mysql中分别创建:
create table student(name varchar(20),no varchar(20),age varchar(20),sex varchar(20),primary key(no));
create table course(c_name varchar(20),c_no varchar(20),c_credit varchar(20),PRIMARY KEY(c_no));
create table sc(no varchar(20),c_no varchar(20),sc_grade varchar(20),foreign key (no) references student(no),foreign key (c_no) references course(c_no));
4.临时写的,代码写的很乱,也没写返回功能。
二. 运行截图
1.登录界面,错误会提示重新输入,正确会创建新窗口
2. 选择一个表进行操作
3.点击查询
4.增加一位学生信息
5.删除学号为006的学生
三. 源代码
import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.sql.*;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTextField;
public class Test {
public static void main(String[] args) {
/*登陆面板*/
JFrame f= new JFrame("学生选课系统");
f.setSize(400, 300);
f.setLocationRelativeTo(null);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setLayout(new GridLayout(5,1));
JLabel name1 = new JLabel(" 用户名:");
JLabel password1 = new JLabel(" 密码:");
JTextField name2 = new JTextField();
JTextField password2 = new JTextField();
JButton bt = new JButton("登陆");
f.add(name1);
f.add(name2);
f.add(password1);
f.add(password2);
f.add(bt);
f.setVisible(true);
bt.addActionListener(e->{
String name3 = name2.getText();
String password3 =password2.getText();
if(name3.equals("root") & password3.equals("123")) {
JDialog d1 = new JDialog(f, "提示",true);
d1.setSize(200, 100);
d1.setLocationRelativeTo(null);
d1.add(new JLabel("登陆成功"),BorderLayout.CENTER);
d1.setVisible(true);
f.setVisible(false);
//加载MYSQL JDBC驱动程序
try {
Class.forName("com.mysql.jdbc.Driver");
}
catch (Exception e1) {
e1.printStackTrace();
}
//新的窗体,进行管理操作
JFrame f2 = new JFrame("学生选课管理系统");
f2.setSize(500, 400);
f2.setLocationRelativeTo(null);
f2.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f2.setVisible(true);
//右面板,用于显示操作的结果
JPanel right = new JPanel();
right.setLayout(new BorderLayout());
right.add(new JLabel(" 查询结果如下: "),BorderLayout.NORTH);
JTextArea ta1 = new JTextArea(20,5);
right.add(ta1,BorderLayout.SOUTH);
//左边面板,用于选择操作的数据表
JPanel left = new JPanel();
left.setLayout(new GridLayout(4,1,10,10));
JLabel leftl = new JLabel("请点击按钮选择要操作的数据表");
JButton leftbt1 = new JButton("TABLE: student");
JButton leftbt2 = new JButton("TABLE: course");
JButton leftbt3 = new JButton("TABLE: sc");
left.add(leftl);
left.add(leftbt1);
left.add(leftbt2);
left.add(leftbt3);
//student按钮的监听器
leftbt1.addActionListener(a->{
f2.remove(left);
JPanel left1 = new JPanel();
left1.setLayout(new GridLayout(5,1,10,10));
JButton leftbt11 = new JButton("查询所有学生的信息");
//第一个按钮的监听器
leftbt11.addActionListener(b->{
//连接数据库
try {
Connection connect = DriverManager.getConnection(
"jdbc:mysql://localhost:3306/XUANKE","root","123");
Statement stmt = connect.createStatement();
ResultSet rs = stmt.executeQuery("select * from student;");
StringBuffer sb1 = new StringBuffer("\n\n\n|姓名| 学号 | 年龄 |性别 |\n");
while (rs.next()) {
String str1 = new String("| "+rs.getString("name")+" | "+rs.getString("no")+" | "+rs.getString("age")+" | "+rs.getString("sex")+"|\n");
sb1.append(str1);
}
ta1.setText(sb1.toString());
right.validate();
}
catch (Exception e1) {
System.out.print("get data error!");
e1.printStackTrace();
}
});
//第二个按钮的监听器
JPanel left11 = new JPanel();
left11.setLayout(new GridLayout(1,4,10,10));
JTextField tf1 = new JTextField();
JTextField tf2 = new JTextField();
JTextField tf3 = new JTextField();
JTextField tf4 = new JTextField();
left11.add(tf1);
left11.add(tf2);
left11.add(tf3);
left11.add(tf4);
left1.add(left11);
JButton leftbt12 = new JButton("按照上方填写内容增加一位学生的信息,");
leftbt12.addActionListener(c->{
String str_tf1 = tf1.getText();
String str_tf2 = tf2.getText();
String str_tf3 = tf3.getText();
String str_tf4 = tf4.getText();
//连接数据库
try {
Connection connect = DriverManager.getConnection(
"jdbc:mysql://localhost:3306/XUANKE","root","123");
Statement stmt = connect.createStatement();
String str1 = new String("insert into student values('"+str_tf1+"','"+str_tf2+"','"+str_tf3+"','"+str_tf4+"');");
stmt.executeUpdate(str1);
ResultSet rs = stmt.executeQuery("select * from student;");
StringBuffer sb1 = new StringBuffer("\n\n\n|姓名| 学号 | 年龄 |性别 |\n");
while (rs.next()) {
String str11 = new String("| "+rs.getString("name")+" | "+rs.getString("no")+" | "+rs.getString("age")+" | "+rs.getString("sex")+"|\n");
sb1.append(str11);
}
ta1.setText(sb1.toString());
right.validate();
}
catch (Exception e1) {
System.out.print("get data error!");
e1.printStackTrace();
}
});
//第三个按钮的监听器
JButton leftbt13 = new JButton("按照上方输入的学号删除一位学生的信息");
JTextField tf5= new JTextField();
leftbt13.addActionListener(c->{
//连接数据库
try {
Connection connect = DriverManager.getConnection(
"jdbc:mysql://localhost:3306/XUANKE","root","123");
Statement stmt = connect.createStatement();
String str = new String("delete from student where no='"+tf5.getText()+"';");
stmt.executeUpdate(str);
ResultSet rs = stmt.executeQuery("select * from student;");
StringBuffer sb1 = new StringBuffer("\n\n\n|姓名| 学号 | 年龄 |性别 |\n");
while (rs.next()) {
String str1 = new String("| "+rs.getString("name")+" | "+rs.getString("no")+" | "+rs.getString("age")+" | "+rs.getString("sex")+"|\n");
sb1.append(str1);
}
ta1.setText(sb1.toString());
right.validate();
}
catch (Exception e1) {
System.out.print("get data error!");
e1.printStackTrace();
}
});
left1.add(leftbt11);
left1.add(left11);
left1.add(leftbt12);
left1.add(tf5);
left1.add(leftbt13);
f2.add(left1);
f2.validate();
});
//以下为粘贴的内容
//student按钮的监听器
leftbt2.addActionListener(a->{
f2.remove(left);
JPanel left2 = new JPanel();
left2.setLayout(new GridLayout(5,1,10,10));
JButton leftbt11 = new JButton("查询所有课程的信息");
//第一个按钮的监听器
leftbt11.addActionListener(b->{
//连接数据库
try {
Connection connect = DriverManager.getConnection(
"jdbc:mysql://localhost:3306/XUANKE","root","123");
Statement stmt = connect.createStatement();
ResultSet rs = stmt.executeQuery("select * from course;");
StringBuffer sb1 = new StringBuffer("\n\n\n|课程名| 课程号 | 课程学分|\n");
while (rs.next()) {
String str1 = new String("| "+rs.getString("c_name")+" | "+rs.getString("c_no")+" | "+rs.getString("c_credit")+" | \n");
sb1.append(str1);
}
ta1.setText(sb1.toString());
right.validate();
}
catch (Exception e1) {
System.out.print("get data error!");
e1.printStackTrace();
}
});
//第二个按钮的监听器
JPanel left21= new JPanel();
left21.setLayout(new GridLayout(1,3,10,10));
JTextField tf1 = new JTextField();
JTextField tf2 = new JTextField();
JTextField tf3 = new JTextField();
left21.add(tf1);
left21.add(tf2);
left21.add(tf3);
// left2.add(left21);
JButton leftbt12 = new JButton("按照上方填写内容增加一门课程的信息,");
leftbt12.addActionListener(c->{
String str_tf1 = tf1.getText();
String str_tf2 = tf2.getText();
String str_tf3 = tf3.getText();
//连接数据库
try {
Connection connect = DriverManager.getConnection(
"jdbc:mysql://localhost:3306/XUANKE","root","123");
Statement stmt = connect.createStatement();
String str1 = new String("insert into course values('"+str_tf1+"','"+str_tf2+"','"+str_tf3+"');");
stmt.executeUpdate(str1);
ResultSet rs = stmt.executeQuery("select * from course;");
StringBuffer sb1 = new StringBuffer("\n\n\n|课程名| 课程号 | 课程学分 |\n");
while (rs.next()) {
String str11 = new String("| "+rs.getString("c_name")+" | "+rs.getString("c_no")+" | "+rs.getString("c_credit")+" |\n");
sb1.append(str11);
}
ta1.setText(sb1.toString());
right.validate();
}
catch (Exception e1) {
System.out.print("get data error!");
e1.printStackTrace();
}
});
//第三个按钮的监听器
JButton leftbt13 = new JButton("按照上方输入的课程号删除一门课程的信息");
JTextField tf4= new JTextField();
leftbt13.addActionListener(c->{
//连接数据库
try {
Connection connect = DriverManager.getConnection(
"jdbc:mysql://localhost:3306/XUANKE","root","123");
Statement stmt = connect.createStatement();
String str = new String("delete from course where c_no='"+tf4.getText()+"';");
stmt.executeUpdate(str);
ResultSet rs = stmt.executeQuery("select * from course;");
StringBuffer sb1 = new StringBuffer("\n\n\n|课程名| 课程号 | 课程学分 |\n");
while (rs.next()) {
String str1 = new String("| "+rs.getString("c_name")+" | "+rs.getString("c_no")+" | "+rs.getString("c_credit")+" |\n");
sb1.append(str1);
}
ta1.setText(sb1.toString());
right.validate();
}
catch (Exception e1) {
System.out.print("get data error!");
e1.printStackTrace();
}
});
left2.add(leftbt11);
left2.add(left21);
left2.add(leftbt12);
left2.add(tf4);
left2.add(leftbt13);
f2.add(left2);
f2.validate();
});
f2.add(left,BorderLayout.WEST);
f2.add(right,BorderLayout.EAST);
}else {
JDialog d2 = new JDialog(f, "提示",true);
d2.setSize(250, 100);
d2.setLocationRelativeTo(null);
d2.add(new JLabel("登陆失败,请重新输入用户名与密码"),BorderLayout.CENTER);
d2.setVisible(true);
}
});
}
}