免费99精品国产自在现线观看_人妻少妇精品视频区性色_丝袜 屁股 在线 国产_无码视频在线免费观看

Java 項目 學生信息管理系統(tǒng) 超詳細 超完整 保姆級講解

Java 學生信息管理系統(tǒng)

關注博主不迷路,博主帶你碼代碼!

1. 項目概述

這是一個學生信息管理系統(tǒng),學生信息主要包括:學號,姓名,性別,出生日期,班級;暫時就只有這些信息,若后續(xù)需要,可以相應的進行添加。

該項目主要采用的技術是 Java GUI 設計,這個地方主要用 Java 工具 windowsbuilder 進行布局,(windowsbuilder沒有安裝或者不會安裝的,可以看看我的這篇博客 Eclipse 安裝 windowbuilder插件 ),MySql 數(shù)據庫(Mysql 數(shù)據庫沒有安裝的 或者數(shù)據庫使用軟件 Navicat 沒有安裝的可以看看我的這篇博客 2021 MySql 8.0.26 安裝教程(最詳細的傻瓜教程) ),以及使用 Java 中的 JDBC 將數(shù)據庫鏈接起來。

2. 數(shù)據庫的建立

由于在項目中沒有創(chuàng)建學生數(shù)據庫的功能,所以學生數(shù)據庫必須要先建立好,下面給出如何建立好MySQL學生數(shù)據庫的相關代碼 圖片

-- 創(chuàng)建數(shù)據庫CREATE DATABASE niitDEFAULT CHARACTER SET utf8mb4;SHOW DATABASES;-- 使用數(shù)據庫niitUSE niit;-- 創(chuàng)建表StudentCREATE TABLE Student( ID INT PRIMARY KEY, NAME VARCHAR(4), GENDER CHAR(1), DOB DATE, BATCH INT);-- 插入記錄INSERT INTO Student VALUES(1,'獨孤求敗','男','1988-1-2',1),(2,'周芷若','女','1988-12-3',1),(3,'金毛獅王','男','1978-11-1',1),(4,'蒙摯','男','1989-2-5',2),(5,'夏春','女','1998-4-3',2),(6,'梅長蘇','男','1999-12-6',2);-- 查看表中所有記錄 語法:select * from 表名SELECT * FROM Student;

Java 項目 學生信息管理系統(tǒng) 超詳細 超完整 保姆級講解

3. 項目頁面

主頁面

Java 項目 學生信息管理系統(tǒng) 超詳細 超完整 保姆級講解

添加學生

Java 項目 學生信息管理系統(tǒng) 超詳細 超完整 保姆級講解

修改信息

Java 項目 學生信息管理系統(tǒng) 超詳細 超完整 保姆級講解

刪除學生

Java 項目 學生信息管理系統(tǒng) 超詳細 超完整 保姆級講解

查詢信息

Java 項目 學生信息管理系統(tǒng) 超詳細 超完整 保姆級講解

4. 項目代碼 分析

主頁面

