个人主页:程序员杨工
个人简介:从事软件开发多年,前后端均有涉猎,具有丰富的开发经验
博客内容:全栈开发,分享Java、Python、Php、小程序、前后端、数据库经验和实战
文末有本人名片,希望和大家一起共同努力,一起进步,顶峰相见。
源码下载见文末(本系统源码开源免费、无套路)。
开发背景:
最近好多小伙伴们找我咨询关于java、mysql代码方面的各种问题,大家一般都是刚接触编程,因此很容易遇到各种问题,在完成老师给定的小作业或者数据库设计的时候,往往无从下手。比如如何去进行swing-gui的界面设计,如何在java代码中连接数据库,如何使用jdbc去操作数据库进行增删改查数据操作。我们今天就用一个学生信息管理案例的形式,去简单介绍一下如何完成一个增删改查的小功能。
系统亮点:
简单学生信息管理系统,功能比较简单,就是一个学生信息的增删改查。
需求分析:
本系统为java+swing+mysql的简单学生信息管理系统,希望功能可以做到如下:按学号或者姓名查询学生信息;输入学生基本信息之后,点击添加可以添加新的学生;选中某一行记录之后,点击修改,可以修改学生信息;选中某行之后,点击删除,可以删除学生信息。此外,由于学号具有唯一性,因此添加学生时,需要保证学号唯一验证。
开发工具:
Jdk环境:jdk1.8
数据库版本:MySQL5.6或以上
开发工具:eclipse(Idea)
开发技术:
Java Swing、MySQL与JDBC之间的关联主要体现在Java桌面应用程序开发中,它们共同协作以实现数据的可视化展示、用户交互以及数据的存储与管理。以下是它们之间的关联和介绍:
Java Swing是Java的一个图形用户界面工具包,它提供了一套丰富的GUI组件用于构建复杂的桌面应用程序。Swing组件包括按钮、文本框、标签、菜单、表格等,这些组件允许开发者创建具有图形界面的应用程序,使用户能够通过点击、输入等方式与应用程序进行交互。
MySQL是一个流行的开源关系数据库管理系统(RDBMS),它使用SQL(结构化查询语言)来管理或操作数据库中的数据。MySQL数据库支持大型数据库,具有高性能、可靠性和易用性等特点,广泛应用于Web应用程序和桌面应用程序中。MySQL数据库能够存储、检索、更新和删除数据,为应用程序提供数据支持。
JDBC(Java Database Connectivity)是Java语言中用于与关系型数据库进行交互的API。它提供了一组类和接口,允许Java应用程序连接到数据库,执行SQL语句,并处理结果。JDBC使得Java应用程序能够以一种统一的方式与不同的数据库进行交互,而不需要针对每种数据库编写特定的代码。JDBC还提供了事务管理、批处理操作和元数据访问等功能,增强了数据库操作的灵活性和效率。
数据库设计:
数据库设计很简单,为了保存学生基本信息,设计了一个学生表,字段如下:
DROP TABLE IF EXISTS `t_student`;
CREATE TABLE `t_student` (
`id` int(11) NOT NULL AUTO_INCREMENT COMMENT '学生ID',
`username` varchar(32) DEFAULT NULL COMMENT '学号',
`nickname` varchar(32) DEFAULT NULL COMMENT '姓名',
`sex` int(11) DEFAULT NULL COMMENT '性别:0男,1女',
`birthday` varchar(32) DEFAULT NULL COMMENT '生日',
`telephone` varchar(32) DEFAULT NULL COMMENT '电话',
`email` varchar(32) DEFAULT NULL COMMENT '邮箱',
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8 COMMENT='学生表';
系统实现:
系统功能包含学生查询、添加、修改、删除。这里我们简单实现,在一个界面上完成这些功能,如下:
学生管理
添加
删除
代码实现:
实体类
package com.xiaoniucr.entity;
import java.io.Serializable;
import java.util.Date;
/**
* 用户信息
*/
public class Student implements Serializable{
/**
* ID
*/
private Integer id;
/**
* 账号
*/
private String username;
/**
* 姓名
*/
private String nickname;
/**
* 性别: 0男,1女
*/
private Integer sex;
/**
* 生日
*/
private String birthday;
/**
* 电话
*/
private String telephone;
/**
* 邮箱
*/
private String email;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getNickname() {
return nickname;
}
public void setNickname(String nickname) {
this.nickname = nickname;
}
public Integer getSex() {
return sex;
}
public void setSex(Integer sex) {
this.sex = sex;
}
public String getBirthday() {
return birthday;
}
public void setBirthday(String birthday) {
this.birthday = birthday;
}
public String getTelephone() {
return telephone;
}
public void setTelephone(String telephone) {
this.telephone = telephone;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
}
数据库操作Dao
package com.xiaoniucr.dao;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import com.xiaoniucr.entity.Student;
import com.xiaoniucr.utils.JDBCUtils;
/**
* 学生数据库操作
*
*/
public class StudentDao {
/**
* 查询学生列表
*
* @param username
* 账号
* @param nickname
* 姓名
* @return
*/
public List<Student> queryList(String username, String nickname) {
List<Student> list = new ArrayList<Student>();
Connection con = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
try {
con = JDBCUtils.getConnection();
List<Object> params = new ArrayList<>();
StringBuffer sb = new StringBuffer("select * from t_student where 1=1 ");
if (nickname != null && !"".equals(username)) {
sb.append("and username like ? ");
params.add("%" + username + "%");
}
if (nickname != null && !"".equals(nickname)) {
sb.append("and nickname like ? ");
params.add("%" + nickname + "%");
}
pstmt = con.prepareStatement(sb.toString());
if (params != null && params.size() > 0) {
for (int i = 0; i < params.size(); i++) {
pstmt.setObject(i + 1, params.get(i));
}
}
rs = pstmt.executeQuery();
while (rs.next()) {
Student student = new Student();
student.setId(rs.getInt("id"));
student.setUsername(rs.getString("username"));
student.setNickname(rs.getString("nickname"));
student.setSex(rs.getInt("sex"));
student.setBirthday(rs.getString("birthday"));
student.setTelephone(rs.getString("telephone"));
student.setEmail(rs.getString("email"));
list.add(student);
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
JDBCUtils.close(con, pstmt, rs);
}
return list;
}
/**
* 保存学生信息
*
* @param user
* 学生信息
* @return
*/
public boolean save(Student student) {
Connection con = null;
String sql = "insert into t_student(username,nickname,sex,birthday,telephone,email) values(?,?,?,?,?,?)";
PreparedStatement pstmt = null;
try {
con = JDBCUtils.getConnection();
pstmt = con.prepareStatement(sql);
pstmt.setString(1, student.getUsername());
pstmt.setString(2, student.getNickname());
pstmt.setInt(3, student.getSex());
pstmt.setString(4, student.getBirthday());
pstmt.setString(5, student.getTelephone());
pstmt.setString(6, student.getEmail());
int rows = pstmt.executeUpdate();
if (rows > 0) {
return true;
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
JDBCUtils.close(con, pstmt, null);
}
return false;
}
/**
* 修改学生信息
*
* @param user
* 学生信息
* @return
*/
public boolean update(Student student) {
Connection con = null;
String sql = "update t_student set username=?,nickname=?,sex=?,birthday=?,telephone=?,email=? where id=?";
PreparedStatement pstmt = null;
try {
con = JDBCUtils.getConnection();
pstmt = con.prepareStatement(sql);
pstmt.setString(1, student.getUsername());
pstmt.setString(2, student.getNickname());
pstmt.setInt(3, student.getSex());
pstmt.setString(4, student.getBirthday());
pstmt.setString(5, student.getTelephone());
pstmt.setString(6, student.getEmail());
pstmt.setInt(7, student.getId());
int rows = pstmt.executeUpdate();
if (rows > 0) {
return true;
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
JDBCUtils.close(con, pstmt, null);
}
return false;
}
/**
* 删除学生信息
*
* @param id
* 主键ID
* @return
*/
public boolean delete(int id) {
Connection con = null;
String sql = "delete from t_student where id=?";
PreparedStatement pstmt = null;
try {
con = JDBCUtils.getConnection();
pstmt = con.prepareStatement(sql);
pstmt.setInt(1, id);
int rows = pstmt.executeUpdate();
if (rows > 0) {
return true;
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
JDBCUtils.close(con, pstmt, null);
}
return false;
}
/**
* 根据ID查询学生
*
* @param id
* 主键ID
* @return
*/
public Student getById(int id) {
Student student = null;
Connection con = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
try {
con = JDBCUtils.getConnection();
String sql = "select * from t_student where id = ?";
pstmt = con.prepareStatement(sql);
pstmt.setObject(1, id);
rs = pstmt.executeQuery();
while (rs.next()) {
student = new Student();
student.setId(rs.getInt("id"));
student.setUsername(rs.getString("username"));
student.setNickname(rs.getString("nickname"));
student.setSex(rs.getInt("sex"));
student.setBirthday(rs.getString("birthday"));
student.setTelephone(rs.getString("telephone"));
student.setEmail(rs.getString("email"));
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
JDBCUtils.close(con, pstmt, rs);
}
return student;
}
/**
* 根据账号查询学生
*
* @param username
* @return
*/
public Student getByUsername(String username) {
Student student = null;
Connection con = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
try {
con = JDBCUtils.getConnection();
String sql = "select * from t_student where username = ?";
pstmt = con.prepareStatement(sql);
pstmt.setString(1, username);
rs = pstmt.executeQuery();
while (rs.next()) {
student = new Student();
student.setId(rs.getInt("id"));
student.setUsername(rs.getString("username"));
student.setNickname(rs.getString("nickname"));
student.setSex(rs.getInt("sex"));
student.setBirthday(rs.getString("birthday"));
student.setTelephone(rs.getString("telephone"));
student.setEmail(rs.getString("email"));
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
JDBCUtils.close(con, pstmt, rs);
}
return student;
}
}
界面设计:
package com.xiaoniucr.view;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.List;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import javax.swing.border.EmptyBorder;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;
import com.xiaoniucr.dao.StudentDao;
import com.xiaoniucr.entity.Student;
import sun.swing.table.DefaultTableCellHeaderRenderer;
/**
* 学生管理
*
* @author Lenovo
*
*/
public class StudentView extends JFrame {
private JPanel contentPane;
private JTable table;
private JTextField usernameStrText, nicknameStrText;
private JLabel usernameStrLabel, nicknameStrLabel;
private StudentDao studentDao = new StudentDao();
private JTextField usernameText;
private JTextField nicknameText;
private JLabel sexLabel;
private JTextField birthdayText;
private JTextField telephoneText;
private JTextField emailText;
private JRadioButton maleRadio,femaleRadio;
private JButton searchBtn, addBtn, updateBtn, deleteBtn;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
StudentView frame = new StudentView();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public StudentView() {
this.setTitle("学生管理");
this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
this.setBounds(100, 100, 969, 440);
this.setLocationRelativeTo(null);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
contentPane.setLayout(null);
setContentPane(contentPane);
JScrollPane scrollPane = new JScrollPane();
scrollPane.setBounds(6, 55, 635, 324);
contentPane.add(scrollPane);
usernameStrLabel = new JLabel("学号:");
usernameStrLabel.setBounds(6, 14, 43, 30);
contentPane.add(usernameStrLabel);
usernameStrText = new JTextField();
usernameStrText.setBounds(45, 15, 107, 30);
usernameStrText.setColumns(10);
contentPane.add(usernameStrText);
nicknameStrLabel = new JLabel("姓名:");
nicknameStrLabel.setBounds(162, 14, 52, 30);
contentPane.add(nicknameStrLabel);
nicknameStrText = new JTextField();
nicknameStrText.setBounds(197, 15, 107, 30);
contentPane.add(nicknameStrText);
nicknameStrText.setColumns(10);
searchBtn = new JButton("查询");
searchBtn.setBackground(Color.LIGHT_GRAY);
searchBtn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
clearData();
getData(usernameStrText.getText(), nicknameStrText.getText());
}
});
searchBtn.setBounds(314, 15, 65, 30);
contentPane.add(searchBtn);
Object[] columns = { "ID", "学号", "姓名", "性别", "生日", "电话", "邮箱" };// 字段
Object[][] data = null;// 需要展示的数据,一般是二维数组
DefaultTableModel model = new DefaultTableModel(data, columns);
table = new JTable(model);
table.setRowHeight(30);
table.getTableHeader().getColumnModel().getColumn(0).setMaxWidth(0);
table.getTableHeader().getColumnModel().getColumn(0).setMinWidth(0);
table.getTableHeader().getColumnModel().getColumn(0).setPreferredWidth(0);
DefaultTableCellRenderer tcr = new DefaultTableCellRenderer();// 设置table内容居中
tcr.setHorizontalAlignment(SwingConstants.CENTER);// 这句和上句作用一样
tcr.setVerticalAlignment(SwingConstants.CENTER);
table.setDefaultRenderer(Object.class, tcr);
// 设置表头居中显示
DefaultTableCellHeaderRenderer hr = new DefaultTableCellHeaderRenderer();
hr.setHorizontalAlignment(JLabel.CENTER);
table.getTableHeader().setDefaultRenderer(hr);
table.getTableHeader().setFont(new Font("黑体", Font.PLAIN, 14));
table.getTableHeader().setPreferredSize(new Dimension(1, 30));
table.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
// TODO Auto-generated method stub
if (e.getClickCount() == 1) {
int row = table.getSelectedRow();
int id = Integer.valueOf(table.getValueAt(row, 0).toString());
Student student = studentDao.getById(id);
usernameText.setText(student.getUsername());
nicknameText.setText(student.getNickname());
if(student.getSex() == 0){
maleRadio.setSelected(true);
femaleRadio.setSelected(false);
}else{
maleRadio.setSelected(false);
femaleRadio.setSelected(true);
}
birthdayText.setText(student.getBirthday());
telephoneText.setText(student.getTelephone());
emailText.setText(student.getEmail());
}
}
});
getData(null, null);
scrollPane.setViewportView(table);
JLabel usernameLabel = new JLabel("学号:");
usernameLabel.setBounds(662, 55, 54, 30);
contentPane.add(usernameLabel);
usernameText = new JTextField();
usernameText.setBounds(699, 56, 216, 30);
contentPane.add(usernameText);
usernameText.setColumns(10);
JLabel nicknameLabel = new JLabel("姓名:");
nicknameLabel.setBounds(662, 106, 54, 30);
contentPane.add(nicknameLabel);
nicknameText = new JTextField();
nicknameText.setBounds(699, 107, 216, 30);
contentPane.add(nicknameText);
nicknameText.setColumns(10);
sexLabel = new JLabel("性别:");
sexLabel.setBounds(662, 152, 54, 30);
contentPane.add(sexLabel);
maleRadio = new JRadioButton("男");
maleRadio.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
maleRadio.setSelected(true);
femaleRadio.setSelected(false);
}
});
maleRadio.setSelected(true);
maleRadio.setBounds(699, 152, 65, 30);
contentPane.add(maleRadio);
femaleRadio = new JRadioButton("女");
femaleRadio.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
maleRadio.setSelected(false);
femaleRadio.setSelected(true);
}
});
femaleRadio.setBounds(766, 152, 65, 30);
contentPane.add(femaleRadio);
JLabel birthdayLabel = new JLabel("生日:");
birthdayLabel.setBounds(662, 192, 54, 30);
contentPane.add(birthdayLabel);
birthdayText = new JTextField();
birthdayText.setColumns(10);
birthdayText.setBounds(699, 193, 216, 30);
contentPane.add(birthdayText);
JLabel telephoneLabel = new JLabel("电话:");
telephoneLabel.setBounds(662, 241, 54, 30);
contentPane.add(telephoneLabel);
telephoneText = new JTextField();
telephoneText.setBounds(699, 242, 216, 30);
contentPane.add(telephoneText);
telephoneText.setColumns(10);
JLabel emailLabel = new JLabel("邮箱:");
emailLabel.setBounds(662, 288, 54, 30);
contentPane.add(emailLabel);
emailText = new JTextField();
emailText.setBounds(699, 289, 216, 30);
contentPane.add(emailText);
emailText.setColumns(10);
addBtn = new JButton("添加");
addBtn.setBackground(Color.LIGHT_GRAY);
addBtn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String username = usernameText.getText();
String nickname = nicknameText.getText();
Integer sex = 0;
//获取性别
for(Component c : contentPane.getComponents()){
if(c instanceof JRadioButton){
if(((JRadioButton) c).isSelected()){
String text = ((JRadioButton)c).getText();
if("男".equals(text)){
sex = 0;
}else{
sex = 1;
}
}
}
}
String birthday = birthdayText.getText();
String telephone = telephoneText.getText();
String email = emailText.getText();
if(username == null || "".equals(username)){
JOptionPane.showMessageDialog(contentPane, "请输入学号", "系统提示",JOptionPane.WARNING_MESSAGE);
return;
}
if(nickname == null || "".equals(nickname)){
JOptionPane.showMessageDialog(contentPane, "请输入姓名", "系统提示",JOptionPane.WARNING_MESSAGE);
return;
}
if(birthday == null || "".equals(birthday)){
JOptionPane.showMessageDialog(contentPane, "请输入生日", "系统提示",JOptionPane.WARNING_MESSAGE);
return;
}
if(telephone == null || "".equals(telephone)){
JOptionPane.showMessageDialog(contentPane, "请输入电话", "系统提示",JOptionPane.WARNING_MESSAGE);
return;
}
if(email == null || "".equals(email)){
JOptionPane.showMessageDialog(contentPane, "请输入邮箱", "系统提示",JOptionPane.WARNING_MESSAGE);
return;
}
Student student = studentDao.getByUsername(username);
if(student != null){
JOptionPane.showMessageDialog(contentPane, "学号已存在", "系统提示",JOptionPane.WARNING_MESSAGE);
return;
}
student = new Student();
student.setUsername(username);
student.setNickname(nickname);
student.setSex(sex);
student.setBirthday(birthday);
student.setTelephone(telephone);
student.setEmail(email);
boolean flag = studentDao.save(student);
if(flag){
JOptionPane.showMessageDialog(contentPane, "保存成功!");
clearData();
getData(null, null);
}else{
JOptionPane.showMessageDialog(contentPane, "保存失败!", "系统提示",JOptionPane.WARNING_MESSAGE);
return;
}
}
});
addBtn.setBounds(699, 341, 65, 30);
contentPane.add(addBtn);
updateBtn = new JButton("修改");
updateBtn.setBackground(Color.LIGHT_GRAY);
updateBtn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
// 获取选中行
int row = table.getSelectedRow();
if (row < 0) {
JOptionPane.showMessageDialog(contentPane, "请选择一条记录", "系统提示", JOptionPane.WARNING_MESSAGE);
return;
}
int id = Integer.valueOf(table.getValueAt(row, 0).toString());
String username = usernameText.getText();
String nickname = nicknameText.getText();
Integer sex = 0;
//获取性别
for(Component c : contentPane.getComponents()){
if(c instanceof JRadioButton){
if(((JRadioButton) c).isSelected()){
String text = ((JRadioButton)c).getText();
if("男".equals(text)){
sex = 0;
}else{
sex = 1;
}
}
}
}
String birthday = birthdayText.getText();
String telephone = telephoneText.getText();
String email = emailText.getText();
if(username == null || "".equals(username)){
JOptionPane.showMessageDialog(contentPane, "请输入学号", "系统提示",JOptionPane.WARNING_MESSAGE);
return;
}
if(nickname == null || "".equals(nickname)){
JOptionPane.showMessageDialog(contentPane, "请输入姓名", "系统提示",JOptionPane.WARNING_MESSAGE);
return;
}
if(birthday == null || "".equals(birthday)){
JOptionPane.showMessageDialog(contentPane, "请输入生日", "系统提示",JOptionPane.WARNING_MESSAGE);
return;
}
if(telephone == null || "".equals(telephone)){
JOptionPane.showMessageDialog(contentPane, "请输入电话", "系统提示",JOptionPane.WARNING_MESSAGE);
return;
}
if(email == null || "".equals(email)){
JOptionPane.showMessageDialog(contentPane, "请输入邮箱", "系统提示",JOptionPane.WARNING_MESSAGE);
return;
}
Student student = studentDao.getById(id);
if(!student.getUsername().equals(username)){
Student exist = studentDao.getByUsername(username);
if(exist != null){
JOptionPane.showMessageDialog(contentPane, "学号已存在", "系统提示",JOptionPane.WARNING_MESSAGE);
return;
}
}
student.setUsername(username);
student.setNickname(nickname);
student.setSex(sex);
student.setBirthday(birthday);
student.setTelephone(telephone);
student.setEmail(email);
boolean flag = studentDao.update(student);
if(flag){
JOptionPane.showMessageDialog(contentPane, "保存成功!");
clearData();
getData(null, null);
}else{
JOptionPane.showMessageDialog(contentPane, "保存失败!", "系统提示",JOptionPane.WARNING_MESSAGE);
return;
}
}
});
updateBtn.setBounds(775, 341, 65, 30);
contentPane.add(updateBtn);
deleteBtn = new JButton("删除");
deleteBtn.setBackground(Color.LIGHT_GRAY);
deleteBtn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
// 获取选中行
int row = table.getSelectedRow();
if (row < 0) {
JOptionPane.showMessageDialog(contentPane, "请选择一条记录", "系统提示", JOptionPane.WARNING_MESSAGE);
return;
}
int result = JOptionPane.showConfirmDialog(contentPane, "确定删除此记录吗?", "提示", JOptionPane.YES_NO_OPTION);
if (result == 0) {
int id = Integer.valueOf(table.getValueAt(row, 0).toString());
boolean flag = studentDao.delete(id);
if (flag) {
JOptionPane.showMessageDialog(contentPane, "删除成功!");
clearData();
getData(null, null);
} else {
JOptionPane.showMessageDialog(contentPane, "操作失败", "系统提示", JOptionPane.WARNING_MESSAGE);
}
}
return;
}
});
deleteBtn.setBounds(850, 341, 65, 30);
contentPane.add(deleteBtn);
}
// 填充表格数据
public void getData(String username, String nickname) {
List<Student> list = studentDao.queryList(username, nickname);
DefaultTableModel tableModel = (DefaultTableModel) table.getModel();
tableModel.setRowCount(0);// 清除原有行
// 填充数据
for (Student item : list) {
String[] arr = new String[7];
arr[0] = item.getId() + "";
arr[1] = item.getUsername();
arr[2] = item.getNickname();
arr[3] = item.getSex() == 0 ? "男" : "女";
arr[4] = item.getBirthday();
arr[5] = item.getTelephone();
arr[6] = item.getEmail();
// 添加数据到表格
tableModel.addRow(arr);
}
}
private void clearData(){
usernameText.setText("");
nicknameText.setText("");
maleRadio.setSelected(true);
femaleRadio.setSelected(false);
birthdayText.setText("");
telephoneText.setText("");
emailText.setText("");
}
}
数据库工具类:
package com.xiaoniucr.utils;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
/**
* 数据库连接
*
*/
public class JDBCUtils {
//数据库连接地址
public static String URL = "jdbc:mysql://localhost:3306/db_student?useUnicode=true&characterEncoding=utf8&serverTimezone=GMT%2B8";
//数据库驱动
public static String DRIVER = "com.mysql.cj.jdbc.Driver";
//数据库用户名
public static String USER = "root";
//数据库密码
public static String PWD = "123456";
/*
* 数据库连接
*/
public static Connection getConnection() {
Connection con = null;
try {
Class.forName(DRIVER);
con = DriverManager.getConnection(URL, USER, PWD);
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
return con;
}
/**
* 关闭连接资源
* @param con 连接对象
* @param pstmt 预编译对象
* @param rs 结果集
*/
public static void close(Connection con, PreparedStatement pstmt, ResultSet rs) {
try {
if (rs != null){
rs.close();
}
if (pstmt != null){
pstmt.close();
}
if (con != null){
con.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
总结:
java+swing+mysql简单学生信息管理,系统非常简单,就是一个学生信息的增删改查和模块,对于初学者具有较强的学习意义,有需要可以下载参考。本人具备多年软件开发经验,有问题可以共同探讨。
源码下载:
开源分享java+swing+mysql简单学生信息管理系统(GUI图形界面)