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;

JAVA学生信息管理系统(数据库实现)

2,相关代码(主页面+增删查改)

JAVA学生信息管理系统(数据库实现)

 

(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);
	}
}

 

JAVA学生信息管理系统(数据库实现)

(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);
	}
}

 JAVA学生信息管理系统(数据库实现)

(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);
	}
}

 JAVA学生信息管理系统(数据库实现)

 (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);
	}
}


JAVA学生信息管理系统(数据库实现)

 (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);
	}
}

JAVA学生信息管理系统(数据库实现)

(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,根据自己版本下载

JAVA学生信息管理系统(数据库实现)文章来源地址https://www.toymoban.com/news/detail-502881.html

3. 数据库链接成功,完成项目的简单操作

到了这里,关于JAVA学生信息管理系统(数据库实现)的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • (解析+源码)基于JAVA Swing+MySQL实现学生信息管理系统(增、删、改、查)数据库/文件存储

    本文适合 有一定JAVA编程基础(听过一点课的同学) 的同学“食用”,源代码都在文末 源代码(点击跳转) ,第四部分是各个模块的实现,新建一个工程把下面代码添加进去,然后在数据库里按id-username-password和id-name-sex-telephone-number-birthday-note创建两个表,在Connect.java里面将us

    2024年02月04日
    浏览(53)
  • java 学生信息管理系统Myeclipse开发mysql数据库web结构jsp编程计算机网页项目

    一、源码特点     java 学生信息管理系统是一套完善的java web信息管理系统,对理解JSP java编程开发语言有帮助,系统具有完整的源代码和数据库,系统主要采用B/S模式开发。开发环境为 TOMCAT7.0,Myeclipse8.5开发,数据库为Mysql5.0,使用java语言开发。 java 学生信息管理系统 二、

    2024年02月09日
    浏览(56)
  • 学生信息管理系统(数据库)

    要求实现功能: (1)学生、课程、教师等信息的录入和维护,一门课只由一位教师上,一位教师可上多门课 (2)学生进行选课,一学期约20学分 (3)教师在每门课结束后给出学生成绩,不及格则补考后记录补考成绩 (4)能明细查询某学生的选课情况及某课程的选修学生情

    2024年02月03日
    浏览(46)
  • 学生信息管理系统的数据库设计MySQL

    1.课程设计目的 学生信息管理系统是一个教育单位不可缺少的部分。一个功能齐全、简单易用的信息管理系统不但能有效地减轻学校相关工作人员的工作负担,它的内容对于学校的决策者和管理者来说都至关重要。所以学生信息管理系统应该能够为用户提供充足的信息和快捷

    2024年02月05日
    浏览(54)
  • 学生信息及成绩管理系统(Python+Sqlite)数据库版

    目录 功能模块: 运行功能演示:  具体代码实现过程: 创建sqlite 数据库  Python代码 引入os和sqlite3包: 初始化数据库: 连接数据库: 关闭并提交数据到数据库: 查询数据并显示: 添加并插入数据到数据库: 更新数据到数据库: 删除数据并更新数据库:  导入和导出数据

    2024年02月04日
    浏览(47)
  • [Python+Django]Web学生信息管理系统数据库设计及系统实现

    本文我们完成数据的设计,并通过Django框架完成数据库构建同时利用Django框架模式实现学生信息管理系统的功能。 简单的包装下毕设应该没问题了。 Python,Mysql,Pycharm的安装本文就不做特别介绍了,有需要的同学请参考如下博文。 Python + Django4 搭建个人博客(二):准备开

    2024年02月03日
    浏览(65)
  • 数据库课程设计——学生信息管理系统C#,SQL Sever

    目录 利用SQL Sever和 VS C#实现 一、程序流程图 二、具体实现:利用SQL Sever和 VS实现,使用C#连接数据库 1、新建一个名为MySchool的数据库​​​​​​​ 2、C#连接数据库,并实现对MySchool数据库的增、删、改、查操作 (1)主界面  (2)学生登录页面 (3)个人主页 (4)注册页

    2024年01月18日
    浏览(52)
  • 数据库课程设计——学生信息管理系统(Sqlserver,C#,Winform)

    目录 需求分析 一.登录功能 二.注册功能 三.管理员登录后跳转到功能页面: 四.学生信息管理(主界面,删除功能在主界面代码中) 五.学生信息添加和修改(设计在一个页面上,修改需要选中行) 六.课程信息管理(删除功能在主界面中)  七.课程信息添加和修改  八.成绩

    2024年02月02日
    浏览(53)
  • python 实现学生信息管理系统+MySql 数据库,包含源码及相关实现说明~

    1、系统说明 python 编写的学生信息管理系统+MySQL数据库,实现了增删改查的基本功能。 2、数据库说明 本人使用的是 MySQL8.0 版本 数据库端口号为:3306 数据库用户名是:root 数据库名称是:practice 建立的表是:students 3、系统功能 增加学生信息 删除学生信息 修改学生信息 查

    2024年02月11日
    浏览(54)
  • Java连接数据库(学生管理系统案例,可以实现增删改查)

    首先,需要做一个准备工作 ——下载jar包,这个包是用来支持数据库的连接的 官网的下载链接:MySQL :: Download Connector/J 点击链接进入页面: 选择画红框的下载按钮。 与此同时,打开IDEA开发工具,在当前项目目录下新建一个lib目录文件夹用来存放第三方jar包,这样做方便管

    2024年02月07日
    浏览(55)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包