package studentsmanager;import java.awt.Color;import java.awt.EventQueue;import java.awt.Font;import javax.swing.JButton;import javax.swing.JFrame;import javax.swing.JLabel;import javax.swing.JPanel;import javax.swing.border.EmptyBorder;import java.awt.event.ActionListener;import java.awt.event.ActionEvent;public class MainFrame extends JFrame { private JPanel contentPane; /** * Launch the application. */ // 有了main函數(shù)才可以單獨運行 public static void main(String[] args) { EventQueue.invokeLater(new Runnable() { public void run() { try { MainFrame frame = new MainFrame(); frame.setVisible(true); } catch (Exception e) { e.printStackTrace(); } } }); } /** * Create the frame. */ public MainFrame() { // 設置大小不可改變 setResizable(false); // 設置標題 setTitle("學生管理系統(tǒng)"); // 由于這是主頁面,所有當主頁面關閉的時候,程序就直接退出 setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // setBounds(int x,int y,int width,int height); // x 和 y 是窗口打開時的位置 // width 和 height 是窗口打寬度和高度 setBounds(100, 100, 450, 350); // setLocationRelativeTo(c) // 設置窗口相對于 c 的位置,當 c 為空或者 null 時,默認為是相對于屏幕中央 setLocationRelativeTo(null); // 實例化一個 pane contentPane = new JPanel(); contentPane.setBorder(new EmptyBorder(5, 5, 5, 5)); setContentPane(contentPane); contentPane.setLayout(null); JLabel lblNewLabel = new JLabel("學生管理系統(tǒng)"); lblNewLabel.setForeground(Color.RED); lblNewLabel.setFont(new Font("宋體", Font.BOLD, 25)); lblNewLabel.setBounds(140, 10, 163, 44); contentPane.add(lblNewLabel); JButton addButton = new JButton("添加學生"); // addActionListener 注冊監(jiān)聽器 addButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { // 設置新窗口可見 new AddFrame().setVisible(true); } }); addButton.setFont(new Font("宋體", Font.PLAIN, 18)); addButton.setBounds(167, 64, 114, 37); contentPane.add(addButton); JButton changeButton = new JButton("修改信息"); changeButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { // 同上 new ChangeFrame().setVisible(true); } }); changeButton.setFont(new Font("宋體", Font.PLAIN, 18)); changeButton.setBounds(167, 121, 114, 37); contentPane.add(changeButton); JButton deleteButton = new JButton("刪除學生"); deleteButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { // 同上 new DeleteFrame().setVisible(true); } }); deleteButton.setFont(new Font("宋體", Font.PLAIN, 18)); deleteButton.setBounds(167, 180, 114, 37); contentPane.add(deleteButton); JButton queryButton = new JButton("查詢信息"); queryButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { // 同上 new QueryFrame().setVisible(true); } }); queryButton.setFont(new Font("宋體", Font.PLAIN, 18)); queryButton.setBounds(167, 240, 114, 37); contentPane.add(queryButton); }}

Java 項目 學生信息管理系統(tǒng) 超詳細 超完整 保姆級講解

添加學生

package studentsmanager;import java.awt.EventQueue;import java.awt.Font;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.sql.SQLException;import javax.swing.BoxLayout;import javax.swing.JButton;import javax.swing.JFrame;import javax.swing.JLabel;import javax.swing.JOptionPane;import javax.swing.JPanel;import javax.swing.JTextField;import javax.swing.border.EmptyBorder;public class AddFrame extends JFrame { private JPanel contentPane; private JTextField idField; private JTextField nameField; private JTextField genderField; private JTextField dobField; private JTextField batchField; /** * Launch the application. */ public static void main(String[] args) { EventQueue.invokeLater(new Runnable() { public void run() { try { AddFrame frame = new AddFrame(); frame.setVisible(true); } catch (Exception e) { e.printStackTrace(); } } }); } /** * Create the frame. */ public AddFrame() { // 和前面類似 setResizable(false); setTitle("添加學生"); // 這個地方,添加學生只是這個程序的一部分,所以當關閉這部分的時候,程序不直接退出,只是關閉該部分程序 setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); setBounds(100, 100, 450, 470); setLocationRelativeTo(null); contentPane = new JPanel(); contentPane.setBorder(new EmptyBorder(5, 5, 5, 5)); setContentPane(contentPane); contentPane.setLayout(new BoxLayout(contentPane, BoxLayout.Y_AXIS)); JPanel panel = new JPanel(); contentPane.add(panel); panel.setLayout(null); JLabel TitleLabel = new JLabel("請輸入新學生的信息:"); TitleLabel.setFont(new Font("宋體", Font.BOLD, 20)); TitleLabel.setBounds(71, 34, 208, 45); panel.add(TitleLabel); JLabel idLabel = new JLabel("學號:"); idLabel.setFont(new Font("宋體", Font.PLAIN, 16)); idLabel.setBounds(71, 105, 50, 30); panel.add(idLabel); idField = new JTextField(); idField.setBounds(143, 99, 240, 45); panel.add(idField); idField.setColumns(10); JLabel nameLabel = new JLabel("姓名:"); nameLabel.setFont(new Font("宋體", Font.PLAIN, 16)); nameLabel.setBounds(71, 160, 50, 30); panel.add(nameLabel); nameField = new JTextField(); nameField.setColumns(10); nameField.setBounds(143, 154, 240, 45); panel.add(nameField); JLabel genderLabel = new JLabel("性別:"); genderLabel.setFont(new Font("宋體", Font.PLAIN, 16)); genderLabel.setBounds(71, 215, 50, 30); panel.add(genderLabel); genderField = new JTextField(); genderField.setColumns(10); genderField.setBounds(143, 209, 240, 45); panel.add(genderField); JLabel dobLabel = new JLabel("出生日期:"); dobLabel.setFont(new Font("宋體", Font.PLAIN, 16)); dobLabel.setBounds(41, 270, 80, 30); panel.add(dobLabel); dobField = new JTextField(); dobField.setColumns(10); dobField.setBounds(143, 264, 240, 45); panel.add(dobField); JLabel batchLabel = new JLabel("班級:"); batchLabel.setFont(new Font("宋體", Font.PLAIN, 16)); batchLabel.setBounds(71, 325, 50, 30); panel.add(batchLabel); batchField = new JTextField(); batchField.setColumns(10); batchField.setBounds(143, 319, 240, 45); panel.add(batchField); JButton addButton = new JButton("添加"); // 添加鼠標監(jiān)聽事件 addButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { // 先取出數(shù)據 // getText() 返回字符串類型的 // Integer.parseInt 數(shù)據類型轉換 int id = Integer.parseInt(idField.getText()); String nameString = nameField.getText(); String genderString = genderField.getText(); String dobfieldString = dobField.getText(); int batch = Integer.parseInt(batchField.getText()); // 輸出一下學生的信息,方便修改 System.out.println(id "t" nameString "t" genderString "t" dobfieldString "t" batch); // SQLHelp 是自己寫的一個工具類 SQLHelp sqlHelp = new SQLHelp(); try { // 調用添加學生信息的方法 sqlHelp.addStudent(id, nameString, genderString, dobfieldString, batch); // 彈出對話框,提示用戶添加成功 JOptionPane.showMessageDialog(AddFrame.this, "添加成功!"); } catch (SQLException e1) { // 這里捕獲一下異常,因為學生的學號是唯一的,所以當學號已經存在的時候,提示用戶,該學生信息已存在 // 異常代碼為 23000 if(e1.getSQLState().equals("23000")) { JOptionPane.showMessageDialog(AddFrame.this, "添加失敗!該學生已存在"); } e1.printStackTrace(); } } }); addButton.setFont(new Font("宋體", Font.PLAIN, 18)); addButton.setBounds(182, 389, 97, 33); panel.add(addButton); }}

添加成功:

Java 項目 學生信息管理系統(tǒng) 超詳細 超完整 保姆級講解

添加失?。?/span>

Java 項目 學生信息管理系統(tǒng) 超詳細 超完整 保姆級講解

修改信息

package studentsmanager;import java.awt.EventQueue;import java.awt.Font;import javax.swing.BoxLayout;import javax.swing.JButton;import javax.swing.JFrame;import javax.swing.JLabel;import javax.swing.JOptionPane;import javax.swing.JPanel;import javax.swing.JTextField;import javax.swing.border.EmptyBorder;import java.awt.event.ActionListener;import java.sql.SQLException;import java.awt.event.ActionEvent;public class ChangeFrame extends JFrame { private JPanel contentPane; private JTextField idField; private JTextField nameField; private JTextField genderField; private JTextField dobField; private JTextField batchField; private JTextField searchField; /** * Launch the application. */ public static void main(String[] args) { EventQueue.invokeLater(new Runnable() { public void run() { try { ChangeFrame frame = new ChangeFrame(); frame.setVisible(true); } catch (Exception e) { e.printStackTrace(); } } }); } /** * Create the frame. */ public ChangeFrame() { setResizable(false); setTitle("修改信息"); setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); setBounds(100, 100, 450, 470); setLocationRelativeTo(null); contentPane = new JPanel(); contentPane.setBorder(new EmptyBorder(5, 5, 5, 5)); setContentPane(contentPane); contentPane.setLayout(new BoxLayout(contentPane, BoxLayout.Y_AXIS)); JPanel panel = new JPanel(); contentPane.add(panel); panel.setLayout(null); JLabel lblNewLabel = new JLabel("請輸入要修改的學生的學號:"); lblNewLabel.setFont(new Font("宋體", Font.PLAIN, 16)); lblNewLabel.setBounds(71, 0, 208, 29); panel.add(lblNewLabel); JLabel lblNewLabel_1 = new JLabel("學號:"); lblNewLabel_1.setFont(new Font("宋體", Font.PLAIN, 16)); lblNewLabel_1.setBounds(71, 105, 50, 30); panel.add(lblNewLabel_1); idField = new JTextField(); idField.setEditable(false); idField.setBounds(143, 99, 240, 45); panel.add(idField); idField.setColumns(10); JLabel lblNewLabel_1_1 = new JLabel("姓名:"); lblNewLabel_1_1.setFont(new Font("宋體", Font.PLAIN, 16)); lblNewLabel_1_1.setBounds(71, 160, 50, 30); panel.add(lblNewLabel_1_1); nameField = new JTextField(); nameField.setColumns(10); nameField.setBounds(143, 154, 240, 45); panel.add(nameField); JLabel lblNewLabel_1_1_1 = new JLabel("性別:"); lblNewLabel_1_1_1.setFont(new Font("宋體", Font.PLAIN, 16)); lblNewLabel_1_1_1.setBounds(71, 215, 50, 30); panel.add(lblNewLabel_1_1_1); genderField = new JTextField(); genderField.setColumns(10); genderField.setBounds(143, 209, 240, 45); panel.add(genderField); JLabel lblNewLabel_1_1_1_1 = new JLabel("出生日期:"); lblNewLabel_1_1_1_1.setFont(new Font("宋體", Font.PLAIN, 16)); lblNewLabel_1_1_1_1.setBounds(41, 270, 80, 30); panel.add(lblNewLabel_1_1_1_1); dobField = new JTextField(); dobField.setColumns(10); dobField.setBounds(143, 264, 240, 45); panel.add(dobField); JLabel lblNewLabel_1_1_1_2 = new JLabel("班級:"); lblNewLabel_1_1_1_2.setFont(new Font("宋體", Font.PLAIN, 16)); lblNewLabel_1_1_1_2.setBounds(71, 325, 50, 30); panel.add(lblNewLabel_1_1_1_2); batchField = new JTextField(); batchField.setColumns(10); batchField.setBounds(143, 319, 240, 45); panel.add(batchField); JButton changeButton = new JButton("修改"); changeButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent arg0) { int id = Integer.parseInt(searchField.getText()); String nameString = nameField.getText(); String gendeString = genderField.getText(); String dobString = dobField.getText(); int batch = Integer.parseInt(batchField.getText()); SQLHelp sqlHelp = new SQLHelp(); try { sqlHelp.changeStudent(id, nameString, gendeString, dobString, batch); JOptionPane.showMessageDialog(ChangeFrame.this, "修改成功!"); } catch (SQLException e) { JOptionPane.showMessageDialog(ChangeFrame.this, "修改失?。?#034;); e.printStackTrace(); } } }); changeButton.setFont(new Font("宋體", Font.PLAIN, 18)); changeButton.setBounds(182, 389, 97, 33); panel.add(changeButton); searchField = new JTextField(); searchField.setBounds(71, 39, 208, 45); panel.add(searchField); searchField.setColumns(10); JButton searchButton = new JButton("查找"); searchButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e){ int id = Integer.parseInt(searchField.getText()); try { SQLHelp sqlHelp = new SQLHelp(); Student student = sqlHelp.queryStudent(id); if(student != null) { // id 是直接讀入的,所以就不需要從數(shù)據庫中讀出了 idField.setText(String.valueOf(id)); nameField.setText(student.getName()); genderField.setText(student.getGender()); dobField.setText(student.getDob()); batchField.setText(String.valueOf(student.getBatch())); } else { JOptionPane.showMessageDialog(ChangeFrame.this, "無此用戶"); } } catch (SQLException e1) { e1.printStackTrace(); } } }); searchButton.setFont(new Font("宋體", Font.PLAIN, 18)); searchButton.setBounds(289, 42, 97, 39); panel.add(searchButton); }}

修改前查詢到學生信息

Java 項目 學生信息管理系統(tǒng) 超詳細 超完整 保姆級講解

修改成功

Java 項目 學生信息管理系統(tǒng) 超詳細 超完整 保姆級講解

修改失敗

失敗的情況是數(shù)據鏈接出現(xiàn)了問題

Java 項目 學生信息管理系統(tǒng) 超詳細 超完整 保姆級講解

未查詢到學生信息

Java 項目 學生信息管理系統(tǒng) 超詳細 超完整 保姆級講解

刪除學生

package studentsmanager;import java.awt.EventQueue;import java.awt.Font;import javax.swing.BoxLayout;import javax.swing.JButton;import javax.swing.JFrame;import javax.swing.JLabel;import javax.swing.JOptionPane;import javax.swing.JPanel;import javax.swing.JTextField;import javax.swing.border.EmptyBorder;import java.awt.event.ActionListener;import java.sql.SQLException;import java.awt.event.ActionEvent;public class DeleteFrame extends JFrame { private JPanel contentPane; private JTextField idField; private JTextField nameField; private JTextField genderField; private JTextField dobField; private JTextField batchField; private JTextField searchField; /** * Launch the application. */ public static void main(String[] args) { EventQueue.invokeLater(new Runnable() { public void run() { try { DeleteFrame frame = new DeleteFrame(); frame.setVisible(true); } catch (Exception e) { e.printStackTrace(); } } }); } /** * Create the frame. */ public DeleteFrame() { setResizable(false); setTitle("刪除學生"); setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); setBounds(100, 100, 450, 470); setLocationRelativeTo(null); contentPane = new JPanel(); contentPane.setBorder(new EmptyBorder(5, 5, 5, 5)); setContentPane(contentPane); contentPane.setLayout(new BoxLayout(contentPane, BoxLayout.Y_AXIS)); JPanel panel = new JPanel(); contentPane.add(panel); panel.setLayout(null); JLabel lblNewLabel = new JLabel("請輸入要刪除的學生的學號:"); lblNewLabel.setFont(new Font("宋體", Font.PLAIN, 16)); lblNewLabel.setBounds(71, 0, 208, 29); panel.add(lblNewLabel); JLabel lblNewLabel_1 = new JLabel("學號:"); lblNewLabel_1.setFont(new Font("宋體", Font.PLAIN, 16)); lblNewLabel_1.setBounds(71, 105, 50, 30); panel.add(lblNewLabel_1); idField = new JTextField(); idField.setEditable(false); idField.setBounds(143, 99, 240, 45); panel.add(idField); idField.setColumns(10); JLabel lblNewLabel_1_1 = new JLabel("姓名:"); lblNewLabel_1_1.setFont(new Font("宋體", Font.PLAIN, 16)); lblNewLabel_1_1.setBounds(71, 160, 50, 30); panel.add(lblNewLabel_1_1); nameField = new JTextField(); nameField.setColumns(10); nameField.setBounds(143, 154, 240, 45); panel.add(nameField); JLabel lblNewLabel_1_1_1 = new JLabel("性別:"); lblNewLabel_1_1_1.setFont(new Font("宋體", Font.PLAIN, 16)); lblNewLabel_1_1_1.setBounds(71, 215, 50, 30); panel.add(lblNewLabel_1_1_1); genderField = new JTextField(); genderField.setColumns(10); genderField.setBounds(143, 209, 240, 45); panel.add(genderField); JLabel lblNewLabel_1_1_1_1 = new JLabel("出生日期:"); lblNewLabel_1_1_1_1.setFont(new Font("宋體", Font.PLAIN, 16)); lblNewLabel_1_1_1_1.setBounds(41, 270, 80, 30); panel.add(lblNewLabel_1_1_1_1); dobField = new JTextField(); dobField.setColumns(10); dobField.setBounds(143, 264, 240, 45); panel.add(dobField); JLabel lblNewLabel_1_1_1_2 = new JLabel("班級:"); lblNewLabel_1_1_1_2.setFont(new Font("宋體", Font.PLAIN, 16)); lblNewLabel_1_1_1_2.setBounds(71, 325, 50, 30); panel.add(lblNewLabel_1_1_1_2); batchField = new JTextField(); batchField.setColumns(10); batchField.setBounds(143, 319, 240, 45); panel.add(batchField); JButton deleteButton = new JButton("刪除"); deleteButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { int id = Integer.parseInt(searchField.getText()); try { SQLHelp sqlHelp = new SQLHelp(); sqlHelp.deletestudent(id); JOptionPane.showMessageDialog(DeleteFrame.this, "刪除成功!"); } catch (SQLException e1) { JOptionPane.showMessageDialog(DeleteFrame.this, "刪除失敗"); e1.printStackTrace(); } } }); deleteButton.setFont(new Font("宋體", Font.PLAIN, 18)); deleteButton.setBounds(182, 389, 97, 33); panel.add(deleteButton); searchField = new JTextField(); searchField.setBounds(71, 39, 208, 45); panel.add(searchField); searchField.setColumns(10); JButton searchButton = new JButton("查找"); searchButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent arg0) { int id = Integer.parseInt(searchField.getText()); try { SQLHelp sqlHelp = new SQLHelp(); Student student = sqlHelp.queryStudent(id); if(student != null) { idField.setText(String.valueOf(id)); nameField.setText(student.getName()); genderField.setText(student.getGender()); dobField.setText(student.getDob()); batchField.setText(String.valueOf(student.getBatch())); } else { JOptionPane.showMessageDialog(DeleteFrame.this, "無此學生"); } } catch (SQLException e) { e.printStackTrace(); } } }); searchButton.setFont(new Font("宋體", Font.PLAIN, 18)); searchButton.setBounds(289, 42, 97, 39); panel.add(searchButton); }}

