首页 > 基础资料 博客日记
JAVA学生信息管理系统(数据库实现)
2024-01-05 21:32:07基础资料围观215次
文章JAVA学生信息管理系统(数据库实现)分享给大家,欢迎收藏Java资料网,专注分享技术知识
简单介绍:
这次的项目是用数据库实现学生的信息管理系统,有三步组成,写项目>>链接数据库>>实现相关的操作
开发工具:eclipse、MySQL、navicat、mysql-connector-java-8.0.27
1.数据库建表
-- 创建数据库
CREATE DATABASE niit
DEFAULT CHARACTER SET utf8mb4;
SHOW DATABASES;
-- 使用数据库niit
USE niit;
-- 创建表Student
CREATE 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;
2,相关代码(主页面+增删查改)
(1)主页面
package studentManage;
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函数才可以单独运行
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("学生管理系统");
// 由于这是主页面,所有当主页面关闭的时候,程序就直接退出
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("学生管理系统");
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 注册监听器
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);
}
}
(2)添加界面
package studentManage;
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, 220, 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("添加");
// 添加鼠标监听事件
addButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
// 先取出数据
// getText() 返回字符串类型的
// Integer.parseInt 数据类型转换
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);
}
}
(3)删除界面
package studentManage;
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);
}
}
(4)修改界面
package studentManage;
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, "修改失败!");
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 是直接读入的,所以就不需要从数据库中读出了
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);
}
}
(5)查找界面
package studentManage;
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
// 因为学生的学号是唯一的,所以我们根据学号查询后,只会有一条数据
// 转化一下数据类型
int id = Integer.parseInt(searchField.getText());
// 2.执行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);
}
}
(6)数据库链接
package studentManage;
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 = "123456";
private Connection connection = null;
private PreparedStatement pStatement = null;
private ResultSet rSet = null;
// 加载驱动
// 静态初始化块(只执行一次)
static {
try {
Class.forName(JDBC_DRIVER);
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
// 链接数据库
public void connectDB() {
try {
connection = DriverManager.getConnection(DB_URL,username,password);
System.out.println("数据库链接成功");
} catch (SQLException e) {
System.out.println("数据库链接失败");
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 无论是否抛出异常都将执行 finally 中的语句
try {
// 先链接到数据库
connectDB();
// sql 语句
// 静态 sql 语句需要进行字符串拼接
// 动态 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);
// 三种方法执行
// 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();
}
}
}
package studentManage;
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;
}
}
package studentManage;
import java.sql.*;
public class JDBC{
public static void main(String[] args) throws Exception {
try {
// 注册 JDBC 驱动
Class.forName("com.mysql.cj.jdbc.Driver");
// 打开链接
Connection conn = DriverManager.getConnection("jdbc:conn-local://localhost:3306/text?useSSL=false&serverTimezone=UTC", "SQ", "109036");
// 执行查询
Statement stmt = conn.createStatement();
String sql = "SELECT id, name, url FROM text";
ResultSet rs = stmt.executeQuery(sql);
// 展开结果集数据库
while (rs.next()) {
// 通过字段检索
int id = rs.getInt("id");
String name = rs.getString("name");
String url = rs.getString("url");
// 输出数据
System.out.print("ID: " + id);
System.out.print(" NAME " + name);
System.out.print(" URL: " + url);
System.out.print("\n");
}
// 释放资源
rs.close();
stmt.close();
conn.close();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
}
添加Java驱动包,在官方网站下载mysql-connector-java-8.0.27,根据自己版本下载
3. 数据库链接成功,完成项目的简单操作
文章来源:https://blog.csdn.net/m0_61364659/article/details/122343769
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:jacktools123@163.com进行投诉反馈,一经查实,立即删除!
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:jacktools123@163.com进行投诉反馈,一经查实,立即删除!
标签:
上一篇:DNS服务器的安装与配置(VM虚拟机)
下一篇:Mybatis配置详解