实现简单的学生管理系统,适用于新手学习
学生管理系统演示
要实现管理数据,首先需要令Eclipse连接上数据库,即JDBC技术。首先需要导入电脑中安装好的Mysql的jar包,具体往项目中添加jar包的方式如下
考虑到每个人设置的Mysql登录密码不一样,为了使学生管理系统在每个电脑都能够运行,我们在进入系统时候需要手动输入Mysql的账号与密码,界面图如下
可以看到,只需要对"连接"按钮进行事件绑定即可,具体做法是令其监听两个输入框,当账号密码都正确时会得到一个连接Connection con
,后续访问数据库都需要依靠这个连接;若账号密码与本地Mysql不匹配,则抛出异常,出现"账号密码错误"
的提示框。具体的事件实现会在后边讲,这里主要说一下界面组成
注册账号
与忘记密码
的按钮,这两个功能是比较常见的,也是必要的,因为登录有学生与管理员之分,所以注册也自然有学生和管理员之分。根据实际,只允许管理员注册账号,这是两者不同权限之一student_system
数据库用来存放所有与学生管理系统相关的表内容(数据库以及表的创建语句会在后边给出)
admin
和stu
用来分别存放管理员的账号密码以及学生的账号密码,为了便于数据处理,我将所有的类型都设置为char
,表结构如下information
表来维护;而成绩则放在grade
表create database student_system;
set names gbk;
create table admin(
num char(15) primary key not null,
password char(20) not null
)engine myisam charset utf8;
create table stu(
num char(15) primary key not null,
password char(20) not null
)engine myisam charset utf8;
create table information(
num char(15) primary key not null,
name char(20) not null,
tele char(20) not null,
address char(30) not null
)engine myisam charset utf8;
create table grade(
num char(15) primary key not null,
name char(10) not null,
chinese_grade char(10) not null default -1,
math_grade char(10) not null default -1,
english_grade char(10) not null default -1,
);engine myisam charset utf8;
Java的图形化可以在Eclipse中安装WindowsBuilder插件进行操作,效率会高很多,我本人也是最近才发现了这个宝贝东西,先前纯手写太痛苦了,不知道怎么操作的可看下面这个↓。
eclipse安装WindowBuilder插件最新教程及问题解决方法
在实现过程中为了使界面更加美观,可以添加在JFrame上添加图片背景,也可以根据需要对按钮添加背景,我在放图片的时候发现大小不好调整且不知道怎么放到底层,不过通过看大佬文章最终还是解决了,为了让各位少走弯路,我将主要代码贴进来,自己琢磨一下,如果还有不清楚的地方可以再去看看大佬们写的关于添加背景图片的文章。
ImageIcon icon=new ImageIcon("src\\学生管理系统\\img\\校门.jpg");//背景图
Image h = icon.getImage().getScaledInstance(组件名称.getWidth(), 组件名称.getHeight(),
icon.getImage().SCALE_DEFAULT); //设置图片大小
icon=new ImageIcon(h); //将设置好大小的图片重新分配图片图标对象
JLabel morning = new JLabel(icon);//往一个标签中加入图片
morning.setBounds(0, 0, icon.getIconWidth(),icon.getIconHeight());//设置标签位置大小为图片大小
(jframe.)getLayeredPane().add(morning, Integer.valueOf(Integer.MIN_VALUE));//标签添加到第二层面板
JPanel bg=(JPanel) getContentPane();//后边要加什么东西到背景中只需要加入这个底层面板中即可
bg.setOpaque(false); //底层面板透明,处在最底层的图片才看得见
Attribute
对象,对象内封装了界面的所有组件,当需要对组件进行操作时(如获取文本框的值),只需要对Attribute
内的对象进行操作即可,每个事件都拥有这个对象,因此操作起来会十分方便,同时代码量也大大减少tool
中,如sql语句查询/更新
需要频繁使用,因此可以将其写入工具包的工具类中用不同的包放置不同功能的类,如mysql连接相关的类放入
mysql_join
包中;图形化相关的类放入gui
包中;事件相关的类放入event
包中;event
包内与管理员功能相关的类放入admin_function
包中……这样的好处之一是当发现BUG时能够很快定位到问题发生处,并进行修复与维护
首先是数据库问题,无法接通是因为导出来的jar包运行时找不到mysql的jar包,因此数据库相关类就失效了,自然就连接不上数据库。我们解决的关键是让其能够认识mysql的jar包
,因此我们需要将mysql的jar
包一起封装进项目中导出,具体操作方法如下:
接着是没有主属性清单的问题,这个问题的本质在于jar
包运行时不知道那个类中有主函数,因此我们让他知道就行,依然是对MANIFEST.MF
进行操作。我的主类为路径src下的stu_system包下的Start.java
,所以我的主类全名为:stu_system.Start
最后是图片显示问题,这个是图片路径问题,因为脱离了Exclipse环境后的JAR
包在运行时只认识主类,所以我们的所有路径都应该围绕主类展开才能够被识别,Java中有提供获得当前类路径的方法:类名.class.getResource(字符串)
如我的图片img包
和主类Start.java
处于同一级,因此对于img
里装着的校门.jpg
可以这样取得
Start.class.getResource("img/校门.jpg")
package stu_system;
import java.awt.EventQueue;
import org.junit.Test;
import stu_system.gui.MysqlPanel;
public class Start {
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
MysqlPanel frame = new MysqlPanel();
frame.EventAdd();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
}
package stu_system.gui;
import java.awt.BorderLayout;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.plaf.FontUIResource;
import com.mysql.jdbc.Connection;
import stu_system.Start;
import stu_system.event.admin_function.AccountClick;
import stu_system.event.admin_function.Attribute;
import stu_system.event.admin_function.ButtonClickCarton;
import stu_system.event.admin_function.DisposeClick;
import stu_system.event.admin_function.InsertClick;
import stu_system.event.admin_function.SearchClick;
import stu_system.event.admin_function.UpdateGradeClick;
import stu_system.event.admin_function.UpdateInfoClick;
import stu_system.event.admin_function.UpdatePasswordClick;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import java.awt.Font;
import java.awt.Image;
import java.awt.TextArea;
import java.awt.event.ActionListener;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.awt.event.ActionEvent;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.UIManager;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import java.awt.Color;
import java.awt.SystemColor;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
public class AdminFunctionPanel {
private JPanel contentPane = new JPanel();
private JFrame jf = new JFrame(); // 当前框架
private JTextArea showArea = new JTextArea(); // 显示区域
private JLabel Menu = new JLabel("\u663E\u793A\uFF1A"); // 菜单条目
private JButton newStuButton = new JButton("\u65B0\u589E\u5B66\u751F"); // 新增学生按钮
private JButton searchInfoButton = new JButton("\u67E5\u8BE2\u4FE1\u606F"); // 查询信息按钮
private JButton insertGradeButton = new JButton("\u5F55\u5165\u6210\u7EE9"); // 插入成绩按钮
private JButton searchGradeButton = new JButton("\u67E5\u8BE2\u6210\u7EE9"); // 查询成绩按钮
private JButton updatePasswordButton = new JButton("\u4FEE\u6539\u5BC6\u7801"); // 修改密码按钮
private JButton disposeAccountButton = new JButton("\u6CE8\u9500\u8D26\u6237"); // 注销账户按钮
private JButton updateInfoButton = new JButton("\u4FEE\u6539\u4FE1\u606F"); // 修改信息按钮
private JButton returnButton = new JButton("\u8FD4\u56DE"); // 返回上一界面按钮
private JButton accountSetButton = new JButton("\u8D26\u53F7\u5E93"); // 查看所有账户按钮
private JButton updateGradeButton = new JButton("\u4FEE\u6539\u6210\u7EE9");
public AdminFunctionPanel(Connection con, String myNum) {
Attribute attr = new Attribute(con, contentPane, jf, showArea, Menu, newStuButton, searchInfoButton,
insertGradeButton, searchGradeButton, updatePasswordButton, disposeAccountButton, updateInfoButton,
returnButton, accountSetButton, updateGradeButton, myNum);
jf.setTitle("\u7BA1\u7406\u5458\u529F\u80FD\u754C\u9762");
jf.setResizable(false);
// 设置按钮字体大小
UIManager.put("OptionPane.buttonFont", new FontUIResource(new Font("楷体", Font.BOLD, 25)));
// 设置文本字体大小
UIManager.put("OptionPane.messageFont", new FontUIResource(new Font("楷体", Font.BOLD, 30)));
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jf.setBounds(100, 100, 1150, 695);
jf.setContentPane(contentPane);
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
contentPane.setLayout(null);
JPanel panel = new JPanel();
panel.setOpaque(false);
panel.setBorder(null);
panel.setBounds(226, 89, 874, 490);
contentPane.add(panel);
panel.setLayout(null);
JScrollPane scrollPane = new JScrollPane();
scrollPane.setOpaque(false);
scrollPane.getViewport().setOpaque(false); // 滚动面板设置透明还需要多这一条
scrollPane.setBorder(null);
scrollPane.setBounds(0, 0, 874, 490);
panel.add(scrollPane); //面板放滚动面板
showArea.setLineWrap(true);
scrollPane.setViewportView(showArea);
showArea.setFont(new Font("楷体", Font.BOLD, 28));
Menu.setForeground(UIManager.getColor("Button.highlight"));
Menu.setFont(new Font("楷体", Font.BOLD, 28));
Menu.setBounds(253, 33, 847, 59);
contentPane.add(Menu);
showArea.setForeground(new Color(224, 255, 255));
showArea.setOpaque(false);
showArea.setEditable(false);
newStuButton.addMouseListener(new ButtonClickCarton(attr, 0));
newStuButton.addActionListener(new InsertClick(attr, 0));
newStuButton.setFont(new Font("楷体", Font.PLAIN, 28));
newStuButton.setBounds(29, 84, 182, 47);
contentPane.add(newStuButton);
searchInfoButton.addMouseListener(new ButtonClickCarton(attr, 1));
searchInfoButton.addActionListener(new SearchClick(attr, 1));
searchInfoButton.setFont(new Font("楷体", Font.PLAIN, 28));
searchInfoButton.setBounds(29, 146, 182, 47);
contentPane.add(searchInfoButton);
insertGradeButton.addMouseListener(new ButtonClickCarton(attr, 2));
insertGradeButton.addActionListener(new InsertClick(attr, 1));
insertGradeButton.setFont(new Font("楷体", Font.PLAIN, 28));
insertGradeButton.setBounds(29, 208, 182, 47);
contentPane.add(insertGradeButton);
searchGradeButton.addMouseListener(new ButtonClickCarton(attr, 3));
searchGradeButton.addActionListener(new SearchClick(attr, 0));
searchGradeButton.setFont(new Font("楷体", Font.PLAIN, 28));
searchGradeButton.setBounds(29, 270, 182, 47);
contentPane.add(searchGradeButton);
updatePasswordButton.addMouseListener(new ButtonClickCarton(attr, 4));
updatePasswordButton.addActionListener(new UpdatePasswordClick(attr));
updatePasswordButton.setFont(new Font("楷体", Font.PLAIN, 28));
updatePasswordButton.setBounds(29, 518, 182, 47);
contentPane.add(updatePasswordButton);
disposeAccountButton.addMouseListener(new ButtonClickCarton(attr, 5));
disposeAccountButton.addActionListener(new DisposeClick(attr));
disposeAccountButton.setFont(new Font("楷体", Font.PLAIN, 28));
disposeAccountButton.setBounds(29, 456, 182, 47);
contentPane.add(disposeAccountButton);
updateGradeButton.addMouseListener(new ButtonClickCarton(attr, 9));
updateGradeButton.addActionListener(new UpdateGradeClick(attr));
updateGradeButton.setFont(new Font("楷体", Font.PLAIN, 28));
updateGradeButton.setBounds(29, 332, 182, 47);
contentPane.add(updateGradeButton);
updateInfoButton.addMouseListener(new ButtonClickCarton(attr, 6));
updateInfoButton.addActionListener(new UpdateInfoClick(attr));
updateInfoButton.setFont(new Font("楷体", Font.PLAIN, 28));
updateInfoButton.setBounds(29, 394, 182, 47);
contentPane.add(updateInfoButton);
accountSetButton.addMouseListener(new ButtonClickCarton(attr, 8));
accountSetButton.addActionListener(new AccountClick(attr));
accountSetButton.setFont(new Font("楷体", Font.PLAIN, 28));
accountSetButton.setBounds(29, 22, 182, 47);
contentPane.add(accountSetButton);
returnButton.addMouseListener(new ButtonClickCarton(attr, 7));
returnButton.addActionListener(new InsertClick(attr, 2));
returnButton.setFont(new Font("楷体", Font.PLAIN, 28));
returnButton.setBounds(29, 580, 182, 47);
contentPane.add(returnButton);
ImageIcon icon = new ImageIcon(Start.class.getResource("img/校门.jpg"));// 背景图
Image h = icon.getImage().getScaledInstance(jf.getWidth(), jf.getHeight(), icon.getImage().SCALE_DEFAULT);
icon = new ImageIcon(h); // 将设置好大小的图片重新分配图片图标对象
JLabel morning = new JLabel(icon);// 往一个标签中加入图片
morning.setBounds(0, 0, icon.getIconWidth(), icon.getIconHeight());// 设置标签位置大小为图片大小
jf.getLayeredPane().add(morning, Integer.valueOf(Integer.MIN_VALUE));// 标签添加到第二层面板
JPanel bg = (JPanel) jf.getContentPane();
bg.setOpaque(false); // 底层面板透明,处在最底层的图片才看得见
jf.setVisible(true);
}
}
package stu_system.gui;
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import com.mysql.jdbc.Connection;
import stu_system.event.admin_function.grade_insert.Attribute;
import stu_system.event.admin_function.grade_insert.InsertGradeClick;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import java.awt.Font;
import javax.swing.JTextField;
import java.awt.event.ActionListener;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.awt.event.ActionEvent;
public class InsertGradePanel {
private JDialog jd = null;
private JTextField chineseField = new JTextField();
private JTextField mathField = new JTextField();
private JTextField englishField = new JTextField();
private JTextField nameField = new JTextField();
private JTextField numField = new JTextField();
private JButton affirmButton = new JButton("\u5F55\u5165");
public InsertGradePanel(JFrame jf, Connection con) {
jd = new JDialog(jf, true);
Attribute attr = new Attribute(con, jd, chineseField, mathField, englishField, nameField, numField,
affirmButton);
jd.setResizable(false);
jd.setTitle("\u6210\u7EE9\u5F55\u5165");
jd.setBounds(100, 100, 826, 706);
jd.getContentPane().setLayout(null);
chineseField.setFont(new Font("楷体", Font.PLAIN, 28));
chineseField.setBounds(354, 339, 122, 41);
jd.getContentPane().add(chineseField);
chineseField.setColumns(10);
mathField.setFont(new Font("楷体", Font.PLAIN, 28));
mathField.setColumns(10);
mathField.setBounds(354, 414, 122, 41);
jd.getContentPane().add(mathField);
englishField.setFont(new Font("楷体", Font.PLAIN, 28));
englishField.setColumns(10);
englishField.setBounds(354, 486, 122, 41);
jd.getContentPane().add(englishField);
affirmButton.addActionListener(new InsertGradeClick(attr));
affirmButton.setFont(new Font("楷体", Font.BOLD, 28));
affirmButton.setBounds(578, 303, 131, 113);
jd.getContentPane().add(affirmButton);
nameField.setFont(new Font("楷体", Font.PLAIN, 28));
nameField.setColumns(10);
nameField.setBounds(354, 265, 122, 41);
jd.getContentPane().add(nameField);
numField.setFont(new Font("楷体", Font.PLAIN, 28));
numField.setColumns(10);
numField.setBounds(354, 196, 122, 41);
jd.getContentPane().add(numField);
JLabel chineseGradeLabel = new JLabel("\u8BED\u6587\u6210\u7EE9\uFF1A");
chineseGradeLabel.setFont(new Font("楷体", Font.BOLD, 32));
chineseGradeLabel.setBounds(169, 322, 170, 71);
jd.getContentPane().add(chineseGradeLabel);
JLabel mathGradeLabel = new JLabel("\u6570\u5B66\u6210\u7EE9\uFF1A");
mathGradeLabel.setFont(new Font("楷体", Font.BOLD, 32));
mathGradeLabel.setBounds(169, 397, 170, 71);
jd.getContentPane().add(mathGradeLabel);
JLabel englishGradeLabel = new JLabel("\u82F1\u8BED\u6210\u7EE9\uFF1A");
englishGradeLabel.setFont(new Font("楷体", Font.BOLD, 32));
englishGradeLabel.setBounds(169, 469, 170, 71);
jd.getContentPane().add(englishGradeLabel);
JLabel TitleLabel = new JLabel("\u6210\u7EE9\u5F55\u5165");
TitleLabel.setFont(new Font("楷体", Font.BOLD | Font.ITALIC, 40));
TitleLabel.setBounds(327, 58, 195, 49);
jd.getContentPane().add(TitleLabel);
JLabel nameLabel = new JLabel("\u59D3\u540D\uFF1A");
nameLabel.setFont(new Font("楷体", Font.BOLD, 32));
nameLabel.setBounds(237, 248, 102, 71);
jd.getContentPane().add(nameLabel);
JLabel lastTwoNumLabel = new JLabel("\u5B66\u53F7\u540E\u4E24\u4F4D\uFF1A");
lastTwoNumLabel.setFont(new Font("楷体", Font.BOLD, 32));
lastTwoNumLabel.setBounds(135, 191, 215, 59);
jd.getContentPane().add(lastTwoNumLabel);
jd.setVisible(true);
}
}
package stu_system.gui;
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.plaf.FontUIResource;
import com.mysql.jdbc.Connection;
import stu_system.event.admin_function.info_insert.Attribute;
import stu_system.event.admin_function.info_insert.InsertStuClick;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import java.awt.Font;
import javax.swing.JTextField;
import javax.swing.UIManager;
import java.awt.event.ActionListener;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.awt.event.ActionEvent;
public class InsertStuPanel {
private JDialog jd = null;
private JTextField numField = new JTextField();
private JTextField nameField = new JTextField();
private JTextField teleField = new JTextField();
private JTextField addrField = new JTextField();
private JButton AffirmClick = new JButton("\u786E\u8BA4");
public InsertStuPanel(JFrame jf, Connection con) {
jd = new JDialog(jf, true);
Attribute attr = new Attribute(con, jd, numField, nameField, teleField, addrField);
// 设置按钮字体大小
UIManager.put("OptionPane.buttonFont", new FontUIResource(new Font("楷体", Font.BOLD, 25)));
// 设置文本字体大小
UIManager.put("OptionPane.messageFont", new FontUIResource(new Font("楷体", Font.BOLD, 30)));
jd.setTitle("\u8F93\u5165\u57FA\u672C\u4FE1\u606F");
jd.setBounds(100, 100, 699, 590);
jd.getContentPane().setLayout(null);
JLabel numLabel = new JLabel("\u5B66\u53F7\uFF1A");
numLabel.setFont(new Font("楷体", Font.PLAIN, 28));
numLabel.setBounds(121, 93, 84, 39);
jd.getContentPane().add(numLabel);
JLabel nameLabel = new JLabel("\u59D3\u540D\uFF1A");
nameLabel.setFont(new Font("楷体", Font.PLAIN, 28));
nameLabel.setBounds(121, 169, 84, 39);
jd.getContentPane().add(nameLabel);
JLabel teleLabel = new JLabel("\u7535\u8BDD\uFF1A");
teleLabel.setFont(new Font("楷体", Font.PLAIN, 28));
teleLabel.setBounds(121, 245, 84, 39);
jd.getContentPane().add(teleLabel);
JLabel addrLabel = new JLabel("\u5730\u5740\uFF1A");
addrLabel.setFont(new Font("楷体", Font.PLAIN, 28));
addrLabel.setBounds(121, 324, 84, 39);
jd.getContentPane().add(addrLabel);
numField.setText("18140809032");
numField.setFont(new Font("楷体", Font.PLAIN, 24));
numField.setBounds(222, 96, 336, 36);
jd.getContentPane().add(numField);
numField.setColumns(10);
nameField.setFont(new Font("楷体", Font.PLAIN, 24));
nameField.setColumns(10);
nameField.setBounds(222, 169, 336, 36);
jd.getContentPane().add(nameField);
teleField.setFont(new Font("楷体", Font.PLAIN, 24));
teleField.setColumns(10);
teleField.setBounds(220, 245, 336, 36);
jd.getContentPane().add(teleField);
addrField.setFont(new Font("楷体", Font.PLAIN, 24));
addrField.setColumns(10);
addrField.setBounds(220, 324, 336, 36);
jd.getContentPane().add(addrField);
AffirmClick.addActionListener(new InsertStuClick(attr));
AffirmClick.setFont(new Font("楷体", Font.PLAIN, 28));
AffirmClick.setBounds(303, 403, 141, 65);
jd.getContentPane().add(AffirmClick);
jd.setResizable(false);
jd.setVisible(true);
}
}
package stu_system.gui;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JTextField;
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import javax.swing.JButton;
import javax.swing.JDialog;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.awt.event.ActionEvent;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import java.awt.Font;
import java.awt.Image;
import javax.swing.SwingConstants;
import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import java.awt.Color;
import javax.swing.UIManager;
import javax.swing.plaf.FontUIResource;
import stu_system.Start;
import stu_system.event.login_panel.AdminClick;
import stu_system.event.login_panel.Attribute;
import stu_system.event.login_panel.FogetClick;
import stu_system.event.login_panel.IdentityButtonCarton;
import stu_system.event.login_panel.LoginClick;
import stu_system.event.login_panel.ReIdentityClick;
import stu_system.event.login_panel.RegisterClick;
import stu_system.event.login_panel.StuClick;
import java.sql.Connection;
import java.awt.Toolkit;
import javax.swing.JPanel;
import javax.swing.JLayeredPane;
import java.awt.event.FocusAdapter;
import java.awt.event.FocusEvent;
import java.awt.event.MouseMotionAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
public class LoginPanel {
private JFrame frame = new JFrame();
private JTextField accountField = new JTextField();
private JTextField passwordField = new JTextField();
private JLabel attention = new JLabel("");
private JButton adminButton = new JButton("");
private JButton stuButton = new JButton("");
private JLabel adminName = new JLabel("\u7BA1\u7406\u5458");
private JLabel stuName = new JLabel("\u5B66\u751F");
private JButton identityButton = new JButton("\u91CD\u9009\u8EAB\u4EFD");
private JButton loginButton = new JButton("\u767B\u5F55");
private JButton forgetButton = new JButton("\u5FD8\u8BB0\u5BC6\u7801");
private JButton registerButton = new JButton("\u6CE8\u518C\u8D26\u53F7");
public LoginPanel(Connection con) {
Attribute attr = new Attribute(con, frame, accountField, passwordField, attention, adminButton, stuButton,
adminName, stuName, identityButton, loginButton, forgetButton, registerButton);
// 设置按钮字体大小
UIManager.put("OptionPane.buttonFont", new FontUIResource(new Font("楷体", Font.BOLD, 25)));
// 设置文本字体大小
UIManager.put("OptionPane.messageFont", new FontUIResource(new Font("楷体", Font.BOLD, 30)));
frame.setResizable(false);
frame.setTitle("\u767B\u5F55");
frame.setBounds(100, 100, 884, 666);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);
JLabel lblNewLabel = new JLabel("\u8D26\u53F7\uFF1A");
lblNewLabel.setBackground(new Color(240, 255, 255));
lblNewLabel.setForeground(new Color(240, 255, 255));
lblNewLabel.setFont(new Font("楷体", Font.BOLD, 30));
lblNewLabel.setBounds(196, 134, 119, 32);
frame.getContentPane().add(lblNewLabel);
JLabel label = new JLabel("\u5BC6\u7801\uFF1A");
label.setBackground(new Color(240, 255, 255));
label.setForeground(new Color(240, 255, 255));
label.setFont(new Font("楷体", Font.BOLD, 30));
label.setBounds(196, 233, 106, 32);
frame.getContentPane().add(label);
JLabel label_1 = new JLabel("\u5B66\u751F\u7BA1\u7406\u7CFB\u7EDF");
label_1.setForeground(Color.WHITE);
label_1.setIcon(null);
label_1.setHorizontalAlignment(SwingConstants.CENTER);
label_1.setFont(new Font("楷体", Font.BOLD, 50));
label_1.setBounds(251, 15, 427, 91);
frame.getContentPane().add(label_1);
accountField.setFont(new Font("楷体", Font.PLAIN, 25));
accountField.setBounds(295, 135, 325, 34);
accountField.setColumns(10);
frame.getContentPane().add(accountField);
passwordField.setFont(new Font("楷体", Font.PLAIN, 25));
passwordField.setColumns(10);
passwordField.setBounds(295, 234, 325, 34);
frame.getContentPane().add(passwordField);
ImageIcon adminButtonImg = new ImageIcon(Start.class.getResource("img/Admin.png"));
adminButton.setBackground(new Color(255, 250, 205));
adminButton.setFont(new Font("宋体", Font.PLAIN, 10));
adminButton.addActionListener(new AdminClick(attr));
adminButton.setBounds(215, 421, 137, 110);
adminButton.addMouseListener(new IdentityButtonCarton(attr, true));
Image temp = adminButtonImg.getImage().getScaledInstance(adminButton.getWidth(), adminButton.getHeight(),
adminButtonImg.getImage().SCALE_DEFAULT);
adminButton.setIcon(new ImageIcon(temp));
adminButton.setContentAreaFilled(false); // 设置按钮透明
frame.getContentPane().add(adminButton);
adminName.setForeground(new Color(240, 255, 255));
adminName.setFont(new Font("楷体", Font.PLAIN, 28));
adminName.setHorizontalAlignment(SwingConstants.CENTER);
adminName.setBounds(240, 528, 89, 49);
frame.getContentPane().add(adminName);
stuButton.addActionListener(new StuClick(attr));
stuButton.setBounds(510, 410, 131, 110);
stuButton.addMouseListener(new IdentityButtonCarton(attr, false));
ImageIcon buttonImg2 = new ImageIcon(Start.class.getResource("img/Stu.png"));
Image temp2 = buttonImg2.getImage().getScaledInstance(stuButton.getWidth(), stuButton.getHeight(),
buttonImg2.getImage().SCALE_DEFAULT);
stuButton.setIcon(new ImageIcon(temp2));
stuButton.setContentAreaFilled(false);
frame.getContentPane().add(stuButton);
stuName.setForeground(new Color(245, 245, 245));
stuName.setHorizontalAlignment(SwingConstants.CENTER);
stuName.setFont(new Font("楷体", Font.PLAIN, 28));
stuName.setBounds(531, 528, 89, 49);
frame.getContentPane().add(stuName);
attention.setForeground(Color.WHITE);
attention.setBackground(Color.WHITE);
attention.setHorizontalAlignment(SwingConstants.CENTER);
attention.setFont(new Font("楷体", Font.PLAIN, 35));
attention.setBounds(117, 423, 719, 119);
frame.getContentPane().add(attention);
loginButton.setBackground(new Color(255, 255, 255));
loginButton.setForeground(Color.BLACK);
loginButton.setFont(new Font("楷体", Font.BOLD, 40));
loginButton.addActionListener(new LoginClick(attr));
loginButton.setBounds(361, 317, 148, 49);
frame.getContentPane().add(loginButton);
registerButton.setBackground(new Color(245, 245, 245));
registerButton.addActionListener(new RegisterClick(attr));
registerButton.setFont(new Font("楷体", Font.BOLD, 27));
registerButton.setBounds(672, 131, 148, 41);
frame.getContentPane().add(registerButton);
forgetButton.setBackground(new Color(245, 245, 245));
forgetButton.addActionListener(new FogetClick(attr));
forgetButton.setFont(new Font("楷体", Font.BOLD, 27));
forgetButton.setBounds(672, 233, 148, 41);
frame.getContentPane().add(forgetButton);
identityButton.setVisible(false);
identityButton.addActionListener(new ReIdentityClick(attr));
identityButton.setForeground(Color.BLACK);
identityButton.setFont(new Font("楷体", Font.BOLD, 32));
identityButton.setBounds(27, 559, 198, 49);
frame.getContentPane().add(identityButton);
ImageIcon icon = new ImageIcon(Start.class.getResource("img/清晨.jpg"));// 背景图
Image h = icon.getImage().getScaledInstance(frame.getWidth(), frame.getHeight(), icon.getImage().SCALE_DEFAULT);
icon = new ImageIcon(h);
JLabel morning = new JLabel(icon);// 往一个标签中加入图片
morning.setBounds(0, 0, icon.getIconWidth(), icon.getIconHeight());// 设置标签位置大小为图片大小
frame.getLayeredPane().add(morning, Integer.valueOf(Integer.MIN_VALUE));// 标签添加到第二层面板
JPanel bg = (JPanel) frame.getContentPane();
bg.setOpaque(false); // 底层面板透明,处在最底层的图片才看得见
frame.setVisible(true);
}
}
package stu_system.gui;
import java.awt.BorderLayout;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.plaf.FontUIResource;
import stu_system.Start;
import stu_system.event.mysql_join.Click;
import stu_system.tool.Connect;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import java.awt.Font;
import java.awt.Image;
import javax.swing.SwingConstants;
import javax.swing.UIManager;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JTextField;
import javax.swing.JPasswordField;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.SQLException;
import java.awt.event.ActionEvent;
import java.awt.Color;
public class MysqlPanel extends JFrame {
static Connection con = null; // 连接接口
private JPanel contentPane; //整个面板
private JTextField MysqlAccountField; //账号框
private JPasswordField passwordField; //密码框
private JButton ConnectButton; //连接按钮
private JPanel bg; // 背景面板
public MysqlPanel() {
setTitle("MYSQL\u63A5\u5165");
setResizable(false);
// 设置按钮字体大小
UIManager.put("OptionPane.buttonFont", new FontUIResource(new Font("楷体", Font.BOLD, 25)));
// 设置文本字体大小
UIManager.put("OptionPane.messageFont", new FontUIResource(new Font("楷体", Font.BOLD, 30)));
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 824, 631);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
JLabel lblNewLabel = new JLabel("MYSQL\u6570\u636E\u5E93\u63A5\u5165");
lblNewLabel.setHorizontalAlignment(SwingConstants.CENTER);
lblNewLabel.setVerticalAlignment(SwingConstants.CENTER);
lblNewLabel.setFont(new Font("楷体", Font.PLAIN, 49));
lblNewLabel.setBounds(213, 26, 433, 95);
contentPane.add(lblNewLabel);
JLabel lblNewLabel_1 = new JLabel("\u7528\u6237\u540D\uFF1A");
lblNewLabel_1.setForeground(new Color(240, 248, 255));
lblNewLabel_1.setBackground(new Color(245, 245, 245));
lblNewLabel_1.setHorizontalAlignment(SwingConstants.CENTER);
lblNewLabel_1.setFont(new Font("楷体", Font.BOLD, 32));
lblNewLabel_1.setBounds(134, 136, 152, 66);
contentPane.add(lblNewLabel_1);
JLabel label = new JLabel("\u5BC6\u7801\uFF1A");
label.setForeground(new Color(240, 248, 255));
label.setBackground(new Color(245, 245, 245));
label.setHorizontalAlignment(SwingConstants.CENTER);
label.setFont(new Font("楷体", Font.BOLD, 32));
label.setBounds(154, 230, 142, 45);
contentPane.add(label);
JLabel lb3 = new JLabel(
"\u767B\u5F55\u524D\u9700\u4FDD\u8BC1\u6709\u540D\u4E3Astudent_system\u7684\u6570\u636E\u5E93");
lb3.setHorizontalAlignment(SwingConstants.CENTER);
lb3.setFont(new Font("楷体", Font.BOLD, 30));
lb3.setBounds(43, 320, 760, 91);
contentPane.add(lb3);
ConnectButton = new JButton("\u8FDE\u63A5");
ConnectButton.setForeground(new Color(0, 0, 0));
ConnectButton.setBackground(new Color(255, 255, 255));
ConnectButton.setFont(new Font("楷体", Font.BOLD, 40));
ConnectButton.setBounds(341, 426, 163, 59);
contentPane.add(ConnectButton);
MysqlAccountField = new JTextField();
MysqlAccountField.setText("root");
MysqlAccountField.setFont(new Font("楷体", Font.PLAIN, 28));
MysqlAccountField.setBounds(275, 150, 339, 38);
contentPane.add(MysqlAccountField);
MysqlAccountField.setColumns(10);
passwordField = new JPasswordField();
passwordField.setEchoChar('*');
passwordField.setFont(new Font("楷体", Font.PLAIN, 28));
passwordField.setBounds(275, 239, 339, 38);
passwordField.setText("admin");
contentPane.add(passwordField);
ImageIcon icon = new ImageIcon(Start.class.getResource("img/蓝天.jpg"));
Image h = icon.getImage().getScaledInstance(getWidth(), getHeight(), icon.getImage().SCALE_DEFAULT);
icon = new ImageIcon(h); // 将设置好大小的图片重新分配图片图标对象
JLabel morning = new JLabel(icon);// 往一个标签中加入图片
morning.setBounds(0, 0, icon.getIconWidth(), icon.getIconHeight());// 设置标签位置大小为图片大小
getLayeredPane().add(morning, Integer.valueOf(Integer.MIN_VALUE));// 标签添加到第二层面板
bg = (JPanel) getContentPane();
bg.setOpaque(false); // 底层面板透明,处在最底层的图片才看得见
}
public void EventAdd() {
ConnectButton.addActionListener(new Click(MysqlAccountField, passwordField, this)); //登录按钮事件
}
}
package stu_system.gui;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import com.mysql.jdbc.Connection;
import stu_system.event.login_panel.register.Attribute;
import stu_system.event.login_panel.register.Register;
import java.awt.event.ActionListener;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.awt.event.ActionEvent;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import java.awt.Font;
import javax.swing.JTextField;
import javax.swing.UIManager;
import javax.swing.plaf.FontUIResource;
public class RegisterPanel {
private JDialog jd = null;
private JTextField num = new JTextField();
private JTextField password = new JTextField();
private JButton button = new JButton("\u6CE8\u518C");
public RegisterPanel(Connection con, JFrame jf, int flag) {
jd = new JDialog(jf, true);
Attribute attr = new Attribute(con, jd, num, password, button, flag);
// 设置按钮字体大小
UIManager.put("OptionPane.buttonFont", new FontUIResource(new Font("楷体", Font.BOLD, 25)));
// 设置文本字体大小
UIManager.put("OptionPane.messageFont", new FontUIResource(new Font("楷体", Font.BOLD, 30)));
jd.setAlwaysOnTop(true);
if (flag == 0) {
jd.setTitle("管理员注册界面");
} else if (flag == 1) {
jd.setTitle("学生注册界面");
}
jd.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
jd.setBounds(100, 100, 665, 450);
jd.getContentPane().setLayout(null);
JLabel label = new JLabel("\u65B0\u8D26\u53F7\uFF1A");
label.setFont(new Font("楷体", Font.PLAIN, 28));
label.setBounds(112, 92, 112, 48);
jd.getContentPane().add(label);
JLabel label_1 = new JLabel("\u65B0\u5BC6\u7801\uFF1A");
label_1.setFont(new Font("楷体", Font.PLAIN, 28));
label_1.setBounds(112, 180, 112, 48);
jd.getContentPane().add(label_1);
num.setText("18140809032");
num.setFont(new Font("楷体", Font.PLAIN, 24));
num.setBounds(223, 99, 343, 34);
jd.getContentPane().add(num);
num.setColumns(10);
password.setFont(new Font("楷体", Font.PLAIN, 24));
password.setColumns(10);
password.setBounds(223, 191, 343, 34);
jd.getContentPane().add(password);
button.addActionListener(new Register(attr));
button.setFont(new Font("楷体", Font.PLAIN, 30));
button.setBounds(280, 281, 182, 67);
jd.getContentPane().add(button);
jd.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
jd.setVisible(true);
}
}
package stu_system.gui;
import java.awt.BorderLayout;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import com.mysql.jdbc.Connection;
import stu_system.Start;
import stu_system.event.stu_function.Attribute;
import stu_system.event.stu_function.ButtonClickCarton;
import stu_system.event.stu_function.ReturnClick;
import stu_system.event.stu_function.SearchClick;
import stu_system.event.stu_function.UpdateInfoClick;
import stu_system.event.stu_function.UpdatePasswordClick;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import java.awt.Font;
import java.awt.Image;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import java.awt.Component;
import java.awt.event.ActionListener;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.awt.event.ActionEvent;
import javax.swing.JLabel;
import java.awt.Color;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
public class StuFunctionPanel {
private JFrame jf = new JFrame();
private JTextArea showtArea = new JTextArea();
private JButton searchInfoButton = new JButton("\u67E5\u8BE2\u672C\u4EBA\u4FE1\u606F");
private JButton searchGradeButton = new JButton("\u67E5\u8BE2\u672C\u4EBA\u6210\u7EE9");
private JButton returnButton = new JButton("\u8FD4\u56DE");
private JButton updateInfoButton = new JButton("\u66F4\u65B0\u672C\u4EBA\u4FE1\u606F");
private JButton updatePasswordButton = new JButton("\u4FEE\u6539\u672C\u4EBA\u5BC6\u7801");
public StuFunctionPanel(String myNum, Connection con) {
Attribute attr = new Attribute(myNum, con, jf, showtArea, searchInfoButton, searchGradeButton, returnButton,
updateInfoButton, updatePasswordButton);
jf.setResizable(false);
jf.setTitle("\u5B66\u751F\u529F\u80FD\u754C\u9762");
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jf.setBounds(100, 100, 1037, 693);
JPanel contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
jf.setContentPane(contentPane);
contentPane.setLayout(null);
searchInfoButton.addMouseListener(new ButtonClickCarton(attr, 0));
searchInfoButton.addActionListener(new SearchClick(attr, 0));
searchInfoButton.setFont(new Font("楷体", Font.PLAIN, 28));
searchInfoButton.setBounds(53, 224, 219, 51);
contentPane.add(searchInfoButton);
searchGradeButton.addMouseListener(new ButtonClickCarton(attr, 1));
searchGradeButton.addActionListener(new SearchClick(attr, 1));
searchGradeButton.setFont(new Font("楷体", Font.PLAIN, 28));
searchGradeButton.setBounds(53, 305, 219, 51);
contentPane.add(searchGradeButton);
updatePasswordButton.addMouseListener(new ButtonClickCarton(attr, 2));
updatePasswordButton.addActionListener(new UpdatePasswordClick(attr));
updatePasswordButton.setFont(new Font("楷体", Font.PLAIN, 28));
updatePasswordButton.setBounds(53, 391, 219, 51);
contentPane.add(updatePasswordButton);
updateInfoButton.addMouseListener(new ButtonClickCarton(attr, 3));
updateInfoButton.addActionListener(new UpdateInfoClick(attr));
updateInfoButton.setFont(new Font("楷体", Font.PLAIN, 28));
updateInfoButton.setBounds(53, 135, 219, 51);
contentPane.add(updateInfoButton);
JPanel panel = new JPanel();
panel.setOpaque(false);
panel.setBorder(null);
showtArea.setEditable(false);
showtArea.setForeground(new Color(240, 248, 255));
showtArea.setOpaque(false);
panel.setBounds(325, 102, 652, 465);
contentPane.add(panel);
panel.setLayout(null);
JScrollPane scrollPane = new JScrollPane();
scrollPane.setOpaque(false);
scrollPane.setBorder(null);
scrollPane.getViewport().setOpaque(false);
scrollPane.setBounds(0, 0, 652, 465);
panel.add(scrollPane);
showtArea.setFont(new Font("楷体", Font.BOLD, 30));
scrollPane.setViewportView(showtArea);
showtArea.setLineWrap(true);
JLabel menu = new JLabel("\u663E\u793A\u4F4D\u7F6E\u2193");
menu.setForeground(new Color(255, 255, 255));
menu.setFont(new Font("楷体", Font.PLAIN, 35));
menu.setBounds(325, 48, 209, 51);
contentPane.add(menu);
ImageIcon icon = new ImageIcon(Start.class.getResource("img/美食节.jpg"));// 背景图
Image h = icon.getImage().getScaledInstance(jf.getWidth(), jf.getHeight(), icon.getImage().SCALE_DEFAULT);
icon = new ImageIcon(h); // 将设置好大小的图片重新分配图片图标对象
JLabel morning = new JLabel(icon);// 往一个标签中加入图片
morning.setBounds(0, 0, icon.getIconWidth(), icon.getIconHeight());// 设置标签位置大小为图片大小
jf.getLayeredPane().add(morning, Integer.valueOf(Integer.MIN_VALUE));// 标签添加到第二层面板
JPanel bg = (JPanel) jf.getContentPane();
bg.setOpaque(false); // 底层面板透明,处在最底层的图片才看得见
bg.add(panel);
returnButton.addMouseListener(new ButtonClickCarton(attr, 4));
returnButton.addActionListener(new ReturnClick(attr));
returnButton.setFont(new Font("楷体", Font.PLAIN, 28));
returnButton.setBounds(53, 477, 219, 51);
contentPane.add(returnButton);
jf.setVisible(true);
}
}
package stu_system.event.admin_function.grade_insert;
import java.sql.Connection;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JTextField;
public class Attribute {
Connection con;
JDialog jd ;
JTextField chineseField;
JTextField mathField ;
JTextField englishField ;
JTextField nameField;
JTextField numField;
JButton affirmButton;
public Attribute(Connection con, JDialog jd, JTextField chineseField, JTextField mathField, JTextField englishField,
JTextField nameField, JTextField numField, JButton affirmButton) {
this.con = con;
this.jd = jd;
this.chineseField = chineseField;
this.mathField = mathField;
this.englishField = englishField;
this.nameField = nameField;
this.numField = numField;
this.affirmButton = affirmButton;
}
}
package stu_system.event.admin_function.grade_insert;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import javax.swing.JOptionPane;
import stu_system.tool.SqlSearch;
public class InsertGradeClick implements ActionListener {
Attribute attr;
public InsertGradeClick(Attribute attr) {
this.attr = attr;
}
public void actionPerformed(ActionEvent e) {
try {
String chinese_grade = attr.chineseField.getText();
String math_grade = attr.mathField.getText();
String english_grade = attr.englishField.getText();
if (!(chinese_grade.equals("") || math_grade.equals("") || english_grade.equals("")
|| attr.nameField.getText().equals("") || attr.numField.getText().equals(""))) {
String sql = "select num from stu where num=?";
String num = SqlSearch.dataByAnother(sql, attr.con, "18140809032" + attr.numField.getText());
if (num != null) {
sql = "select num from grade where num=?";
String temp = SqlSearch.dataByAnother(sql, attr.con, "18140809032" + attr.numField.getText());
if (temp == null) {
sql = "insert into grade values(?,?,?,?,?)";
boolean update = SqlSearch.Update(sql, attr.con, "18140809032" + attr.numField.getText(),
attr.nameField.getText(), chinese_grade, math_grade, english_grade);
if (update) {
JOptionPane.showMessageDialog(null, "学生信息添加成功!");
attr.jd.dispose();
} else {
JOptionPane.showMessageDialog(null, "学生信息添加失败!");
}
} else {
JOptionPane.showMessageDialog(null, "学生成绩已录过!");
}
} else {
JOptionPane.showMessageDialog(null, "无此学生!");
}
} else {
JOptionPane.showMessageDialog(null, "请将所有信息填上!");
}
} catch (Exception e2) {
JOptionPane.showMessageDialog(null, "学生信息添加失败!");
}
}
}
package stu_system.event.admin_function.info_insert;
import java.sql.Connection;
import javax.swing.JDialog;
import javax.swing.JTextField;
public class Attribute {
Connection con;
JDialog jd;
JTextField numField;
JTextField nameField;
JTextField teleField;
JTextField addrField;
public Attribute(Connection con, JDialog jd,JTextField numField, JTextField nameField, JTextField teleField,
JTextField addrField) {
this.con = con;
this.jd=jd;
this.numField = numField;
this.nameField = nameField;
this.teleField = teleField;
this.addrField = addrField;
}
}
package stu_system.event.admin_function.info_insert;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.PreparedStatement;
import javax.swing.JOptionPane;
import stu_system.tool.SqlSearch;
public class InsertStuClick implements ActionListener {
Attribute attr;
public InsertStuClick(Attribute attr) {
this.attr = attr;
}
public void actionPerformed(ActionEvent e) {
try {
if (!(attr.numField.getText().equals("") || attr.nameField.getText().equals("")
|| attr.teleField.getText().equals("") || attr.addrField.getText().equals(""))) {
String sql = "select num from stu where num=?";
String num = SqlSearch.dataByAnother(sql, attr.con, attr.numField.getText());
if (num!=null) {
sql = "select num from information where num=?";
String temp = SqlSearch.dataByAnother(sql, attr.con, num);
if (temp==null) {
sql = "insert into information values (?,?,?,?)";
boolean update = SqlSearch.Update(sql, attr.con, attr.numField.getText(), attr.nameField.getText(), attr.teleField.getText(),
attr.addrField.getText());
if(update) {
JOptionPane.showMessageDialog(null, "学生信息添加成功!");
attr.jd.dispose();
}else {
JOptionPane.showMessageDialog(null, "学生信息添加失败!");
}
} else {
JOptionPane.showMessageDialog(null, "学生信息已存在!");
}
} else {
JOptionPane.showMessageDialog(null, "无此学号学生!");
}
} else {
JOptionPane.showMessageDialog(null, "请将所有信息填上!");
}
} catch (Exception e2) {
// TODO: handle exception
}
}
}
package stu_system.event.admin_function;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import com.mysql.jdbc.Connection;
public class Attribute {
Connection con;
JPanel contentPane;
JFrame jf; // 当前框架
JTextArea showArea; // 显示区域
JLabel Menu; // 菜单条目
JButton newStuButton; // 新增学生按钮
JButton searchInfoButton; // 查询信息按钮
JButton insertGradeButton; // 插入成绩按钮
JButton searchGradeButton; // 查询成绩按钮
JButton updatePasswordButton; // 修改密码按钮
JButton disposeAccountButton; // 注销账户按钮
JButton updateInfoButton; // 修改信息按钮
JButton returnButton; // 返回上一界面按钮
JButton accountSetButton; // 查看所有账户按钮
JButton updateGradeButton; //修改成绩按钮
String myNum; //当前账号
public Attribute(Connection con, JPanel contentPane, JFrame jf, JTextArea showArea, JLabel menu,
JButton newStuButton, JButton searchInfoButton, JButton insertGradeButton, JButton searchGradeButton,
JButton updatePasswordButton, JButton disposeAccountButton, JButton updateInfoButton, JButton returnButton,
JButton accountSetButton,JButton updateGradeButton,String myNum) {
super();
this.con = con;
this.contentPane = contentPane;
this.jf = jf;
this.showArea = showArea;
Menu = menu;
this.newStuButton = newStuButton;
this.searchInfoButton = searchInfoButton;
this.insertGradeButton = insertGradeButton;
this.searchGradeButton = searchGradeButton;
this.updatePasswordButton = updatePasswordButton;
this.disposeAccountButton = disposeAccountButton;
this.updateInfoButton = updateInfoButton;
this.returnButton = returnButton;
this.accountSetButton = accountSetButton;
this.updateGradeButton=updateGradeButton;
this.myNum=myNum;
}
}
package stu_system.event.admin_function;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.List;
import stu_system.tool.SqlSearch;
public class AccountClick implements ActionListener {
Attribute attr;
public AccountClick(Attribute attr) {
this.attr = attr;
}
public void actionPerformed(ActionEvent arg0) {
try {
attr.showArea.setText("");
attr.Menu.setText("显示(依次为账号,密码)↓");
String sql = "select * from admin";
List<StringBuilder> result = SqlSearch.SelectAll(sql, attr.con);
if (result.size() == 0) {
attr.showArea.append("管理员账号库为空!\n");
} else {
for (StringBuilder builder : result) {
attr.showArea.append("管理员账号:\n");
attr.showArea.append(builder.toString() + "\n");
}
}
attr.showArea.append("\n");
sql = "select * from stu";
result = SqlSearch.SelectAll(sql, attr.con);
if (result.size() == 0) {
attr.showArea.append("学生账号库为空!\n");
} else {
for (StringBuilder builder : result) {
attr.showArea.append("学生账号:\n");
attr.showArea.append(builder.toString() + "\n");
}
}
} catch (Exception e) {
// TODO: handle exception
}
}
}
package stu_system.event.admin_function;
import java.awt.Color;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
public class ButtonClickCarton implements MouseListener {
Attribute attr;
int flag;
public ButtonClickCarton(Attribute attr, int flag) {
this.attr = attr;
this.flag = flag;
}
@Override
public void mouseClicked(MouseEvent arg0) {
// TODO 自动生成的方法存根
}
@Override
public void mouseEntered(MouseEvent arg0) {
switch (flag) {
case 0:
attr.newStuButton.setForeground(Color.red);
break;
case 1:
attr.searchInfoButton.setForeground(Color.red);
break;
case 2:
attr.insertGradeButton.setForeground(Color.red);
break;
case 3:
attr.searchGradeButton.setForeground(Color.red);
break;
case 4:
attr.updatePasswordButton.setForeground(Color.red);
break;
case 5:
attr.disposeAccountButton.setForeground(Color.red);
break;
case 6:
attr.updateInfoButton.setForeground(Color.red);
break;
case 7:
attr.returnButton.setForeground(Color.red);
break;
case 8:
attr.accountSetButton.setForeground(Color.red);
break;
case 9:
attr.updateGradeButton.setForeground(Color.red);
break;
default:
break;
}
}
@Override
public void mouseExited(MouseEvent e) {
switch (flag) {
case 0:
attr.newStuButton.setForeground(Color.black);
break;
case 1:
attr.searchInfoButton.setForeground(Color.black);
case 2:
attr.insertGradeButton.setForeground(Color.black);
break;
case 3:
attr.searchGradeButton.setForeground(Color.black);
break;
case 4:
attr.updatePasswordButton.setForeground(Color.black);
break;
case 5:
attr.disposeAccountButton.setForeground(Color.black);
break;
case 6:
attr.updateInfoButton.setForeground(Color.black);
break;
case 7:
attr.returnButton.setForeground(Color.black);
break;
case 8:
attr.accountSetButton.setForeground(Color.black);
break;
case 9:
attr.updateGradeButton.setForeground(Color.black);
break;
default:
break;
}
}
@Override
public void mousePressed(MouseEvent arg0) {
// TODO 自动生成的方法存根
}
@Override
public void mouseReleased(MouseEvent arg0) {
// TODO 自动生成的方法存根
}
}
package stu_system.event.admin_function;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.PreparedStatement;
import javax.swing.JOptionPane;
import stu_system.gui.LoginPanel;
import stu_system.tool.SqlSearch;
public class DisposeClick implements ActionListener{
Attribute attr;
public DisposeClick(Attribute attr) {
this.attr = attr;
}
public void actionPerformed(ActionEvent e) {
try {
Object[] options = { "本人", "指定学生" };
int i = JOptionPane.showOptionDialog(null, "注销本人账号还是学生账号?", "选择", JOptionPane.YES_NO_OPTION,
JOptionPane.QUESTION_MESSAGE, null, options, null);
if (i == 0) { // 注销本人账号
String sql = "delete from admin where num=?";
SqlSearch.Update(sql, attr.con, attr.myNum);
JOptionPane.showMessageDialog(attr.jf, "账户注销成功!");
new LoginPanel(attr.con);
attr.jf.dispose();
} else if (i == 1) {
String sql = "select num from stu where num=?";
String num = SqlSearch.dataByAnother(sql, attr.con, JOptionPane.showInputDialog("注销学生账号为:"));
if (num!=null) {
String sql2 = "delete from stu where num=?";
String sql3 = "delete from information where num=?";
String sql4 = "delete from math_grade where 学号=?";
String sql5 = "delete from english_grade where 学号=?";
String sql6 = "delete from chinese_grade where 学号=?";
boolean update = SqlSearch.batchUpdate(new String[] {sql2,sql3,sql4,sql5,sql6}, attr.con, num);
if(update) {
JOptionPane.showMessageDialog(attr.jf, "学生账户注销成功!");
}else {
JOptionPane.showMessageDialog(attr.jf, "学生账户注销失败!");
}
} else {
JOptionPane.showMessageDialog(attr.jf, "无此账号!");
}
}
} catch (Exception e2) {
// TODO: handle exception
}
}
}
package stu_system.event.admin_function;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import stu_system.gui.InsertStuPanel;
import stu_system.gui.LoginPanel;
import stu_system.gui.InsertGradePanel;
public class InsertClick implements ActionListener {
Attribute attr;
int flag; // 根据不同值在更新对象时打开不同窗口
public InsertClick(Attribute attr, int flag) {
this.attr = attr;
this.flag = flag;
}
@Override
public void actionPerformed(ActionEvent e) {
switch (flag) {
case 0:
new InsertStuPanel(attr.jf, attr.con);
break;
case 1:
new InsertGradePanel(attr.jf, attr.con);
break;
case 2:
new LoginPanel(attr.con);
attr.jf.dispose();
break;
default:
break;
}
}
}
package stu_system.event.admin_function;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.List;
import javax.swing.JOptionPane;
import stu_system.tool.SqlSearch;
public class SearchClick implements ActionListener {
Attribute attr;
int flag;
public SearchClick(Attribute attr, int flag) {
this.attr = attr;
this.flag = flag;
}
@Override
public void actionPerformed(ActionEvent e) {
Object[] options = { "所有人", "特定人" };
int i = JOptionPane.showOptionDialog(null, "查询所有人还是一个人?", "选择", JOptionPane.YES_NO_OPTION,
JOptionPane.QUESTION_MESSAGE, null, options, null);
attr.showArea.setText("");
String sql = null;
switch (flag) {
case 0:
attr.Menu.setText("显示(依次为学号、姓名,语文成绩,数学成绩,英语成绩)↓");
if (i == 0) {
sql = "select * from grade";
} else if (i == 1) {
sql = "select * from grade where num=?";
}
break;
case 1:
attr.Menu.setText("显示(依次为学号、姓名,电话,地址)↓");
if (i == 0) {
sql = "select * from information";
} else if (i == 1) {
sql = "select * from information where num=?";
}
break;
}
if (i == 0) {// 查询所有人
List<StringBuilder> list = SqlSearch.SelectAll(sql, attr.con);
if (list.size() == 0) {
JOptionPane.showMessageDialog(attr.jf, "当前无数据!");
} else {
for (StringBuilder builder : list) {
attr.showArea.append(builder.toString() + "\n");
}
}
} else if (i == 1) {
List<StringBuilder> list = SqlSearch.SelectAll(sql, attr.con, JOptionPane.showInputDialog("输入学号:")); // 查询得到的结果
if (list.size() == 0) {
JOptionPane.showMessageDialog(attr.jf, "查无此人!");
} else {
for (StringBuilder builder : list) {
attr.showArea.append(builder.toString() + "\n");
}
}
}
}
}
package stu_system.event.admin_function;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.PreparedStatement;
import javax.swing.JOptionPane;
import stu_system.tool.SqlSearch;
public class UpdateGradeClick implements ActionListener {
Attribute attr;
public UpdateGradeClick(Attribute attr) {
this.attr = attr;
}
@Override
public void actionPerformed(ActionEvent e) {
try {
String sql = "select num from grade where num=?";
String num = SqlSearch.dataByAnother(sql, attr.con, JOptionPane.showInputDialog("输入学号"));
if (num != null) {
Object[] options = { "语文", "数学", "英语" };
int i = JOptionPane.showOptionDialog(null, "修改哪科分数?", "选择", JOptionPane.YES_NO_OPTION,
JOptionPane.QUESTION_MESSAGE, null, options, null);
if (i != -1) {
String up = JOptionPane.showInputDialog("修改分数为:");
if (i == 0) {
sql = "update grade set chinese_grade=? where num=?";
} else if (i == 1) {
sql = "update grade set math_grade=?where num=?";
} else if (i == 2) {
sql = "update grade set english_grade=? where num=?";
}
boolean update = SqlSearch.Update(sql, attr.con, up, num);
if (update) {
JOptionPane.showMessageDialog(attr.jf, num + "【" + options[i] + "】成绩修改成功!");
} else {
JOptionPane.showMessageDialog(attr.jf, num + "【" + options[i] + "】成绩修改失败!");
}
}
} else {
JOptionPane.showMessageDialog(null, "查无此人!");
}
} catch (Exception e2) {
// TODO: handle exception
}
}
}
package stu_system.event.admin_function;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JOptionPane;
import stu_system.tool.SqlSearch;
public class UpdateInfoClick implements ActionListener {
Attribute attr;
public UpdateInfoClick(Attribute attr) {
this.attr = attr;
}
@Override
public void actionPerformed(ActionEvent e) {
String sql = "select num from information where num=?";
String num = SqlSearch.dataByAnother(sql, attr.con, JOptionPane.showInputDialog("输入学号")); // 学号
if (num != null) {
Object[] options = { "学号", "姓名", "电话", "地址" };
int i = JOptionPane.showOptionDialog(null, "修改哪项信息?", "选择", JOptionPane.YES_NO_OPTION,
JOptionPane.QUESTION_MESSAGE, null, options, null);
if (i != -1) {
String up = JOptionPane.showInputDialog("修改学号为" + num + "的【" + options[i] + "】为:");
boolean update = false;
if (i == 0) {
if (up.startsWith("18140809032") && up.length() == 13) {
String sql2 = "update chinese_grade set 学号=? where 学号=?";
String sql3 = "update math_grade set 学号=? where 学号=?";
String sql4 = "update english_grade set 学号=? where 学号=?";
String sql5 = "update information set num=? where num=?";
String sql6 = "update stu set num=? where num=?";
update = SqlSearch.batchUpdate(new String[] { sql2, sql3, sql4, sql5, sql6 }, attr.con, up,
num);
} else {
JOptionPane.showMessageDialog(null, "学号格式必须为:18140809032XX");
return;
}
} else if (i == 1) {
String sql2 = "update chinese_grade set 姓名=? where 学号=?";
String sql3 = "update math_grade set 姓名=? where 学号=?";
String sql4 = "update english_grade set 姓名=? where 学号=?";
String sql5 = "update information set name=? where num=?";
update = SqlSearch.batchUpdate(new String[] { sql2, sql3, sql4, sql5 }, attr.con, up, num);
} else {
String sql2 = null;
if (i == 2) {
sql2 = "update information set tele=? where num=?";
} else if (i == 3) {
sql2 = "update information set address=? where num=?";
}
update = SqlSearch.Update(sql2, attr.con, up, num);
}
if (update) {
JOptionPane.showMessageDialog(null, "学号为" + num + "的【" + options[i] + "】修改成功!");
} else {
JOptionPane.showMessageDialog(null, "学号为" + num + "的【" + options[i] + "】修改失败!");
}
}
} else {
JOptionPane.showMessageDialog(null, "无此人");
}
}
}
package stu_system.event.admin_function;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JOptionPane;
import stu_system.gui.LoginPanel;
import stu_system.tool.SqlSearch;
public class UpdatePasswordClick implements ActionListener {
Attribute attr;
public UpdatePasswordClick(Attribute attr) {
this.attr = attr;
}
@Override
public void actionPerformed(ActionEvent e) {
Object[] options = { "本人", "指定学生" };
int i = JOptionPane.showOptionDialog(null, "修改本人密码还是学生密码?", "选择", JOptionPane.YES_NO_OPTION,
JOptionPane.QUESTION_MESSAGE, null, options, null);
String sql = null;
if (i == 0) {// 修改本人密码
sql = "update admin set password=? where num=?";
String pw = JOptionPane.showInputDialog("新密码:");
boolean update = SqlSearch.Update(sql, attr.con, pw, attr.myNum);
if (update) {
JOptionPane.showMessageDialog(attr.jf, "密码修改成功!请重新登录");
new LoginPanel(attr.con);
attr.jf.dispose();
}else {
JOptionPane.showMessageDialog(attr.jf, "密码修改失败!");
}
}else if(i==1) {
sql = "select num from stu where num=?";
String ad = JOptionPane.showInputDialog("学生账号为:");
String result = SqlSearch.dataByAnother(sql, attr.con, ad); //学生账号若存在则不为null
if(result!=null) {
sql = "update stu set password=? where num=?";
String newPw = JOptionPane.showInputDialog("新密码为:"); //新密码
boolean update = SqlSearch.Update(sql, attr.con, newPw,ad);
if(update) {
JOptionPane.showMessageDialog(attr.jf, "学生密码修改成功!");
}else {
JOptionPane.showMessageDialog(attr.jf, "学生密码修改失败!");
}
}else {
JOptionPane.showMessageDialog(attr.jf, "无此学生账号!");
}
}
}
}
package stu_system.event.login_panel.register;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JTextField;
import com.mysql.jdbc.Connection;
public class Attribute {
int flag;
Connection con;
JDialog jd;
JTextField num;
JTextField password;
JButton button;
public Attribute(Connection con, JDialog jd, JTextField num, JTextField password, JButton button,int flag) {
this.con = con;
this.jd = jd;
this.num = num;
this.password = password;
this.button = button;
this.flag=flag;
}
}
package stu_system.event.login_panel.register;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import javax.swing.JOptionPane;
import stu_system.tool.SqlSearch;
public class Register implements ActionListener {
Attribute attr;
public Register(Attribute attr) {
this.attr = attr;
}
public void actionPerformed(ActionEvent e) {
String account = attr.num.getText();
String password = attr.password.getText();
if (account.startsWith("18140809032") && account.length() == 13) { // 账号必须以这个前缀开头
String sql = "select * from admin,stu where stu.num=? or admin.num=?";
String sql2 = null;
if (attr.flag == 0) {
sql2 = "insert into admin values (?,?)";
} else if (attr.flag == 1) {
sql2 = "insert into stu values (?,?)";
}
String num = SqlSearch.dataByAnother(sql, attr.con, account,account);
if (num == null) { // 说明账号先前没被注册
boolean update = SqlSearch.Update(sql2, attr.con, account, password);
if (update) {
if (attr.flag == 0) {
JOptionPane.showMessageDialog(null, "管理员账号注册成功!");
} else if (attr.flag == 1) {
JOptionPane.showMessageDialog(null, "学生账号注册成功!");
}
attr.jd.dispose();
} else {
if (attr.flag == 0) {
JOptionPane.showMessageDialog(null, "管理员账号注册失败!");
} else if (attr.flag == 1) {
JOptionPane.showMessageDialog(null, "学生账号注册失败!");
}
}
} else {
JOptionPane.showMessageDialog(null, "账号已被注册!");
}
} else {
JOptionPane.showMessageDialog(null, "账号格式需为:18140809032XX");
}
}
}
package stu_system.event.login_panel;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
public class Attribute {
Connection con;
ResultSet rs = null; // 结果集接口
int flag = -1; // 是否选择身份的标志,-1为未选,1为管理员,2为学生
JFrame frame;
JTextField accountField;
JTextField passwordField;
JLabel attention;
JButton adminButton;
JButton stuButton;
JLabel adminName ;
JLabel stuName;
PreparedStatement ps;
JButton identityButton;
JButton loginButton ;
JButton forgetButton ;
JButton registerButton ;
public Attribute() {
}
/**
*
* @param con Sql连接
* @param frame 主体框架
* @param accountField 账号栏
* @param passwordField 密码栏
* @param attention 当前选择身份情况说明
* @param adminButton 管理员按钮
* @param stuButton 学生按钮
* @param adminName 管理员标签
* @param stuName 学生标签
* @param ps Sql执行绑定工具
* @param identityButton 重新选择身份按钮
* @param loginButton 登录按钮
* @param forgetButton 忘记密码
* @param registerButton 注册账号
*/
public Attribute(Connection con, JFrame frame, JTextField accountField,
JTextField passwordField, JLabel attention, JButton adminButton, JButton stuButton, JLabel adminName,
JLabel stuName, JButton identityButton, JButton loginButton, JButton forgetButton,
JButton registerButton) {
this.con = con;
this.frame = frame;
this.accountField = accountField;
this.passwordField = passwordField;
this.attention = attention;
this.adminButton = adminButton;
this.stuButton = stuButton;
this.adminName = adminName;
this.stuName = stuName;
this.identityButton = identityButton;
this.loginButton = loginButton;
this.forgetButton = forgetButton;
this.registerButton = registerButton;
}
}
package stu_system.event.login_panel;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.PreparedStatement;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
public class AdminClick implements ActionListener {
Attribute attr;
public AdminClick(Attribute attr) {
this.attr = attr;
}
@Override
public void actionPerformed(ActionEvent e) {
try {
// if (JOptionPane.showInputDialog(frame, "输入暗号:").equals("皮蛋不是蛋")) {
attr.identityButton.setVisible(true);
attr.adminButton.setVisible(false);
attr.stuButton.setVisible(false);
attr.adminName.setVisible(false);
attr.stuName.setVisible(false);
attr.attention.setText("已选择管理员身份,请输入账号密码!");
attr.attention.setVisible(true);
attr.flag = 1;
// } else {
// JOptionPane.showMessageDialog(frame, "暗号错误!");
// }
} catch (Exception e1) {
}
}
}
package stu_system.event.login_panel;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.PreparedStatement;
import javax.swing.JOptionPane;
import stu_system.tool.SqlSearch;
public class FogetClick implements ActionListener {
Attribute attr;
public FogetClick(Attribute attr) {
this.attr = attr;
}
@Override
public void actionPerformed(ActionEvent arg0) {
try {
String result = null;
String sql = null;
if (attr.flag == -1) {
JOptionPane.showMessageDialog(attr.frame, "请先选择身份!");
return ;
} else if (attr.flag == 1) { // 是管理员
sql = "select password from admin where num=?";
} else {
sql = "select password from stu where num=?";
}
String s = JOptionPane.showInputDialog(attr.frame, "请输入账号:"); //根据sql查询密码
result = SqlSearch.dataByAnother(sql, attr.con, s);
if (result == null) {
JOptionPane.showMessageDialog(attr.frame, "无效账号!");
} else {
JOptionPane.showConfirmDialog(attr.frame, "密码为:" + result);
}
} catch (Exception e) {
}
}
}
package stu_system.event.login_panel;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.BorderFactory;
public class IdentityButtonCarton implements MouseListener {
Attribute attr;
boolean flag;
public IdentityButtonCarton(Attribute attr,boolean flag) {
this.attr = attr;
this.flag=flag;
}
@Override
public void mouseEntered(MouseEvent arg0) {
if(flag) {
attr.adminButton.setBorder(BorderFactory.createRaisedSoftBevelBorder());
}else {
attr.stuButton.setBorder(BorderFactory.createRaisedSoftBevelBorder());
}
}
@Override
public void mouseExited(MouseEvent e) {
if(flag) {
attr.adminButton.setBorder(BorderFactory.createLoweredSoftBevelBorder());
}else {
attr.stuButton.setBorder(BorderFactory.createLoweredSoftBevelBorder());
}
}
@Override
public void mousePressed(MouseEvent e) {
if(flag) {
attr.adminButton.setBorder(BorderFactory.createLoweredSoftBevelBorder());
}else {
attr.stuButton.setBorder(BorderFactory.createLoweredSoftBevelBorder());
}
}
@Override
public void mouseClicked(MouseEvent e) {
// TODO 自动生成的方法存根
}
@Override
public void mouseReleased(MouseEvent e) {
// TODO 自动生成的方法存根
}
}
package stu_system.event.login_panel;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.PreparedStatement;
import javax.swing.JOptionPane;
import stu_system.gui.AdminFunctionPanel;
import stu_system.gui.StuFunctionPanel;
import stu_system.tool.SqlSearch;
public class LoginClick implements ActionListener {
Attribute attr;
public LoginClick(Attribute attr) {
this.attr = attr;
}
@Override
public void actionPerformed(ActionEvent e) {
try {
if (attr.flag == -1) {
JOptionPane.showMessageDialog(null, "请先选择身份!");
return;
}
String sql = null;
String sql2 = null;
String s = attr.accountField.getText();
String result = null;
if (attr.flag == 1) { // 管理员身份
sql = "select num from admin where num=?";
} else {
sql = "select num from stu where num=?";
}
result = SqlSearch.dataByAnother(sql, attr.con, s);
if (result == null) {
JOptionPane.showMessageDialog(attr.frame, "无效账号!");
} else {// 账号有效
if (attr.flag == 1) {// 管理员
sql2 = "select password from admin where num=?";
} else {
sql2 = "select password from stu where num=?";
}
result = SqlSearch.dataByAnother(sql2, attr.con, s);
if (!result.equals(attr.passwordField.getText())) {
JOptionPane.showMessageDialog(attr.frame, "密码错误!");
} else {
if (attr.flag == 1) {
new AdminFunctionPanel((com.mysql.jdbc.Connection) attr.con, attr.accountField.getText());
} else {
new StuFunctionPanel(attr.accountField.getText(), (com.mysql.jdbc.Connection) attr.con);
}
attr.frame.dispose();
}
}
} catch (Exception e1) {
// TODO: handle exception
}
}
}
package stu_system.event.login_panel;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JOptionPane;
import stu_system.gui.RegisterPanel;
public class RegisterClick implements ActionListener {
Attribute attr;
public RegisterClick(Attribute attr) {
this.attr = attr;
}
@Override
public void actionPerformed(ActionEvent e) {
if (attr.flag == -1) {
// 消息提示框
JOptionPane.showMessageDialog(null, "请先选择身份!");
} else if (attr.flag == 1) { // 管理员身份
Object[] options = { "管理员", "学生" };
int i = JOptionPane.showOptionDialog(null, "注册学生账号还是管理员账号?", "选择", JOptionPane.YES_NO_OPTION,
JOptionPane.QUESTION_MESSAGE, null, options, null);
if (i == 0) {
new RegisterPanel((com.mysql.jdbc.Connection) attr.con,attr.frame,0);
} else if (i == 1) {
new RegisterPanel((com.mysql.jdbc.Connection) attr.con,attr.frame,1);
}
} else {
// 消息提示框
JOptionPane.showMessageDialog(null, "学生没注册账号权限!");
}
}
}
package stu_system.event.login_panel;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class ReIdentityClick implements ActionListener{
Attribute attr;
public ReIdentityClick(Attribute attr) {
this.attr = attr;
}
@Override
public void actionPerformed(ActionEvent e) {
attr.identityButton.setVisible(false);
attr.adminButton.setVisible(true);
attr.stuButton.setVisible(true);
attr.adminName.setVisible(true);
attr.stuName.setVisible(true);
attr.attention.setVisible(false);
attr.flag = -1;
}
}
package stu_system.event.login_panel;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class StuClick implements ActionListener{
Attribute attr;
public StuClick(Attribute attr) {
this.attr = attr;
}
@Override
public void actionPerformed(ActionEvent e) {
attr.identityButton.setVisible(true);
attr.adminButton.setVisible(false);
attr.stuButton.setVisible(false);
attr.adminName.setVisible(false);
attr.stuName.setVisible(false);
attr.attention.setText("已选择学生身份,请输入账号密码!");
attr.attention.setVisible(true);
attr.flag = 2;
}
}
package stu_system.event.mysql_join;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import stu_system.gui.MysqlPanel;
import stu_system.gui.LoginPanel;
import stu_system.tool.Connect;
public class Click implements ActionListener {
static Connection con = null; // 连接接口
private JTextField txtRoot;
private JPasswordField passwordField;
private MysqlPanel frame;
public Click(JTextField txtRoot, JPasswordField passwordField, MysqlPanel frame) {
this.txtRoot = txtRoot;
this.passwordField = passwordField;
this.frame = frame;
}
public void actionPerformed(ActionEvent e) {
String account = txtRoot.getText(); // Mysql用户名
String password = passwordField.getPassword().toString(); // Mysql密码
if (account.equals("") || password.equals("")) {
JOptionPane.showMessageDialog(null, "请填写所有信息!");
} else {
con = new Connect().prepartion("student_system", account,passwordField.getPassword()); // 数据库连接
if (con != null) {
new LoginPanel(con);
frame.dispose();
}
}
}
}
package stu_system.event.stu_function;
import java.sql.Connection;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JTextArea;
public class Attribute {
String myNum;
Connection con;
JFrame jf ;
JTextArea showArea ;
JButton searchInfoButton ;
JButton searchGradeButton ;
JButton returnButton ;
JButton updateInfoButton ;
JButton updatePasswordButton;
public Attribute(String myNum, Connection con, JFrame jf, JTextArea showtArea, JButton searchInfoButton,
JButton searchGradeButton, JButton returnButton, JButton updateInfoButton, JButton updatePasswordButton) {
this.myNum = myNum;
this.con = con;
this.jf = jf;
this.showArea = showtArea;
this.searchInfoButton = searchInfoButton;
this.searchGradeButton = searchGradeButton;
this.returnButton = returnButton;
this.updateInfoButton = updateInfoButton;
this.updatePasswordButton = updatePasswordButton;
}
}
package stu_system.event.stu_function;
import java.awt.Color;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
public class ButtonClickCarton implements MouseListener {
Attribute attr;
int flag;
public ButtonClickCarton(Attribute attr, int flag) {
this.attr = attr;
this.flag = flag;
}
@Override
public void mouseClicked(MouseEvent arg0) {
// TODO 自动生成的方法存根
}
@Override
public void mouseEntered(MouseEvent arg0) {
switch (flag) {
case 0:
attr.searchInfoButton.setForeground(Color.red);
break;
case 1:
attr.searchGradeButton.setForeground(Color.red);
break;
case 2:
attr.updatePasswordButton.setForeground(Color.red);
break;
case 3:
attr.updateInfoButton.setForeground(Color.red);
break;
case 4:
attr.returnButton.setForeground(Color.red);
default:
break;
}
}
@Override
public void mouseExited(MouseEvent e) {
switch (flag) {
case 0:
attr.searchInfoButton.setForeground(Color.black);
break;
case 1:
attr.searchGradeButton.setForeground(Color.black);
case 2:
attr.updatePasswordButton.setForeground(Color.black);
break;
case 3:
attr.updateInfoButton.setForeground(Color.black);
break;
case 4:
attr.returnButton.setForeground(Color.black);
default:
break;
}
}
@Override
public void mousePressed(MouseEvent arg0) {
// TODO 自动生成的方法存根
}
@Override
public void mouseReleased(MouseEvent arg0) {
// TODO 自动生成的方法存根
}
}
package stu_system.event.stu_function;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import stu_system.gui.LoginPanel;
public class ReturnClick implements ActionListener{
Attribute attr;
public ReturnClick(Attribute attr) {
this.attr = attr;
}
@Override
public void actionPerformed(ActionEvent arg0) {
new LoginPanel(attr.con);
attr.jf.dispose();
}
}
package stu_system.event.stu_function;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.List;
import javax.swing.JOptionPane;
import stu_system.tool.SqlSearch;
public class SearchClick implements ActionListener {
Attribute attr;
int flag;
public SearchClick(Attribute attr, int flag) {
this.attr = attr;
this.flag = flag;
}
@Override
public void actionPerformed(ActionEvent e) {
attr.showArea.setText("");
String sql = null;
switch (flag) {
case 0:
sql = "select * from information where num=?";
break;
case 1:
sql = "select chinese_grade,math_grade,english_grade from grade where num=?";
break;
default:
break;
}
List<StringBuilder> list = SqlSearch.SelectAll(sql, attr.con, attr.myNum); // 查询得到的结果
if (list.size() == 0) {
JOptionPane.showMessageDialog(attr.jf, "查无此人!");
} else {
String[] set = null;
switch (flag) {
case 0:
set = new String[] { "学号:", "姓名:", "电话:", "地址:" };
break;
case 1:
set = new String[] { "语文成绩:", "数学成绩:", "英语成绩:" };
break;
default:
break;
}
int index = 0;
for (StringBuilder builder : list) { // 一行数据,我们要将每一列都单独拿出来
String[] split = builder.toString().split("\t");
for (String str : split) {
attr.showArea.append("\n" + set[index++] + str + "\n");
}
}
}
}
}
package stu_system.event.stu_function;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JOptionPane;
import stu_system.tool.SqlSearch;
public class UpdateInfoClick implements ActionListener {
Attribute attr;
public UpdateInfoClick(Attribute attr) {
this.attr = attr;
}
@Override
public void actionPerformed(ActionEvent e) {
Object[] options = { "姓名", "电话", "地址" };
int i = JOptionPane.showOptionDialog(null, "修改哪项信息?", "选择", JOptionPane.YES_NO_OPTION,
JOptionPane.QUESTION_MESSAGE, null, options, null);
String up = JOptionPane.showInputDialog("修改【" + options[i] + "】为:");
boolean update = false;
if (i == 0) { // 名字
String sql2 = "update chinese_grade set 姓名=? where 学号=?";
String sql3 = "update math_grade set 姓名=? where 学号=?";
String sql4 = "update english_grade set 姓名=? where 学号=?";
String sql5 = "update information set name=? where num=?";
update = SqlSearch.batchUpdate(new String[] { sql2, sql3, sql4, sql5 }, attr.con, up, attr.myNum);
} else {
String sql = null;
if (i == 1) {
sql = "update information set tele=? where 学号=?";
} else if (i == 2) {
sql = "update information set address=? where 学号=?";
}
update = SqlSearch.Update(sql, attr.con, up, attr.myNum);
}
if (update) {
JOptionPane.showMessageDialog(attr.jf, "【" + options[i] + "】修改成功!");
} else {
JOptionPane.showMessageDialog(attr.jf, "【" + options[i] + "】修改失败!");
}
}
}
package stu_system.event.stu_function;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JOptionPane;
import stu_system.gui.LoginPanel;
import stu_system.tool.SqlSearch;
public class UpdatePasswordClick implements ActionListener{
Attribute attr;
public UpdatePasswordClick(Attribute attr) {
this.attr = attr;
}
@Override
public void actionPerformed(ActionEvent arg0) {
String sql = "update stu set password=? where num=?";
String pw = JOptionPane.showInputDialog("新密码:");
boolean update = SqlSearch.Update(sql, attr.con, pw, attr.myNum);
if (update) {
JOptionPane.showMessageDialog(attr.jf, "密码修改成功!请重新登录");
new LoginPanel(attr.con);
attr.jf.dispose();
}else {
JOptionPane.showMessageDialog(attr.jf, "密码修改失败!");
}
}
}
package stu_system.tool;
import java.sql.Connection;
import java.sql.DriverManager;
import javax.swing.JOptionPane;
public class Connect {
Connection con=null;
// 数据库连接准备阶段
public Connection prepartion( String DataBase, String user, char[] pw) {
String password=new String(pw);
// 将数据库与JAVA建立通路
try {
String URL = "jdbc:mysql://127.0.0.1:3306/" + DataBase + "?characterEncoding=UTF-8";
// 返回与给定字符串名称的类或接口相关联的类对象
Class.forName("com.mysql.jdbc.Driver");
// 数据库具体连接地址、用户名、密码
con = DriverManager.getConnection(URL, user, password);
} catch (Exception e) {
JOptionPane.showMessageDialog(null, "账号密码错误!");
return null;
}
return con;
}
}
package stu_system.tool;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import com.mysql.jdbc.ResultSetMetaData;
public class SqlSearch {
public static void Bind(PreparedStatement ps, Object... objects) throws SQLException {
for (int i = 0; i < objects.length; i++) {
ps.setString(i + 1, (String) objects[i]);
}
}
// 更新操作
public static boolean Update(String sql, Connection con, String... newData) {
PreparedStatement ps = null;
try {
ps = con.prepareStatement(sql);
Bind(ps, newData); // 绑定SQL
ps.executeUpdate(); // 执行更新操作
return true;
} catch (SQLException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
} finally {
try {
if (ps != null) {
ps.close();
}
} catch (SQLException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
}
return false;
}
public static boolean batchUpdate(String[] sql, Connection con, String... newData) {
PreparedStatement ps = null;
try {
con.setAutoCommit(false); // 不让SQL自动提交,事务操作
for (int i = 0; i < sql.length; i++) {
ps = con.prepareStatement(sql[i]);
Bind(ps, newData);
ps.executeUpdate();
}
con.commit();
return true;
} catch (SQLException e) {
try {
con.rollback(); // 回滚到没有执行时候的状态
} catch (SQLException e1) {
// TODO 自动生成的 catch 块
e1.printStackTrace();
}
// TODO 自动生成的 catch 块
e.printStackTrace();
} finally {
try {
if (ps != null) {
ps.close();
}
con.setAutoCommit(true); // 改回自动提交
} catch (Exception e2) {
// TODO: handle exception
}
}
return false;
}
// 根据参数查询并返回第一个数据
public static String dataByAnother(String sql, Connection con, String...account) {
PreparedStatement ps = null; // 将sql与ps绑定
ResultSet rs = null;
try {
ps = con.prepareStatement(sql);
Bind(ps, account);
rs = ps.executeQuery();
if (rs.next()) {
return rs.getString(1); // 将查询到的结果返回
} else {
return null;
}
} catch (SQLException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
} finally {
try {
if (ps != null) {
ps.close();
}
if (rs != null) {
rs.close();
}
} catch (SQLException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
}
return null;
}
// 查询得到所有数据
public static List<StringBuilder> SelectAll(String sql, Connection con, String... account) {
PreparedStatement ps = null;
ResultSet rs = null;
try {
List<StringBuilder> list = new ArrayList<StringBuilder>();
ps = con.prepareStatement(sql);
if (account != null) {
Bind(ps, account);
}
rs = ps.executeQuery();
java.sql.ResultSetMetaData metaData = rs.getMetaData(); // 结果集工具类
int columnCount = metaData.getColumnCount(); // 列数
while (rs.next()) {
StringBuilder builder = new StringBuilder();
int index = 1;
while (index <= columnCount) {
builder.append(rs.getString(index) + "\t");
index++;
}
list.add(builder);
}
return list;
} catch (SQLException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
} finally {
try {
if (ps != null) {
ps.close();
}
if (rs != null) {
rs.close();
}
} catch (SQLException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
}
return null;
}
}
代码量较大,但是其中的逻辑其实不很难理解,只要愿意静下心来应该都是可以搞懂的,背景图部分各位自己找即可,我这里就不提供了。
码字不易,如果有帮助请来个赞鼓励一下吧!
百度云jar包可执行文件,配合讲解使用
链接:https://pan.baidu.com/s/1a01Sf7DqP-dTC_65wWbbXw
提取码:dl72