刪除成功

Java 項目 學生信息管理系統(tǒng) 超詳細 超完整 保姆級講解

刪除失敗

刪除失敗的時候是數(shù)據庫鏈接出現(xiàn)了問題

Java 項目 學生信息管理系統(tǒng) 超詳細 超完整 保姆級講解

查詢到學生信息

Java 項目 學生信息管理系統(tǒng) 超詳細 超完整 保姆級講解

未查詢到學生

Java 項目 學生信息管理系統(tǒng) 超詳細 超完整 保姆級講解

查詢信息

package studentsmanager;import java.awt.EventQueue;import java.awt.Font;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.sql.SQLException;import javax.swing.BoxLayout;import javax.swing.JButton;import javax.swing.JFrame;import javax.swing.JLabel;import javax.swing.JOptionPane;import javax.swing.JPanel;import javax.swing.JTextField;import javax.swing.border.EmptyBorder;public class QueryFrame extends JFrame { private JPanel contentPane; private JTextField idField; private JTextField nameField; private JTextField genderField; private JTextField dobField; private JTextField batchField; private JTextField searchField; /** * Launch the application. */ public static void main(String[] args) { EventQueue.invokeLater(new Runnable() { public void run() { try { QueryFrame frame = new QueryFrame(); frame.setVisible(true); } catch (Exception e) { e.printStackTrace(); } } }); } /** * Create the frame. */ public QueryFrame() { setResizable(false); setTitle("查詢信息"); setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); setBounds(100, 100, 450, 470); setLocationRelativeTo(null); contentPane = new JPanel(); contentPane.setBorder(new EmptyBorder(5, 5, 5, 5)); setContentPane(contentPane); contentPane.setLayout(new BoxLayout(contentPane, BoxLayout.Y_AXIS)); JPanel panel = new JPanel(); contentPane.add(panel); panel.setLayout(null); JLabel lblNewLabel = new JLabel("請輸入要查找的學生的學號:"); lblNewLabel.setFont(new Font("宋體", Font.PLAIN, 16)); lblNewLabel.setBounds(71, 0, 208, 29); panel.add(lblNewLabel); JLabel lblNewLabel_1 = new JLabel("學號:"); lblNewLabel_1.setFont(new Font("宋體", Font.PLAIN, 16)); lblNewLabel_1.setBounds(71, 105, 50, 30); panel.add(lblNewLabel_1); idField = new JTextField(); idField.setEditable(false); idField.setBounds(143, 99, 240, 45); panel.add(idField); idField.setColumns(10); JLabel lblNewLabel_1_1 = new JLabel("姓名:"); lblNewLabel_1_1.setFont(new Font("宋體", Font.PLAIN, 16)); lblNewLabel_1_1.setBounds(71, 160, 50, 30); panel.add(lblNewLabel_1_1); nameField = new JTextField(); nameField.setColumns(10); nameField.setBounds(143, 154, 240, 45); panel.add(nameField); JLabel lblNewLabel_1_1_1 = new JLabel("性別:"); lblNewLabel_1_1_1.setFont(new Font("宋體", Font.PLAIN, 16)); lblNewLabel_1_1_1.setBounds(71, 215, 50, 30); panel.add(lblNewLabel_1_1_1); genderField = new JTextField(); genderField.setColumns(10); genderField.setBounds(143, 209, 240, 45); panel.add(genderField); JLabel lblNewLabel_1_1_1_1 = new JLabel("出生日期:"); lblNewLabel_1_1_1_1.setFont(new Font("宋體", Font.PLAIN, 16)); lblNewLabel_1_1_1_1.setBounds(41, 270, 80, 30); panel.add(lblNewLabel_1_1_1_1); dobField = new JTextField(); dobField.setColumns(10); dobField.setBounds(143, 264, 240, 45); panel.add(dobField); JLabel lblNewLabel_1_1_1_2 = new JLabel("班級:"); lblNewLabel_1_1_1_2.setFont(new Font("宋體", Font.PLAIN, 16)); lblNewLabel_1_1_1_2.setBounds(71, 325, 50, 30); panel.add(lblNewLabel_1_1_1_2); batchField = new JTextField(); batchField.setColumns(10); batchField.setBounds(143, 319, 240, 45); panel.add(batchField); searchField = new JTextField(); searchField.setBounds(71, 39, 208, 45); panel.add(searchField); searchField.setColumns(10); JButton searchButton = new JButton("查找"); searchButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent arg0) { // 1.讀出要查詢的學生輸入的id // 因為學生的學號是唯一的,所以我們根據學號查詢后,只會有一條數(shù)據 // 轉化一下數(shù)據類型 int id = Integer.parseInt(searchField.getText()); // 2.執(zhí)行JDBC語句 try { SQLHelp sqlHelp = new SQLHelp(); Student student = sqlHelp.queryStudent(id); // 3.將查詢結果填到文本框中 // 前提是學生存在 if(student != null) { idField.setText(String.valueOf(id)); nameField.setText(student.getName()); genderField.setText(student.getGender()); dobField.setText(student.getDob()); batchField.setText(String.valueOf(student.getBatch())); } else { // 此時學生不存在,提示用戶,不存在該學生 JOptionPane.showMessageDialog(QueryFrame.this, "無此學生"); } } catch (SQLException e) { e.printStackTrace(); } } }); searchButton.setFont(new Font("宋體", Font.PLAIN, 18)); searchButton.setBounds(289, 42, 97, 39); panel.add(searchButton); }}

Java 項目 學生信息管理系統(tǒng) 超詳細 超完整 保姆級講解

SQLHelp 工具類

package studentsmanager;import java.sql.Connection;import java.sql.DriverManager;import java.sql.PreparedStatement;import java.sql.ResultSet;import java.sql.SQLException;public class SQLHelp { public static final String JDBC_DRIVER = "com.mysql.cj.jdbc.Driver"; public static final String DB_URL = "jdbc:mysql://localhost:3306/niit?useSSL=false&allowPublicKeyRetrieval=true&serverTimezone=UTC"; public static final String username = "root"; public static final String password = "011216"; private Connection connection = null; private PreparedStatement pStatement = null; private ResultSet rSet = null; // 加載驅動 // 靜態(tài)初始化塊(只執(zhí)行一次) static { try { Class.forName(JDBC_DRIVER); } catch (ClassNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } } // 鏈接數(shù)據庫 public void connectDB() { try { connection = DriverManager.getConnection(DB_URL,username,password); System.out.println("數(shù)據庫鏈接成功"); } catch (SQLException e) { System.out.println("數(shù)據庫鏈接失敗"); e.printStackTrace(); } } // 關閉資源 public void close() { if(rSet != null) { try { rSet.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } if(pStatement != null) { try { pStatement.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } if(connection != null) { try { connection.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } public void addStudent(int id, String nameString, String genderString, String dobString, int batch) throws SQLException { // try finally 無論是否拋出異常都將執(zhí)行 finally 中的語句 try { // 先鏈接到數(shù)據庫 connectDB(); // sql 語句 // 靜態(tài) sql 語句需要進行字符串拼接 // 動態(tài) sql 語句 String addsql = "insert into student values(?, ?, ?, ?, ?)"; pStatement = connection.prepareStatement(addsql); pStatement.setInt(1, id); pStatement.setString(2, nameString); pStatement.setString(3, genderString); pStatement.setString(4, dobString); pStatement.setInt(5, batch); pStatement.executeUpdate(); } finally { close(); } } public void changeStudent(int id, String nameString, String genderString, String dobString, int batch) throws SQLException { try { connectDB(); String changesql = "update student set name = ?, gender = ?, dob = ?, batch = ? where id = ?"; pStatement = connection.prepareStatement(changesql); pStatement.setString(1, nameString); pStatement.setString(2, genderString); pStatement.setString(3, dobString); pStatement.setInt(4, batch); pStatement.setInt(5, id); pStatement.executeUpdate(); } finally { close(); } } public void deletestudent(int id) throws SQLException { try { connectDB(); String deleteString = "select * from student where id = ?"; pStatement = connection.prepareStatement(deleteString); pStatement.setInt(1, id); pStatement.execute(); } finally { close(); } } public Student queryStudent(int id) throws SQLException { try { connectDB(); String querysql = "select * from student where id = ?"; pStatement = connection.prepareStatement(querysql); pStatement.setInt(1, id); // 三種方法執(zhí)行 // execute() 都可以使用,返回true或false // executeQuery() 專門用于查詢,返回結果集 // executeUpdate() 專門用于刪除、更新 rSet = pStatement.executeQuery(); if(rSet.next()) { String nameString = rSet.getString(2); String genderString = rSet.getString(3); String dobString = rSet.getString(4); int batchString = rSet.getInt(5); // 查詢到學生信息返回結果集 return new Student(nameString, genderString, dobString, batchString); } else { // 沒有查詢到學生信息,返回null return null; } } finally { close(); } }}

Student 工具類

package studentsmanager;public class Student { private String name; private String gender; private String dob; private int batch; public Student(String name, String gender, String dob, int batch) { this.name = name; this.gender = gender; this.dob = dob; this.batch = batch; } public String getName() { return name; } public String getGender() { return gender; } public String getDob() { return dob; } public int getBatch() { return batch; } }

5. 項目總結

這算一個比較完整的項目了,既有 GUI 又有 MySQL 數(shù)據庫的已經 Java 的 JDBC ,雖然已經有了 GUI 界面,但很明顯,沒有進行任何美化,就只是簡單的一個界面,勉勉強強可以看的過去,使用 JDBC 鏈接數(shù)據庫的時候,沒有實現(xiàn)可以直接在程序創(chuàng)建數(shù)據庫,只能從數(shù)據庫方創(chuàng)建數(shù)據庫,這是一個缺點,同時使用 JDBC 鏈接數(shù)據庫的時候,所使用的代碼依舊是 MySQL 數(shù)據庫的代碼,(我 MySQL 學的不好,增刪改查啥的數(shù)據庫代碼都還記不清楚 哭了…)還有就是當程序在進行修改和刪除的時候,修改失敗和刪除失敗的時候,都是因為數(shù)據鏈接失敗了才進行彈窗,不知道怎么改成無法修改或者無法刪除的時候提示,(又哭了…)

歡迎個位大佬指出錯誤,同時也歡迎各位小白借鑒,有問題的話,評論或者私聊都是可以的,看了就會回復!

最后一句:

關注博主不迷路,博主帶你碼代碼!

原文地址:https://blog.csdn.net/qq_52354698/article/details/121582956

相關新聞

聯(lián)系我們
聯(lián)系我們
在線咨詢
分享本頁
返回頂部