学生信息管理系统(JAVA+MYSQL)
基于Java swing+MySQL實現學生信息管理系統:功能:1錄入學生基本信息的功能; 2查詢學生基本信息的功能; 3修改學生基本信息的功能 ;4刪除學生基本信息的功能 ;5顯示所有學生信息的功能;應付一般課設足矣,分享給大家。
如有需要鏈接:https://pan.baidu.com/s/19j84pMKe3c-cQZjn3tvBtw?pwd=m28e?
提取碼:m28e
里面包括了所有代碼源文件+mysql8.0.25驅動jar包+登錄頁面時的背景圖345.jpg
1.開發環境:jdk11+win10+mysql 8+IDEA
記得將數據庫與IDEA或者eclipse連接起來,并記得添加數據庫驅動jar包
這兩個分別是添加jar包和idea連接mysql大家可以做一下參考
IDEA導入mysql數據庫驅動_跟著太陽.的博客-CSDN博客https://blog.csdn.net/qq_54705917/article/details/123484397?spm=1001.2014.3001.5502IDEA連接mysql數據庫_跟著太陽.的博客-CSDN博客https://blog.csdn.net/qq_54705917/article/details/123484737?spm=1001.2014.3001.5502?
2.數據庫設計
?代碼:
庫:create database?student
表:create table stu(
stuId varchar(20),
stuName varchar(20),
stuSex varchar(20),
stuAge varchar(20),
stuJG varchar(20),
stuLX varchar(20),
stuBJ varchar(20)
);
3. 窗口及功能設計
(1).主函數
main.java
import javax.swing.*;public class main {public static void main(String[] args) {JFrame jf = new StuLogin();} }(2).登錄界面(默認的賬號密碼都為:admin)
StuLogin.java
import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.*;public class StuLogin extends JFrame {private StuLogin self;private ImageIcon imageIcon;private Image image;private String userid;// 登陸用戶名和密碼private String password;private JLabel unLabel = new JLabel("賬號:");// 登陸面板控件private JTextField unField = new JTextField();private JLabel pwLabel = new JLabel("密碼:");private JPasswordField pwField = new JPasswordField();private JButton dl = new JButton("登錄");private JButton d2 = new JButton("重置");public StuLogin() {this.self = this;this.setSize(350, 300);// 設置登陸面板設置窗口背景圖//先將contentPane設置成透明的((JPanel)getContentPane()).setOpaque(false);//再設置圖片imageIcon = new ImageIcon("345.jpg");//圖標組件image = imageIcon.getImage();JLabel imgLabel = new JLabel(imageIcon);getLayeredPane().add(imgLabel, new Integer(Integer.MIN_VALUE));imgLabel.setBounds(0,0,400,300); //背景圖片的位置this.setIconImage(image);//設置窗口圖像this.setLocation(600,300);this.setVisible(true);this.setResizable(false);this.setLayout(null); // this.getContentPane().setBackground(Color.BLACK);設置窗口背景色;//設置窗口名稱this.setTitle("學生信息管理系統");unLabel.setSize(50, 30);unLabel.setLocation(60, 40);unLabel.setForeground(Color.red);unLabel.setFont(new Font("楷體",Font.BOLD,15));unField.setSize(150, 35);unField.setLocation(110, 35);pwLabel.setSize(50, 30);pwLabel.setLocation(60, 100);pwLabel.setForeground(Color.red);pwLabel.setFont(new Font("楷體",Font.BOLD,15));pwField.setSize(150, 35);pwField.setLocation(110, 100);dl.setSize(80, 35);dl.setLocation(65, 175);dl.setBackground(Color.red);d2.setSize(80, 35);d2.setLocation(185, 175);d2.setBackground(Color.red);dl.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent e) {userid = unField.getText();password = pwField.getText();if(userid.equals("admin")&&password.equals("admin")) {self.setVisible(false); // JOptionPane.showMessageDialog(null, "登錄成功", "登錄情況",JOptionPane.PLAIN_MESSAGE);new StuManager();} else {JOptionPane.showMessageDialog(null, "賬號或密碼錯誤!", "登錄情況",JOptionPane.PLAIN_MESSAGE);}}});d2.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent e) {unField.setText("");pwField.setText("");}});this.add(unLabel);this.add(unField);this.add(pwLabel);this.add(pwField);this.add(dl);this.add(d2);} }登錄頁面的背景圖我會放到 上面分享的鏈接里
?(3).管理員界面(刪除功能在這里面)
StuManager.java
import javax.swing.*; import java.awt.*; import java.awt.event.*; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.Statement;public class StuManager extends JFrame implements ActionListener {//定義一些控件private Object[] types = {"-請選擇查詢方式-", "按學號號查詢", "姓名查詢", "性別查詢","按年齡查詢", "按籍貫查詢","按班級查詢"};private JComboBox searchType = new JComboBox(types); //創建一個組合框用來選取查詢不同的學生信息·JPanel jp1,jp2;JLabel jl1;JButton jb1,jb2,jb3,jb4;JTable jt;JScrollPane jsp;JTextField jtf1,jtf2;String strRS;StuModel sm;//定義連接數據庫的變量PreparedStatement ps;Connection ct = null;ResultSet rs = null;//構造函數public StuManager(){jp1 = new JPanel();jp1.setBackground(Color.gray);jtf1 = new JTextField(15);jtf2 = new JTextField();jtf2.setEditable(false);jb1 = new JButton("查詢");jb1.addActionListener(this);jl1 = new JLabel("總人數:");jp1.add(searchType);jp1.add(jtf1);jp1.add(jb1);jp1.add(jl1);jp1.add(jtf2);jb2 = new JButton("添加");jb2.setSize(100,500);jb2.addActionListener(this);jb3 = new JButton("修改");jb3.addActionListener(this);jb4 = new JButton("刪除");jb4.addActionListener(this);jp2 = new JPanel();jp2.add(jb2);jp2.add(jb3);jp2.add(jb4);jp2.setBackground(Color.gray);//創建模型對象sm = new StuModel();//初始化總人數strRS=String.valueOf(sm.getRowCount());jtf2.setText(strRS);//初始化表和滾動面板jt = new JTable(sm);jsp = new JScrollPane(jt);//將jsp放入到jframe中this.add(jsp);this.add(jp1,BorderLayout.PAGE_START);this.add(jp2,BorderLayout.PAGE_END);this.setTitle("學生信息管理系統"); // this.pack();this.setSize(600, 400);this.setLocation(500, 200);this.setDefaultCloseOperation(EXIT_ON_CLOSE);this.setVisible(true);}public void actionPerformed(ActionEvent arg0) {//判斷是哪個按鈕被點擊if(arg0.getSource() == jb1){System.out.println("用戶希望被查詢...");int index = searchType.getSelectedIndex();String sql = new String();if(index == 0){sql = "select * from stu ";}else if(index == 1){//因為把對表的數據封裝到StuModel中,可以比較簡單的完成查詢String Id =this.jtf1.getText().trim();//寫一個sql語句sql = "select * from stu where stuId = '"+Id+"' ";}else if(index == 2){String name =this.jtf1.getText().trim();sql = "select * from stu where stuName = '"+name+"' ";}else if(index == 3){String sex =this.jtf1.getText().trim();sql = "select * from stu where stuSex = '"+sex+"' ";}else if(index == 4){String age =this.jtf1.getText().trim();sql = "select * from stu where stuAge = '"+age+"' ";}else if(index ==5){String jg =this.jtf1.getText().trim();sql = "select * from stu where stuJG= '"+jg+"' ";}else if(index ==6){String bj =this.jtf1.getText().trim();sql = "select * from stu where stuBJ= '"+bj+"' ";}//構建一個數據模型類,并更新sm = new StuModel(sql);strRS=String.valueOf(sm.getRowCount());jtf2.setText(strRS);//更新jtablejt.setModel(sm);}//一、彈出添加界面else if(arg0.getSource() == jb2){System.out.println("添加...");StuAddDiag sa = new StuAddDiag(this,"添加學生",true);//重新再獲得新的數據模型,sm = new StuModel();strRS=String.valueOf(sm.getRowCount());jtf2.setText(strRS);jt.setModel(sm);}else if(arg0.getSource() == jb4){//二、刪除記錄//1.得到學生的IDint rowNum = this.jt.getSelectedRow();//getSelectedRow會返回給用戶點中的行//如果該用戶一行都沒有選,就返回-1if(rowNum == -1){//提示JOptionPane.showMessageDialog(this, "請選中一行");return ;}//得到學術IDString stuId = (String)sm.getValueAt(rowNum, 0);//連接數據庫,完成刪除任務try{//1.加載驅動Class.forName("com.mysql.cj.jdbc.Driver");//2.連接數據庫String url = "jdbc:mysql://localhost:3306/student";String user = "root";String passwd = "666666";ct = DriverManager.getConnection(url, user, passwd); // System.out.println("連接成功");ps = ct.prepareStatement("delete from stu where stuId = ?");ps.setString(1,stuId);ps.executeUpdate();JOptionPane.showMessageDialog(null, "刪除成功", "刪除情況",JOptionPane.PLAIN_MESSAGE);}catch(Exception e){e.printStackTrace();}finally{try{if(rs!= null){rs.close();rs = null;}if(ps!= null){ps.close();ps = null;}if(ct != null){ct.close();ct = null;}} catch(Exception e){e.printStackTrace();}}sm = new StuModel();strRS=String.valueOf(sm.getRowCount());jtf2.setText(strRS);//更新jtablejt.setModel(sm);}else if(arg0.getSource() == jb3){ // System.out.println("11111");//三、用戶希望修改int rowNum = this.jt.getSelectedRow();if(rowNum == -1){//提示JOptionPane.showMessageDialog(this, "請選擇一行");return ;}//顯示對話框 // System.out.println( "12435");StuUpDiag su = new StuUpDiag(this, "修改學生信息", true, sm, rowNum);sm = new StuModel();jt.setModel(sm);}} }??(4).模型界面
StuModel.java
/*用來刷新、呈現數據庫* 這是我的一個stu表的模型* 可以把對學生表的操作全都封裝到這個類*/ import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.Statement; import java.util.Vector; import javax.swing.table.*;public class StuModel extends AbstractTableModel{//rowData存放行數據,columnNames存放列名Vector rowData,columnNames;//Vector和ArrayList一樣,底層也是一個Object類型的數組Object[]。; 構造一個空向量,使其內部數據數組的大小為10,其標準容量增量為零//定義連接數據庫的變量Statement stat = null;Connection ct = null;ResultSet rs = null;//初始化public void init(String sql){if(sql.equals("")){sql = "select * from stu";}//中間//設置列名columnNames = new Vector();//這里是一維向量表示列;columnNames.add("學號");columnNames.add("名字");columnNames.add("性別");columnNames.add("年齡");columnNames.add("籍貫");columnNames.add("聯系方式");columnNames.add("班級");//rowData存放多行rowData = new Vector();try{//1.加載驅動Class.forName("com.mysql.cj.jdbc.Driver");System.out.println("加載成功");//2.連接數據庫//定義幾個常量String url = "jdbc:mysql://localhost:3306/student";String user = "root";String passwd = "666666";//這里你要填寫你自己的數據庫密碼ct = DriverManager.getConnection(url,user,passwd);stat = ct.createStatement();//創建stat對象rs = stat.executeQuery(sql);//查詢結果while(rs.next()){Vector hang = new Vector();hang.add(rs.getString(1));hang.add(rs.getString(2));hang.add(rs.getString(3));hang.add(rs.getString(4));hang.add(rs.getString(5));hang.add(rs.getString(6));hang.add(rs.getString(7));//加入到rowData中rowData.add(hang);//這里是二維向量,表示行;}}catch(Exception e){e.printStackTrace();}finally{try{if(rs!=null){rs.close();rs = null;}if(stat != null){stat.close();stat = null;}if(ct != null){ct.close();ct = null;}}catch(Exception e){e.printStackTrace();}}}//第二個構造函數,通過傳遞的sql語句來獲得數據模型public StuModel(String sql){this.init(sql);}//構造函數,用于初始化我的數據模型(表)public StuModel(){this.init("");}//得到共有多少行public int getRowCount() {// TODO Auto-generated method stubreturn this.rowData.size();}//得到共有多少列public int getColumnCount() {// TODO Auto-generated method stubreturn this.columnNames.size();}//得到某行某列的數據public Object getValueAt(int row, int column) {// TODO Auto-generated method stubreturn ((Vector)(this.rowData.get(row))).get(column);}//得到屬性名字public String getColumnName(int column) {// TODO Auto-generated method stubreturn (String)this.columnNames.get(column);} }??(5).增加學生界面
StuAddDiag.java
import javax.swing.JDialog; import javax.swing.*; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.sql.Statement; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.*; //用來實現增添讀者功能 public class StuAddDiag extends JDialog implements ActionListener {//定義我需要的swing組件JLabel jl1,jl2,jl3,jl4,jl5,jl6,jl7;JTextField jf1,jf2,jf3,jf4,jf5,jf6,jf7;JPanel jp1,jp2,jp3;JButton jb1,jb2;//owner代筆父窗口,title是窗口的名字,modal指定是模式窗口()或者非模式窗口public StuAddDiag(Frame owner, String title, boolean modal){//調用父類方法super(owner,title,modal);jl1 = new JLabel("學號");jl2 = new JLabel("名字");jl3 = new JLabel("性別");jl4 = new JLabel("年齡");jl5 = new JLabel("籍貫");jl6 = new JLabel("聯系方式");jl7 = new JLabel("班級");jf1 = new JTextField(30);jf2 = new JTextField(30);jf3 = new JTextField(30);jf4 = new JTextField(30);jf5 = new JTextField(30);jf6 = new JTextField(30);jf7 = new JTextField(30);jb1 = new JButton("添加");jb1.addActionListener(this::actionPerformed);jb2 = new JButton("取消");jb2.addActionListener(this::actionPerformed);jp1 = new JPanel();jp2 = new JPanel();jp3 = new JPanel();//設置布局jp1.setLayout(new GridLayout(7,1));jp2.setLayout(new GridLayout(7,1));jp3.add(jb1);jp3.add(jb2);jp1.add(jl1);jp1.add(jl2);jp1.add(jl3);jp1.add(jl4);jp1.add(jl5);jp1.add(jl6);jp1.add(jl7);jp2.add(jf1);jp2.add(jf2);jp2.add(jf3);jp2.add(jf4);jp2.add(jf5);jp2.add(jf6);jp2.add(jf7);this.add(jp1, BorderLayout.WEST);this.add(jp2, BorderLayout.CENTER);this.add(jp3, BorderLayout.SOUTH);this.setLocation(600, 350);this.setSize(300,200);this.setVisible(true);}@Overridepublic void actionPerformed(ActionEvent e) {// TODO Auto-generated method stubif(e.getSource() == jb1){Connection ct = null;PreparedStatement pstmt = null;ResultSet rs = null;try{//1.加載驅動Class.forName("com.mysql.cj.jdbc.Driver");System.out.println("加載成功");//2.連接數據庫//定義幾個常量String url = "jdbc:mysql://localhost:3306/student";String user = "root";String passwd = "666666";ct = DriverManager.getConnection(url,user,passwd);//與編譯語句對象String strsql = "insert into stu values(?,?,?,?,?,?,?)";pstmt = ct.prepareStatement(strsql);//給對象賦值pstmt.setString(1,jf1.getText());pstmt.setString(2,jf2.getText());pstmt.setString(3,jf3.getText());pstmt.setString(4,jf4.getText());pstmt.setString(5,jf5.getText());pstmt.setString(6,jf6.getText());pstmt.setString(7,jf7.getText());pstmt.executeUpdate();JOptionPane.showMessageDialog(null, "添加成功", "添加情況",-1);this.dispose();//關閉學生對話框}catch(Exception arg1){arg1.printStackTrace();}finally{try{if(rs!=null){rs.close();rs = null;}if(pstmt != null){pstmt.close();pstmt = null;}if(ct != null){ct.close();ct = null;}}catch(Exception arg2){arg2.printStackTrace();}}}else{this.dispose();}} }??(6).修改學生界面
StuUpDiag.java
import javax.swing.JDialog; import javax.swing.*;import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.sql.Statement; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.*; /* // * 是修改學生信息*/ public class StuUpDiag extends JDialog implements ActionListener {//定義我需要的swing組件JLabel jl1,jl2,jl3,jl4,jl5,jl6,jl7;JTextField jf1,jf2,jf3,jf4,jf5,jf6,jf7;JPanel jp1,jp2,jp3;JButton jb1,jb2;//owner代筆父窗口,title是窗口的名字,modal指定是模式窗口()或者非模式窗口public StuUpDiag(Frame owner, String title, boolean modal, StuModel sm, int rowNum){//調用父類方法super(owner,title,modal);jl1 = new JLabel("學號");jl2 = new JLabel("名字");jl3 = new JLabel("性別");jl4 = new JLabel("年齡");jl5 = new JLabel("籍貫");jl6 = new JLabel("聯系方式");jl7 = new JLabel("班級");jf1 = new JTextField(30);jf1.setText((sm.getValueAt(rowNum, 0)).toString());jf2 = new JTextField(30);jf2.setText((String)sm.getValueAt(rowNum, 1));jf3 = new JTextField(30);jf3.setText(sm.getValueAt(rowNum, 2).toString());jf4 = new JTextField(30);jf4.setText((sm.getValueAt(rowNum, 3)).toString());jf5 = new JTextField(30);jf5.setText((String)sm.getValueAt(rowNum, 4));jf6 = new JTextField(30);jf6.setText((String)sm.getValueAt(rowNum, 5));jf7 = new JTextField(30);jf7.setText((String)sm.getValueAt(rowNum, 6));jb1 = new JButton("修改");jb1.addActionListener(this::actionPerformed);jb2 = new JButton("取消");jb2.addActionListener(this::actionPerformed);jp1 = new JPanel();jp2 = new JPanel();jp3 = new JPanel();//設置布局jp1.setLayout(new GridLayout(7,1));jp2.setLayout(new GridLayout(7,1));jp3.add(jb1);jp3.add(jb2);jp1.add(jl1);jp1.add(jl2);jp1.add(jl3);jp1.add(jl4);jp1.add(jl5);jp1.add(jl6);jp1.add(jl7);jp2.add(jf1);jp2.add(jf2);jp2.add(jf3);jp2.add(jf4);jp2.add(jf5);jp2.add(jf6);jp2.add(jf7);this.add(jp1, BorderLayout.WEST);this.add(jp2, BorderLayout.CENTER);this.add(jp3, BorderLayout.SOUTH);this.setLocation(600, 350);this.setSize(300,200);this.setVisible(true);}@Overridepublic void actionPerformed(ActionEvent e) {// TODO Auto-generated method stubif(e.getSource() == jb1){Connection ct = null;PreparedStatement pstmt = null;ResultSet rs = null;try{//1.加載驅動Class.forName("com.mysql.cj.jdbc.Driver");System.out.println("加載成功");//2.連接數據庫//定義幾個常量String url = "jdbc:mysql://localhost:3306/student";String user = "root";String passwd = "666666";ct = DriverManager.getConnection(url,user,passwd);//與編譯語句對象String strsql = "update stu set stuName = '"+jf2.getText()+"',stuSex = '"+jf3.getText()+"',stuAge = '"+jf4.getText()+"',stuJG='"+jf5.getText()+"',stuLX='"+jf6.getText()+"',stuBJ='"+jf7.getText()+"' where stuId = '"+jf1.getText()+"'";pstmt = ct.prepareStatement(strsql);pstmt.executeUpdate();JOptionPane.showMessageDialog(null, "修改成功", "修改情況",JOptionPane.PLAIN_MESSAGE);this.dispose();//關閉學生對話框}catch(Exception arg1){arg1.printStackTrace();}finally{try{if(rs!=null){rs.close();rs = null;}if(pstmt != null){pstmt.close();pstmt = null;}if(ct != null){ct.close();ct = null;}}catch(Exception arg2){arg2.printStackTrace();}}}else{this.dispose();//關閉學生對話框}}}總結
以上是生活随笔為你收集整理的学生信息管理系统(JAVA+MYSQL)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Leetcode刷题(6)有效的括号
- 下一篇: python对象序列化或持久化的方法