在快到期末的时候老师叫我们写一个java的课程设计,并要求至少8个页面。由于在学习java中并不是很系统的学习,只是在上课时简单的听听,所以我对java还是很陌生。在写这个项目的时候参考书本及一些大佬的文章,理解如何连接MySQL数据库,终于用了一周的时间才完成这个简单的项目,由于个人能力不足,请各位大佬理解一下。
话不多说,请看图
登录界面
主界面
添加功能
删除功能
查询功能
修改功能
添加管理员、修改密码
部分源代码
登录界面代码
package Student;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
import javax.swing.border.MatteBorder;
public class Login extends JFrame {
static JTextField t1 ; //文本框
JTextField t2 ;
public Login() {
Container cp = getContentPane();
JButton b1 ,b2; //按钮
Label l1 , l2; //标签
t1 = new JTextField(15);
t2 =new JPasswordField(15);
b1 = new JButton("登陆");
b2 = new JButton("重置");
l1 = new Label("用户名: ");
l2 = new Label("密码 : ");
JPanel p1 = new JPanel();
JPanel p2 = new JPanel();
JPanel p3 = new JPanel();
JPanel p4 = new JPanel();
JPanel p5 = new JPanel();
p1.add(l1);
p1.add(t1);
p1.setBorder(new MatteBorder(1,1,1,1,Color.BLACK)); //创建具有指定 insets 和颜色的衬边边框
p1.setBackground(Color.YELLOW); //设置此组件的背景色。
p2.add(l2);
p2.add(t2);
p2.setBorder(new MatteBorder(1,1,1,1,Color.BLACK));
p2.setBackground(Color.YELLOW);
p3.add(b1);
p3.setBorder(new MatteBorder(1,1,1,1,Color.BLACK));
p3.setBackground(Color.yellow);
p4.add(b2);
p4.setBorder(new MatteBorder(1,1,1,1,Color.BLACK));
p4.setBackground(Color.yellow);
p5.add(p1);
p5.add(p2);
p5.add(p3);
p5.add(p4);
p5.setLayout(new FlowLayout(FlowLayout.CENTER,60,30)); //居中,水平距60,垂直距30
cp.add(p5);
//监视器
b1.addActionListener(new ActionListener (){
public void actionPerformed(ActionEvent e) {
if( DAO.DengLu(t1.getText(), t2.getText()) ){
//JOptionPane 有助于方便地弹出要求用户提供值或向其发出通知的标准对话框。
JOptionPane.showMessageDialog(t1,"登录成功"); //调出标题为 "Message" 的信息消息对话框
Operate h1 = new Operate();
dispose(); //释放由此 Window、其子组件及其拥有的所有子组件所使用的所有本机屏幕资源
}
else {
JOptionPane.showMessageDialog(null,"用户名或密码错误","错误",JOptionPane.ERROR_MESSAGE);
t1.setText("");
t2.setText("");
}
}
});
b2.addActionListener(new ActionListener (){
@Override
public void actionPerformed(ActionEvent e) {
t1.setText("");
t2.setText("");
t1.requestFocusInWindow(); //请求此 Component 获取输入焦点
}
});
}
}
主页面代码
package Student;
import java.awt.Color;
import java.awt.Container;
import java.awt.FlowLayout;
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.border.MatteBorder;
public class Operate extends JFrame{
public Operate() {
setBounds(600,300,340,400);
setVisible(true);
setTitle("学生信息管理系统");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setResizable(false); //窗口不可变
Container con = getContentPane();
JButton b1 = new JButton("添加");
JButton b2 = new JButton("删除");
JButton b3 = new JButton("查询");
JButton b4 = new JButton("修改");
JButton b5 = new JButton("系统");
JButton b6 = new JButton("退出");
JPanel p1 = new JPanel();
JPanel p2 = new JPanel();
JPanel p3 = new JPanel();
JPanel p4 = new JPanel();
JPanel p5 = new JPanel();
JPanel p6 = new JPanel();
JPanel p7 = new JPanel();
p1.add(b1);
p1.setBorder(new MatteBorder(1,1,1,1,Color.black));
p1.setBackground(Color.yellow );
b1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if(e.getSource() == b1) {
AddS o1 = new AddS(); //添加
dispose();
}
}
});
p2.add(b2);
p2.setBorder(new MatteBorder(1,1,1,1,Color.black));
p2.setBackground(Color.yellow );
b2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if(e.getSource() == b2) {
Delete d1 = new Delete(); //删除
dispose();
}
}
});
p3.add(b3);
p3.setBorder(new MatteBorder(1,1,1,1,Color.black));
p3.setBackground(Color.yellow );
b3.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if(e.getSource() == b3) {
Lookup l1 = new Lookup(); //查询
dispose();
}
}
});
p4.add(b4);
p4.setBorder(new MatteBorder(1,1,1,1,Color.black));
p4.setBackground(Color.yellow );
b4.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if(e.getSource() == b4) {
ChangeS c1 = new ChangeS(); //修改
dispose();
}
}
});
p5.add(b5);
p5.setBorder(new MatteBorder(1,1,1,1,Color.black));
p5.setBackground(Color.yellow );
b5.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if(e.getSource() == b5) {
XiTong x1 = new XiTong(); //系统
dispose();
}
}
});
p6.add(b6);
p6.setBorder(new MatteBorder(1,1,1,1,Color.black));
p6.setBackground(Color.yellow );
b6.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if(e.getSource() == b6) {
System.exit(0); //退出
dispose();
}
}
});
p7.add(p1);
p7.add(p2);
p7.add(p3);
p7.add(p4);
p7.add(p5);
p7.add(p6);
p7.setLayout(new FlowLayout(FlowLayout.CENTER, 50, 60));
con.add(p7);
}
}
添加—添加学生
package Student;
import java.awt.Color;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.MatteBorder;
public class AddS extends JFrame {
public AddS() {
setTitle("添加");
setBounds(600, 300, 300, 360);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setResizable(false);
JTextField t1,t2,t3,t4,t5;
JLabel l1,l2,l3,l4,l5;
JButton b1,b2;
Container con = getContentPane();
l1 = new JLabel("学号: ");
l2 = new JLabel("姓名: ");
l3 = new JLabel("年龄: ");
l4 = new JLabel("性别: ");
l5 = new JLabel("班级: ");
b1 = new JButton("确认");
b2 = new JButton("取消");
t1 = new JTextField(15);
t2 = new JTextField(15);
t3 = new JTextField(15);
t4 = new JTextField(15);
t5 = new JTextField(15);
JPanel p1 = new JPanel();
JPanel p2 = new JPanel();
JPanel p3 = new JPanel();
JPanel p4 = new JPanel();
JPanel p5 = new JPanel();
JPanel p6 = new JPanel();
JPanel p7 = new JPanel();
JPanel p8 = new JPanel();
p1.add(l1);
p1.add(t1);
p1.setBorder(new MatteBorder(1,1,1,1,Color.BLACK)); //创建具有指定 insets 和颜色的衬边边框
p1.setBackground(Color.YELLOW); //设置此组件的背景色。
p2.add(l2);
p2.add(t2);
p2.setBorder(new MatteBorder(1,1,1,1,Color.BLACK)); //创建具有指定 insets 和颜色的衬边边框
p2.setBackground(Color.YELLOW); //设置此组件的背景色。
p3.add(l3);
p3.add(t3);
p3.setBorder(new MatteBorder(1,1,1,1,Color.BLACK)); //创建具有指定 insets 和颜色的衬边边框
p3.setBackground(Color.YELLOW); //设置此组件的背景色。
p4.add(l4);
p4.add(t4);
p4.setBorder(new MatteBorder(1,1,1,1,Color.BLACK)); //创建具有指定 insets 和颜色的衬边边框
p4.setBackground(Color.YELLOW); //设置此组件的背景色。
p5.add(l5);
p5.add(t5);
p5.setBorder(new MatteBorder(1,1,1,1,Color.BLACK)); //创建具有指定 insets 和颜色的衬边边框
p5.setBackground(Color.YELLOW); //设置此组件的背景色。
p6.add(b1);
p6.setBorder(new MatteBorder(1, 1, 1, 1, Color.BLACK));
p6.setBackground(Color.YELLOW);
p7.add(b2);
p7.setBorder(new MatteBorder(1, 1, 1, 1, Color.BLACK));
p7.setBackground(Color.YELLOW);
p8.add(p1);
p8.add(p2);
p8.add(p3);
p8.add(p4);
p8.add(p5);
p8.add(p6);
p8.add(p7);
p8.setLayout(new FlowLayout(FlowLayout.CENTER,30,15)); //居中,水平距30,垂直距15
con.add(p8);
b1.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
List<Student> list1 = MysqlAll.all1();
String s1 = t1.getText();
String s2 = t2.getText();
String s3 = t3.getText();
String s4 = t4.getText();
String s5 = t5.getText();
Student s = new Student(s1,s2,s3,s4,s5);
for(Student st:list1) {
if(st.getId().equals(s1)) {
JOptionPane.showMessageDialog(null,"学号已存在","错误",JOptionPane.ERROR_MESSAGE);
return; //终止函数的执行,
}
}
if( DAO.IfNull(s1) == true || DAO.IfNull(s2) == true ) { //不能空值
JOptionPane.showMessageDialog(null,"学号或姓名为空","错误",JOptionPane.ERROR_MESSAGE);
}
else {
boolean res = DAO.AddInS(s);
if (res == true) {
JOptionPane.showMessageDialog(b1,"添加成功"); //消息对话框
t1.setText("");
t2.setText("");
t3.setText("");
t4.setText("");
t5.setText("");
}
else {
JOptionPane.showMessageDialog(null,"添加错误","错误",JOptionPane.ERROR_MESSAGE);
}
}
}
});
b2.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
Operate h1 = new Operate();
dispose(); //释放由此 Window、其子组件及其拥有的所有子组件所使用的所有本机屏幕资
}
});
}
}
添加—添加管理员
package Student;
import java.awt.Color;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.MatteBorder;
public class AddU extends JFrame {
JTextField t1,t2,t3,t4;
JLabel l1,l2,l3,l4;
JButton b1,b2;
public AddU() {
Container con = getContentPane();
l1 = new JLabel("用户: ");
l2 = new JLabel("密码: ");
l3 = new JLabel("性别: ");
l4 = new JLabel("电话: ");
b1 = new JButton("确认");
b2 = new JButton("取消");
t1 = new JTextField(15);
t2 = new JTextField(15);
t3 = new JTextField(15);
t4 = new JTextField(15);
JPanel p1 = new JPanel();
JPanel p2 = new JPanel();
JPanel p3 = new JPanel();
JPanel p4 = new JPanel();
JPanel p5 = new JPanel();
JPanel p6 = new JPanel();
JPanel p7 = new JPanel();
p1.add(l1);
p1.add(t1);
p1.setBorder(new MatteBorder(1,1,1,1,Color.BLACK)); //创建具有指定 insets 和颜色的衬边边框
p1.setBackground(Color.YELLOW); //设置此组件的背景色。
p2.add(l2);
p2.add(t2);
p2.setBorder(new MatteBorder(1,1,1,1,Color.BLACK)); //创建具有指定 insets 和颜色的衬边边框
p2.setBackground(Color.YELLOW); //设置此组件的背景色。
p3.add(l3);
p3.add(t3);
p3.setBorder(new MatteBorder(1,1,1,1,Color.BLACK)); //创建具有指定 insets 和颜色的衬边边框
p3.setBackground(Color.YELLOW); //设置此组件的背景色。
p4.add(l4);
p4.add(t4);
p4.setBorder(new MatteBorder(1,1,1,1,Color.BLACK)); //创建具有指定 insets 和颜色的衬边边框
p4.setBackground(Color.YELLOW); //设置此组件的背景色。
p5.add(b1);
p5.setBorder(new MatteBorder(1, 1, 1, 1, Color.BLACK));
p5.setBackground(Color.YELLOW);
p6.add(b2);
p6.setBorder(new MatteBorder(1, 1, 1, 1, Color.BLACK));
p6.setBackground(Color.YELLOW);
p7.add(p1);
p7.add(p2);
p7.add(p3);
p7.add(p4);
p7.add(p5);
p7.add(p6);
p7.setLayout(new FlowLayout(FlowLayout.CENTER,20,20)); //居中,水平距20,垂直距20
con.add(p7);
b1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
List<User> user1 = MysqlAll.all2();
String u1 = t1.getText();
String u2 = t2.getText();
String u3 = t3.getText();
String u4 = t4.getText();
User user2 = new User(u1,u2,u3,u4);
for(User user : user1) {
if(user.getUname().equals(u1)) {
JOptionPane.showMessageDialog(null,"用户已存在","错误",JOptionPane.ERROR_MESSAGE);
return; //终止函数的执行,
}
}
if( DAO.IfNull(u1) == true || DAO.IfNull(u2) == true ) { //不能空值
JOptionPane.showMessageDialog(null,"用户或密码不能为空","错误",JOptionPane.ERROR_MESSAGE);
}
else {
boolean res = DAO.AddInU(user2);
if (res == true) {
JOptionPane.showMessageDialog(b1,"添加成功"); //消息对话框
t1.setText("");
t2.setText("");
t3.setText("");
t4.setText("");
}
else {
JOptionPane.showMessageDialog(null,"添加错误","错误",JOptionPane.ERROR_MESSAGE);
}
}
}
});
b2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
XiTong x1 = new XiTong();
dispose();
}
});
}
}
删除—输入学号查找
package Student;
import java.awt.Color;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.MatteBorder;
public class Delete extends JFrame {
static JTextField t1;
public Delete () {
setTitle("删除");
setBounds(600, 300, 300, 360);
setVisible(true);
setResizable(false);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JLabel l1;
JButton b1,b2;
t1 = new JTextField(15);
l1 = new JLabel("输入学号:");
b1 = new JButton("确认");
b2 = new JButton("取消");
Container con = getContentPane();
JPanel p1 = new JPanel();
JPanel p2 = new JPanel();
JPanel p3 = new JPanel();
JPanel p4 = new JPanel();
JPanel p5 = new JPanel();
JPanel p6 = new JPanel();
JPanel p7 = new JPanel();
p1.add(l1);
p1.add(t1);
p1.setBorder(new MatteBorder(1,1,1,1,Color.BLACK)); //创建具有指定 insets 和颜色的衬边边框
p1.setBackground(Color.YELLOW); //设置此组件的背景色。
p2.add(p1);
p3.add(b1);
p3.setBorder(new MatteBorder(1,1,1,1,Color.BLACK));
p3.setBackground(Color.yellow);
p4.add(p3);
p5.add(b2);
p5.setBorder(new MatteBorder(1,1,1,1,Color.BLACK));
p5.setBackground(Color.yellow);
p6.add(p5);
p7.setLayout(new FlowLayout(FlowLayout.CENTER,40,80)); //居中,水平距40垂直距80
p7.add(p2);
p7.add(p4);
p7.add(p6);
con.add(p7);
b1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if(e.getSource() == b1) {
if( DAO.LookId(t1.getText()).size() == 0 ) {
JOptionPane.showMessageDialog(null, "学号不存在","错误",JOptionPane.ERROR_MESSAGE);
}
else {
DeleteShow ds = new DeleteShow();
dispose();
}
}
}
});
b2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
Operate h1 = new Operate();
dispose(); //释放由此 Window、其子组件及其拥有的所有子组件所使用的所有本机屏幕资
}
});
}
}
删除—是否删除
package Student;
import java.awt.Color;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.border.MatteBorder;
public class DeleteShow extends JFrame{
JLabel l1,l2,l3,l4,l5,l6;
public DeleteShow() {
setTitle("删除该学生信息");
setBounds(800, 200, 350, 580);
setVisible(true);
setResizable(false);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container con = getContentPane();
List<Student> list = DAO.LookId(Delete.t1.getText());
JButton b1 = new JButton("是");
JButton b2 = new JButton("否");
l6 = new JLabel("确定要删除该学生的信息吗 ? ");
for(Student st : list) {
l1 = new JLabel("学号: "+st.getId());
l2 = new JLabel("姓名: "+st.getName());
l3 = new JLabel("年龄: "+st.getAge());
l4 = new JLabel("性别: "+st.getSex());
l5 = new JLabel("班级: "+st.getBanji());
}
JPanel p1 = new JPanel();
JPanel p2 = new JPanel();
JPanel p3 = new JPanel();
JPanel p4 = new JPanel();
JPanel p5 = new JPanel();
JPanel p6 = new JPanel();
JPanel p7 = new JPanel();
JPanel p8 = new JPanel();
JPanel p9 = new JPanel();
JPanel p0 = new JPanel();
p1.add(l1);
p1.setBorder(new MatteBorder(1,1,1,1,Color.BLACK)); //创建具有指定 insets 和颜色的衬边边框
p1.setBackground(Color.YELLOW); //设置此组件的背景色。
p2.add(l2);
p2.setBorder(new MatteBorder(1,1,1,1,Color.BLACK)); //创建具有指定 insets 和颜色的衬边边框
p2.setBackground(Color.YELLOW); //设置此组件的背景色。
p3.add(l3);
p3.setBorder(new MatteBorder(1,1,1,1,Color.BLACK)); //创建具有指定 insets 和颜色的衬边边框
p3.setBackground(Color.YELLOW); //设置此组件的背景色。
p4.add(l4);
p4.setBorder(new MatteBorder(1,1,1,1,Color.BLACK)); //创建具有指定 insets 和颜色的衬边边框
p4.setBackground(Color.YELLOW); //设置此组件的背景色。
p5.add(l5);
p5.setBorder(new MatteBorder(1,1,1,1,Color.BLACK)); //创建具有指定 insets 和颜色的衬边边框
p5.setBackground(Color.YELLOW); //设置此组件的背景色。
p6.add(l6);
p6.setBorder(new MatteBorder(1,1,1,1,Color.BLACK)); //创建具有指定 insets 和颜色的衬边边框
p6.setBackground(Color.YELLOW); //设置此组件的背景色。
p7.add(b1);
p7.setBorder(new MatteBorder(1,1,1,1,Color.BLACK)); //创建具有指定 insets 和颜色的衬边边框
p7.setBackground(Color.RED); //设置此组件的背景色。
p8.add(b2);
p8.setBorder(new MatteBorder(1,1,1,1,Color.BLACK)); //创建具有指定 insets 和颜色的衬边边框
p8.setBackground(Color.white); //设置此组件的背景色。
p9.add(p1);
p9.add(p2);
p9.add(p3);
p9.add(p4);
p9.add(p5);
p9.add(p6);
p9.add(p7);
p9.add(p8);
p9.setLayout(new FlowLayout(FlowLayout.CENTER,150,30)); //居中,水平距150垂直距30
add(p9);
b1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if( DAO.DeleteS(Delete.t1.getText()) ) {
JOptionPane.showMessageDialog(null, "删除成功");
Delete d1 = new Delete();
dispose();
}
else
JOptionPane.showMessageDialog(null, "删除失败","错误",JOptionPane.ERROR_MESSAGE);
}
});
b2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if(e.getSource() == b2) {
dispose();
Delete d1 = new Delete();
dispose();
}
}
});
}
}
连接数据库
package Student;
import java.sql.*;
public class DataBase {
static Connection con = null;
//连接student数据库
public static Connection getConnection1(String u,String p) {
String uri = "jdbc:mysql://localhost:3306/xinxi?useSSL=true&characterEncoding-utf-8";
try {
//加载驱动
Class.forName("com.mysql.jdbc.Driver");
//获取连接对象
con = DriverManager.getConnection(uri,u, p);
}
catch (ClassNotFoundException | SQLException e) {
System.out.println("连接失败!!!");
e.printStackTrace();
}
return con;
}
//连接user数据库
public static Connection getConnection2(String u,String p) {
String uri = "jdbc:mysql://localhost:3306/xinxi?useSSL=true&characterEncoding-utf-8";
try {
//加载驱动
Class.forName("com.mysql.jdbc.Driver");
//获取连接对象
con = DriverManager.getConnection(uri,u, p);
}
catch (ClassNotFoundException | SQLException e) {
System.out.println("连接失败!!!");
e.printStackTrace();
}
return con;
}
}
源码下载:
链接:https://pan.baidu.com/s/1D4KNMvPzymcDpEOp6-ZdKQ
提取码:an01