需要不用数据库的学生管理系统的小伙伴,请移步至:java学生管理系统(swing) (没用数据库,附源码)
事先声明:代码简易,只是实现基本的增删改查功能,大佬们轻点喷
相较于我写的没带数据库的代码,这次增加了几项功能:
下面就是java代码,为了方便大家复制下来看看效果,我没分太多类
import javax.swing.*;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.lang.reflect.Array;
import java.security.spec.ECField;
import java.sql.*;
import java.util.ArrayList;
public class Gradation {
private static JFrame jf,regis_jf;
private static JPanel panel,regis_panel;
private static JLabel jl,jl2,regis_j1,regis_j2;
private static JTextField name,regis_name,regis_password;
private static JPasswordField password;
private static JButton jb,jb2,regis_jb,regis_jb2;
private static Connection con;
private static PreparedStatement sql,sql_2;
private static ResultSet res;
private static String user_name,user_name_2; //用户登录注册的账号
private static String pass_word,pass_word_2; //用户登录注册的密码
public Connection getConnection()
{
try {
Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
try {
con = DriverManager.getConnection("jdbc:mysql:"+"//127.0.0.1:3306/db1"
,"root","root"); //db1是你自己创建的数据库,表都在这里面,而一般教材都写的是test,所以不要照抄搬运
} catch (SQLException e) {
e.printStackTrace();
}
return con;
}
public static void main(String[] args) {
jf = new JFrame("测试窗口1");
jf.setBounds(300,200,300,150);
jf.setLocationRelativeTo(null);
jf.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
//绝对布局
panel = new JPanel(null);
jl = new JLabel("账户");
jl.setBounds(10,10,200,18);
name = new JTextField();
name.setBounds(80,10,150,18);
jl2 = new JLabel("密码");
jl2.setBounds(10,50,200,18);
password = new JPasswordField();
password.setBounds(80,50,150,18);
panel.add(jl);
panel.add(name);
panel.add(jl2);
panel.add(password);
//创建一个按钮,点击后获取密码框中输入的密码
jb = new JButton("登录");
jb.setBounds(80,80,60,18);
jb.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
DengLu();
}
});
panel.add(jb);
jb2 = new JButton("注册");
jb2.setBounds(170,80,60,18);
jb2.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
Register();
}
});
panel.add(jb2);
jf.setContentPane(panel);
jf.setVisible(true);
}
public static void Register()
{
regis_jf = new JFrame("注册");
regis_jf.setBounds(300,200,300,150);
regis_jf.setLocationRelativeTo(null);
regis_jf.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
//绝对布局
regis_panel = new JPanel(null);
regis_j1 = new JLabel("Account");
regis_j1.setBounds(10,10,200,18);
regis_name = new JTextField();
regis_name.setBounds(80,10,150,18);
regis_j2 = new JLabel("Passwords");
regis_j2.setBounds(10,50,200,18);
regis_password = new JTextField();
regis_password.setBounds(80,50,150,18);
regis_panel.add(regis_j1);
regis_panel.add(regis_name);
regis_panel.add(regis_j2);
regis_panel.add(regis_password);
regis_jb = new JButton("确定 ");
regis_jb.setBounds(80,80,60,18);
regis_jb.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
user_name = regis_name.getText();
pass_word = regis_password.getText();
try
{
Gradation grad_2 = new Gradation();
con = grad_2.getConnection();
sql_2 = con.prepareStatement("insert into register (user_name,pass_word) values (?,?)");
sql_2.setString(1,user_name);
sql_2.setString(2,pass_word);
sql_2.execute();
}
catch (Exception e3)
{
e3.printStackTrace();
}
JOptionPane.showMessageDialog(null,"信息注册成功");
regis_jf.dispose();
}
});
regis_panel.add(regis_jb);
regis_jf.setContentPane(regis_panel);
regis_jf.setVisible(true);
}
public static void DengLu()
{
try
{
Gradation grad = new Gradation();
con = grad.getConnection();
user_name_2 = name.getText();
pass_word_2 = new String(password.getPassword());
sql = con.prepareStatement("select user_name,pass_word from register where user_name = ? and pass_word = ?");
sql.setString(1,user_name_2);
sql.setString(2,pass_word_2);
res = sql.executeQuery();
if(name.getText().length() == 0||
new String (password.getPassword()).length() == 0)
{
JOptionPane.showMessageDialog(null,"信息为空");
}
else if(res.next())
{
new Demo();
jf.dispose(); //先关闭第一个窗口
}
else
JOptionPane.showMessageDialog(null,"登录失败,账户或密码错误");
}
catch (Exception e2)
{
e2.printStackTrace();
}
}
}
.
.
.
.
.
这是第二段java代码
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.*;
import java.security.spec.ECField;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
public class Demo {
private JFrame jf2;
private JTextArea jTextArea; //显示信息的文本区域
private File file;
//contentPane是底层面板,其余都是加在它的上面
private JPanel contentPane,found_panel,panel2,delete_panel,update_panel;
private JLabel j1,j2,j3,j4,j_delete; //新建操作的名称标签 + 删除操作的名称标签
private JLabel j_up_name,j_new_name,j_new_num,j_new_age,j_new_major; //修改操作的名称标签
private JTextField jT_1,jT_2,jT_3,jT_4,jT_delete; //新建操作的输入框 + 删除操作的输入框
private JTextField jT_up_name,jT_new_name,jT_new_num,jT_new_age,jT_new_major; //修改操作的输入框
private JButton jb_1,jb_2,found_back,jb_del,j_update,del_back,update_back;
private JMenuBar menuBar;
private JMenu inforMenu,aboutMenu;
private JMenuItem newMenItem,deleteMenItem,foundMenItem,changeMenItem,about_son;
private static Connection cen;
private static PreparedStatement prmen;
private static ResultSet rls;
private static String stu_name,stu_num,stu_age,stu_major; //所添加的学生信息
private static String stu_name_del; //所要删除学生的姓名
private static String stu_up_name,stu_up_newname,stu_up_newnum,stu_up_newage,stu_up_newmajor;
public Demo() {
//设置第二个窗口
jf2 = new JFrame("LS");
jf2.setSize(500, 300);
jf2.setLocationRelativeTo(null);
jf2.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
// 创建底部面板
contentPane = new JPanel();
// 创建“查询”所需面板
found_panel = new JPanel();
// 创建“新建”面板
panel2 = new JPanel(new GridLayout(5,2));
// 创建”删除“面板
delete_panel = new JPanel(new GridLayout(1,3));
// 创建”修改“面板
update_panel = new JPanel(new GridLayout(6,2));
// ”新建“面板中的“新建”内容
j1 = new JLabel("姓名");
jT_1 = new JTextField();
j2 = new JLabel("学号");
jT_2 = new JTextField();
j3 = new JLabel("年龄");
jT_3 = new JTextField();
j4 = new JLabel("专业");
jT_4 = new JTextField();
panel2.add(j1);
panel2.add(jT_1);
panel2.add(j2);
panel2.add(jT_2);
panel2.add(j3);
panel2.add(jT_3);
panel2.add(j4);
panel2.add(jT_4);
jb_1 = new JButton("提交");
jb_2 = new JButton("返回");
panel2.add(jb_1);
panel2.add(jb_2);
// “查询"的页面
jTextArea = new JTextArea();
//jTextArea.setBounds(10,10,30,30);
// 返回开始界面按钮
found_back = new JButton("返回");
//found_back.setBounds(20,100,20,20);
found_panel.add(jTextArea);
found_panel.add(found_back);
/*
* 创建一个菜单栏
*/
menuBar = new JMenuBar();
/*
* 创建一级菜单
*/
inforMenu = new JMenu("信息管理");
aboutMenu = new JMenu("关于");
// 一级菜单添加到菜单栏
menuBar.add(inforMenu);
menuBar.add(aboutMenu);
newMenItem = new JMenuItem("新建");
deleteMenItem = new JMenuItem("删除");
changeMenItem = new JMenuItem("修改");
foundMenItem = new JMenuItem("查询");
about_son = new JMenuItem("再关于");
// 向“信息管理”中添加子菜单
inforMenu.add(newMenItem);
inforMenu.add(deleteMenItem);
inforMenu.add(changeMenItem);
inforMenu.add(foundMenItem);
aboutMenu.add(about_son);
//删除操作的面板内容
jT_delete = new JTextField();
j_delete = new JLabel("姓名");
jb_del = new JButton("删除");
del_back = new JButton("返回");
delete_panel.add(j_delete);
delete_panel.add(jT_delete);
delete_panel.add(jb_del);
delete_panel.add(del_back);
//修改操作的面板内容
j_up_name = new JLabel("姓名");
jT_up_name = new JTextField();
j_new_name = new JLabel("新姓名");
jT_new_name = new JTextField();
j_new_num = new JLabel("新学号");
jT_new_num = new JTextField();
j_new_age = new JLabel("新年龄");
jT_new_age = new JTextField();
j_new_major = new JLabel("新专业");
jT_new_major = new JTextField();
j_update = new JButton("确定");
update_back = new JButton("返回");
update_panel.add(j_up_name);
update_panel.add(jT_up_name);
update_panel.add(j_new_name);
update_panel.add(jT_new_name);
update_panel.add(j_new_num);
update_panel.add(jT_new_num);
update_panel.add(j_new_age);
update_panel.add(jT_new_age);
update_panel.add(j_new_major);
update_panel.add(jT_new_major);
update_panel.add(j_update);
update_panel.add(update_back);
//添加事件监听
Event_do();
contentPane.add(menuBar); //将整个JMenuBar加到contentPane上
JLabel label = new JLabel("欢迎使用学生管理系统");
label.setFont(new Font(Font.DIALOG,Font.BOLD,23)); //设置字体大小
label.setForeground(Color.orange); //设置字体颜色
contentPane.add(label);
jf2.setContentPane(contentPane);
jf2.setVisible(true);
}
public void Event_do()
{
// 设置 "新建" 子菜单被点击的监听器
newMenItem.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
contentPane.removeAll();
contentPane.add(panel2);
contentPane.validate();
contentPane.repaint();
jT_1.setText("");
jT_2.setText("");
jT_3.setText("");
jT_4.setText("");
}
});
// 设置 "删除" 子菜单被点击的监听器
deleteMenItem.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
jT_delete.setText("");
//主页面跳转至删除面板
contentPane.removeAll();
contentPane.add(delete_panel);
contentPane.validate();
contentPane.repaint();
}
});
// 设置“删除面板”中“删除”的监听器
jb_del.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
//获取文本框中数据
stu_name_del = jT_delete.getText();
try
{
Gradation grad_3 = new Gradation();
cen = grad_3.getConnection(); //连接数据库
//首先查询数据库中是否有所要删除学生的信息
prmen = cen.prepareStatement("select stu_name from stu_info where stu_name = ?");
prmen.setString(1,stu_name_del);
rls = prmen.executeQuery();
if(jT_delete.getText().length() == 0)
{
JOptionPane.showMessageDialog(null,"信息为空");
}
//如果有要删除学生的信息
else if(rls.next())
{
//那么就删除他
prmen = cen.prepareStatement("delete from stu_info where stu_name = ?");
prmen.setString(1,stu_name_del);
prmen.executeUpdate();
JOptionPane.showMessageDialog(null,"删除成功");
//主页面跳转至主面板
contentPane.removeAll();
contentPane.add(menuBar);
contentPane.validate();
contentPane.repaint();
}
else
JOptionPane.showMessageDialog(null,"该生不存在");
}
catch (Exception e1)
{
e1.printStackTrace();
}
}
});
// 设置“删除面板”中“返回”的监听器
del_back.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
jT_delete.setText("");
contentPane.removeAll();
contentPane.add(menuBar);
contentPane.validate();
contentPane.repaint();
}
});
// 设置 "修改" 子菜单被点击的监听器
changeMenItem.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
//主页面跳转至修改面板
contentPane.removeAll();
contentPane.add(update_panel);
contentPane.validate();
contentPane.repaint();
jT_up_name.setText("");
jT_new_name.setText("");
jT_new_num.setText("");
jT_new_age.setText("");
jT_new_major.setText("");
}
});
// 设置“修改”面板中“确定”的监听器
j_update.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
stu_up_name = jT_up_name.getText();
stu_up_newname = jT_new_name.getText();
stu_up_newnum = jT_new_num.getText();
stu_up_newage = jT_new_age.getText();
stu_up_newmajor = jT_new_major.getText();
try
{
Gradation grad_4 = new Gradation();
cen = grad_4.getConnection(); //连接数据库
//首先查询数据库中是否有所要修改学生的信息
prmen = cen.prepareStatement("select stu_name from stu_info where stu_name = ?");
prmen.setString(1,stu_up_name);
rls = prmen.executeQuery();
if(jT_up_name.getText().length() == 0)
{
JOptionPane.showMessageDialog(null,"请输入要删除的学生姓名");
}
else if(rls.next())
{
prmen = cen.prepareStatement(
"update stu_info set stu_name = ?,stu_num = ?,stu_age = ?,stu_major = ?" +
" where stu_name = ?");
prmen.setString(1,stu_up_newname);
prmen.setString(2,stu_up_newnum);
prmen.setString(3,stu_up_newage);
prmen.setString(4,stu_up_newmajor);
prmen.setString(5,stu_up_name);
prmen.executeUpdate();
JOptionPane.showMessageDialog(null,"修改成功");
//主页面跳转至主面板
contentPane.removeAll();
contentPane.add(menuBar);
contentPane.validate();
contentPane.repaint();
}
else
JOptionPane.showMessageDialog(null,"您要修改的学生不存在");
}
catch(Exception e2)
{
e2.printStackTrace();
}
}
});
// 设置“修改”面板中“返回”的监听器
update_back.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
jT_up_name.setText("");
jT_new_name.setText("");
jT_new_num.setText("");
jT_new_age.setText("");
jT_new_major.setText("");
contentPane.removeAll();
contentPane.add(menuBar);
contentPane.validate();
contentPane.repaint();
}
});
// 设置 "查询" 子菜单被点击的监听器
foundMenItem.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
try
{
Gradation grad_4 = new Gradation();
cen = grad_4.getConnection(); //连接数据库
prmen = cen.prepareStatement("select * from stu_info"); //查询数据库
rls = prmen.executeQuery(); //执行SQL语句
while (rls.next())
{
String stu_name_fo = rls.getString("stu_name");
String stu_num_fo = rls.getString("stu_num");
String stu_age_fo = rls.getString("stu_age");
String stu_major_fo = rls.getString("stu_major");
jTextArea.append(stu_name_fo +" "+stu_num_fo+" "+
stu_age_fo+" "+stu_major_fo+" \n");
}
}
catch (Exception eb1)
{
eb1.printStackTrace();
}
contentPane.removeAll();
contentPane.add(found_panel);
contentPane.validate();
contentPane.repaint();
}
});
// “查询”界面的 “返回” 的监听器
found_back.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
jTextArea.setText("");
contentPane.removeAll();
contentPane.add(menuBar);
contentPane.validate();
contentPane.repaint();
}
});
// 新建的中的“提交”的监听事件
jb_1.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
//从输入框中获取数据
stu_name = jT_1.getText();
stu_num = jT_2.getText();
stu_age = jT_3.getText();
stu_major = jT_4.getText();
//将获取的数据存入数据库
try
{
Gradation grad_2 = new Gradation();
cen = grad_2.getConnection(); //连接数据库
prmen = cen.prepareStatement("insert into stu_info(stu_name,stu_num,stu_age,stu_major) values(?,?,?,?) ");
prmen.setString(1,stu_name);
prmen.setString(2,stu_num);
prmen.setString(3,stu_age);
prmen.setString(4,stu_major);
prmen.execute();
}
catch (Exception exo_1)
{
exo_1.printStackTrace();
}
//界面返回主页
contentPane.removeAll();
contentPane.add(menuBar);
contentPane.validate();
contentPane.repaint();
}
});
// 新建的中的“返回”的监听事件
jb_2.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
contentPane.removeAll();
contentPane.add(menuBar);
contentPane.validate();
contentPane.repaint();
}
});
about_son.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
JOptionPane.showMessageDialog(null,"仅供点击,请勿当真!");
}
});
}
}
.
.
.
.
以下是mysql的代码
# 建立注册表
CREATE TABLE register (
id INT PRIMARY KEY AUTO_INCREMENT,
user_name VARCHAR(20), -- 账号
pass_word VARCHAR(20) -- 密码
);
# 建立学生信息表
CREATE TABLE stu_info (
stu_name VARCHAR(10), -- 姓名
stu_num VARCHAR(20), -- 学号
stu_age INT, -- 年龄
stu_major VARCHAR(15) -- 专业
);
后期还会更新更好的程序,还希望大家给个赞,嘻嘻!
你的鼓励是我进步的动力