学生信息管理系统
作者:互联网
最近开始在大佬的带领下努力学习编程,下面是一个学生信息管理系统,首先需要连接mysql:
第①步:在Eclipse的Java项目右击新建文件夹,名称为lib
第②步:将jar包复制粘贴到新建好的lib文件夹中
第③步:配置构建路径
点击添加JAR
选中相应Java项目工程中粘贴的jar后点击确定,应用并关闭.
这样准备工作就做好了,随后书写下面的几个类:由于注释都写的比较详细,具体的内容就不多赘述了,还请大佬们多多提出建议意见,共同提高.
begin类:
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
@SuppressWarnings(“serial”)//接口类防止黄色警告
public class Begin extends JFrame implements ActionListener{//窗体与监听器接口
JMenuBar all; //设置总菜单栏 菜单条
JMenu one,two,three,four,five; //设置菜单栏的五个选项 菜单
JMenuItem onebutton,twobutton,threebutton,fourbutton,fivebutton;//设置五个子菜单栏的单击事件 菜单项
AddStudent addpeople;
FindStudent findpeople;
UpdateStudent updatepeople;
DeleteStudent deletepeople;
/**
- 构造方法
- 窗体的基本数据
-
- 设置窗体的标题
-
- 设置窗体的大小和布局
-
- 设置窗体布局为空
-
- 设置窗体的关闭方式
-
- 设置窗体可见
*/
@SuppressWarnings(“deprecation”)//不检测过期
public Begin() {
addpeople = new AddStudent(); //实例化AddStudent类的对象
findpeople = new FindStudent(); //实例化FindStudent类的对象
updatepeople = new UpdateStudent(); //实例化UpdateStudent类的对象
deletepeople = new DeleteStudent(); //实例化DeleteStudent类的对象
all = new JMenuBar(); //初始化菜单组件
one = new JMenu("信息录入"); //自定义相应组件
two = new JMenu("信息查询");
three = new JMenu("信息更新");
four = new JMenu("信息删除");
five = new JMenu("退出系统");
onebutton = new JMenuItem("录入");
twobutton = new JMenuItem("查询");
threebutton = new JMenuItem("更新");
fourbutton = new JMenuItem("删除");
fivebutton = new JMenuItem("退出");
one.add(onebutton); //将鼠标左键单击事件添加到组件中
two.add(twobutton);
three.add(threebutton);
four.add(fourbutton);
five.add(fivebutton);
all.add(one); //将子菜单添加到主菜单中
all.add(two);
all.add(three);
all.add(four);
all.add(five);
onebutton.addActionListener(this); //为各菜单添加事件监听
twobutton.addActionListener(this);
threebutton.addActionListener(this);
fourbutton.addActionListener(this);
fivebutton.addActionListener(this);
setJMenuBar(all); //设置all为窗体的菜单栏
setTitle("学生信息管理系统"); //设置窗体的标题
setBounds(450,150,500,500); //设置窗体的大小和布局:xy坐标和宽高
setDefaultCloseOperation(EXIT_ON_CLOSE); //设置窗体的关闭方式
}
public void actionPerformed(ActionEvent e) {//鼠标进行的事件监听器
if(e.getSource() == onebutton) {//获得事件
this.getContentPane().removeAll();//绘图类
this.getContentPane().add(addpeople,“Center”);
this.getContentPane().repaint(); //对面板进行重新绘制
this.getContentPane().validate();//初始化容器并使其合法
}
if(e.getSource() == twobutton) {
this.getContentPane().removeAll();
this.getContentPane().add(findpeople,“Center”);
this.getContentPane().repaint(); //对面板进行重新绘制
this.getContentPane().validate();
}
if(e.getSource() == threebutton) {
this.getContentPane().removeAll();
this.getContentPane().add(updatepeople,“Center”);
this.getContentPane().repaint(); //对面板进行重新绘制
this.getContentPane().validate();
}
if(e.getSource() == fourbutton) {
this.getContentPane().removeAll();
this.getContentPane().add(deletepeople,“Center”);
this.getContentPane().repaint(); //对面板进行重新绘制
this.getContentPane().validate();
}
if(e.getSource() == fivebutton) {
System.exit(0);//完成退出操作
}
}
public static void main(String[] args) {
Begin b = new Begin();
b.setVisible(true); //设置窗体可见
}
addstudent类:
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
@SuppressWarnings("serial")
public class AddStudent extends JPanel implements ActionListener{
Connection con; //声明connection对象
PreparedStatement sql; //声明PreparedStatement对象
ResultSet res; //声明ResultSet对象
String url;
JButton button1,button2; //定义两个按钮组件:录入、重置
JTextField text1,text2,text3,text4,text5,text6,text7; //定义七个文本框
public AddStudent() {
try {
Class.forName("com.mysql.jdbc.Driver"); //加载数据库驱动
System.out.println("数据库驱动加载成功");
}catch(ClassNotFoundException e) {
e.printStackTrace();//捕获异常
}
try { //通过连接访问数据库的url,获取数据库连接对象
con = DriverManager.getConnection("jdbc:mysql:" + "//127.0.0.1:3306/student","root","");
System.out.println("数据库连接成功");
}catch(SQLException e) {
e.printStackTrace();
}
button1 = new JButton("录入");
button2 = new JButton("重置");
text1 = new JTextField(18); //实例化JTextField组件 输入框长度18列
text2 = new JTextField(18);
text3 = new JTextField(18);
text4 = new JTextField(18);
text5 = new JTextField(18);
text6 = new JTextField(18);
text7 = new JTextField(18);
this.setLayout(null);//清空布局管理器
this.add(button1).setBounds(145,340,80,30); //将JButton组件添加到面板中 长宽xy
this.add(button2).setBounds(265,340,80,30);
this.add(new JLabel("学号:")).setBounds(120,91,50,30); //书写面板
this.add(new JLabel("姓名:")).setBounds(120,131,50,30);
this.add(new JLabel("性别:")).setBounds(120,171,50,30);
this.add(new JLabel("地址:")).setBounds(120,211,50,30);
this.add(new JLabel("电话:")).setBounds(120,251,50,30);
this.add(new JLabel("专业:")).setBounds(120,291,50,30);
this.add(text1).setBounds(165,91,175,30); //将JTextField组件添加到面板中
this.add(text2).setBounds(165,131,175,30);
this.add(text3).setBounds(165,171,175,30);
this.add(text4).setBounds(165,211,175,30);
this.add(text5).setBounds(165,251,175,30);
this.add(text6).setBounds(165,291,175,30);
this.add(text7).setBounds(165,50,175,30);
text7.setEditable(false); //设置文本框为不可见
button1.addActionListener(this); //为3个JButton组件添加监听事件
button2.addActionListener(this);
}
public void actionPerformed(ActionEvent e) {
if(e.getSource() == button1) {
try {
sql = con.prepareStatement("Insert Into student"+" Values(?,?,?,?,?,?)"); //实例化Statement对象
String number = text1.getText(); //获取文本框是"id"字段的值
String name = text2.getText(); //获取文本框是"name"字段的值
String sex = text3.getText(); //获取文本框是"gender"字段的值
String address = text4.getText(); //获取文本框是"address"字段的值
String phone = text5.getText(); //获取文本框是"phone"字段的值
String major = text6.getText(); //获取文本框是"major"字段的值
sql.setString(1,number); //向数据库中添加数据
sql.setString(2,name);
sql.setString(3,sex);
sql.setString(4,address);
sql.setString(5,phone);
sql.setString(6,major);
sql.executeUpdate(); //更新数据
text7.setText("学生信息录入成功");
}catch(Exception e1) {
e1.printStackTrace();//捕获异常
text7.setText("学生信息录入失败");
}
}
if(e.getSource() == button2) {
text1.setText(""); //将文本框置空
text2.setText("");
text3.setText("");
text4.setText("");
text5.setText("");
text6.setText("");
text7.setText(""); //重置成功
}
}
}
deletestudent类:
import java.awt.Color;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
@SuppressWarnings("serial")
public class DeleteStudent extends JPanel implements ActionListener{
Connection con; //声明Connection对象
PreparedStatement sql; //声明PreparedStatement对象
Statement sql2; //声明Statement对象
ResultSet res; //声明ResultSet对象
JButton button1,button2,button3; //定义三个按钮组件
JTextField text1,text2; //定义两个文本框
String src,name;
MyDialog dialog; //产生对话框
public DeleteStudent() {
try {
Class.forName("com.mysql.jdbc.Driver"); //加载数据库驱动
System.out.println("数据库驱动加载成功");
}catch(ClassNotFoundException e) {
e.printStackTrace();//捕捉找不到类的错误
}
try { //通过连接访问数据库的url,获取数据库连接对象
con = DriverManager.getConnection("jdbc:mysql:" + "//127.0.0.1:3306/student","root","");
System.out.println("数据库连接成功");
}catch(SQLException e) {//SQL异常处理块
e.printStackTrace();
}
this.setLayout(null);//清空布局管理器
text1 = new JTextField(18);
text2 = new JTextField(18);
button1 = new JButton("删除");
button2 = new JButton("重置");
button3 = new JButton("查看数据库");
this.add(button1).setBounds(340,88,80,30); //将JButton组件添加到面板中
this.add(button2).setBounds(285,340,80,30);
this.add(button3).setBounds(135,340,100,30);
this.add(new JLabel("请输入学号:")).setBounds(95,90,130,30);
this.add(text1).setBounds(175,132,150,28);
this.add(text2).setBounds(175,88,150,30);//文本框实例化
JLabel label1 = new JLabel("----人生只若初见----");//建立面板
Font font1 = new Font("楷体",Font.BOLD,25);
label1.setForeground(Color.BLUE);//设置前景色
label1.setFont(font1);//设置组件当前使用的字体
this.add(label1).setBounds(120,270,300,60);//设置面板大小
button1.addActionListener(this); //为JButton组件添加监听事件
button2.addActionListener(this);
button3.addActionListener(this);
text1.setEditable(false); //设置文本框不可编辑
}
public void actionPerformed(ActionEvent e) {//点击按钮与回车的监听器
if(e.getSource() == button1) {
try {
String id = text2.getText(); //获取学号输入栏中的值
src = text2.getText();
sql2 = con.createStatement(); //实例化Statement对象:如果不实例化Statement对象,会出现空指针异常
res = sql2.executeQuery("Select name From Student " + "Where id=" + src); //此时res返回的结果是一个boolean类型的数据
sql = con.prepareStatement("Delete From student" + " Where id=?"); //记录删除语句 批量
sql.setString(1,id); //向数据库语句中添加数据
sql.executeUpdate(); //执行数据库语句
if(res == null)
text1.setText("该学生信息不存在");
else {
Student mess = new Student();
while(res.next()) {//游标下移
this.name = res.getString("name"); //获取字段是"name"的值
mess.setName(this.name); //将字段是"name"的值存储到Student的对象mess中
}
System.out.println(this.name); //这段语句用于检测删除的名字是否输出正确
text1.setText(this.name + "的信息删除成功");
}
}catch(SQLException e1){//对有误信息做预处理
text1.setText("学生信息删除失败");
e1.printStackTrace();
}
}
if(e.getSource() == button2) {
text1.setText(""); //文本框置空
text2.setText("");
}
if(e.getSource() == button3) {
dialog = new MyDialog();
}
}
}
findstudent类:
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
@SuppressWarnings("serial")//接口类防止黄色警告
public class FindStudent extends JPanel implements ActionListener{//面板容器和监听器接口
Connection con; //声明Connection对象 SQL专用接口
Statement sql; //声明Statement对象 发送SQL语句
ResultSet res; //声明ResultSet对象 查询结果并返回
String url,temp;
int flag = 1; //定义一个标志变量
JButton button1,button2,button3; //定义三个按钮组件:查询、重置、查看数据库
JTextField text1,text2,text3,text4,text5,text6,text7; //定义七个文本框 单行文本
MyDialog dialog;//产生对话框
public FindStudent() {
try {
Class.forName("com.mysql.jdbc.Driver"); //加载数据库驱动 动态加载类
System.out.println("数据库驱动加载成功");
}catch(ClassNotFoundException e) {//找不到对应的类
e.printStackTrace();//捕获错误
}
try { //通过连接访问数据库的url,获取数据库连接对象
con = DriverManager.getConnection("jdbc:mysql:" + "//127.0.0.1:3306/student","root","");//获取数据库连接
System.out.println("数据库连接成功");
}catch(SQLException e) {//SQLException比Exception更效率
e.printStackTrace();//捕捉异常并做处理
}
button1 = new JButton("查询");//实例化Jbutton组件
button2 = new JButton("重置");
button3 = new JButton("查看数据库");
text1 = new JTextField(18); //实例化JTextField组件 输入框长度为18列
text2 = new JTextField(18);
text3 = new JTextField(18);
text4 = new JTextField(18);
text5 = new JTextField(18);
text6 = new JTextField(18);
text7 = new JTextField(18);
this.setLayout(null);//清空布局管理器
this.add(button1).setBounds(360,62,80,30); //将JButton组件添加到面板中 长宽xy
this.add(button2).setBounds(270,365,80,30);
this.add(button3).setBounds(130,365,100,30);
this.add(new JLabel("学号:")).setBounds(120,111,50,30);//书写面板
this.add(new JLabel("姓名:")).setBounds(120,151,50,30);
this.add(new JLabel("性别:")).setBounds(120,191,50,30);
this.add(new JLabel("地址:")).setBounds(120,231,50,30);
this.add(new JLabel("电话:")).setBounds(120,271,50,30);
this.add(new JLabel("专业:")).setBounds(120,311,50,30);
this.add(new JLabel("请输入学号:")).setBounds(85,62,100,30);
this.add(text1).setBounds(165,111,175,30); //将JTextField组件添加到面板中
this.add(text2).setBounds(165,151,175,30);
this.add(text3).setBounds(165,191,175,30);
this.add(text4).setBounds(165,231,175,30);
this.add(text5).setBounds(165,271,175,30);
this.add(text6).setBounds(165,311,175,30);
this.add(text7).setBounds(165,64,175,30);
text1.setEditable(false); //设置文本框不可输入
text2.setEditable(false);
text3.setEditable(false);
text4.setEditable(false);
text5.setEditable(false);
text6.setEditable(false);
button1.addActionListener(this); //为3个JButton组件添加监听事件
button2.addActionListener(this);
button3.addActionListener(this);//响应点击
}
public void actionPerformed(ActionEvent e) {//接口类重写的必要条件
if(e.getSource() == button1) {//返回事件
try {
sql = con.createStatement(); //实例化Statement对象 用这个对象调用SQL语句
res = sql.executeQuery("Select * From student"); //执行SQL语句
while(res.next()) {
String number = res.getString("id"); //获取列名是"id"字段的值
String name = res.getString("name"); //获取列名是"name"字段的值
String sex = res.getString("gender"); //获取列名是"gender"字段的值
String address = res.getString("address"); //获取列名是"address"字段的值
String phone = res.getString("phone"); //获取列名是"phone"字段的值
String major = res.getString("major"); //获取列名是"major"字段的值
temp = text7.getText(); //将文本框中输入的内容赋值给temp
if(temp.equals(number)) {
text1.setText(number); //将获取到的列值输出
text2.setText(name);
text3.setText(sex);
text4.setText(address);
text5.setText(phone);
text6.setText(major);
text7.setText("查询成功");
flag = 0; //如果输出列值就像flag定义为0 总列数大于输出列数,够了就中断
}
}
}catch(Exception e1) {
System.out.println(e1);
}
if(flag == 1)
text7.setText("没有该学生");
}
if(e.getSource() == button2) {
text1.setText(""); //将文本框置空
text2.setText("");
text3.setText("");
text4.setText("");
text5.setText("");
text6.setText("");
text7.setText(""); //完成重置
}
if(e.getSource() == button3) {
dialog = new MyDialog(); //实例化JDialog对象 建立文本框
}
}
}
mydialog类:
import java.awt.BorderLayout;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Vector;
import javax.swing.JDialog;
import javax.swing.JScrollPane;
import javax.swing.JTable;
@SuppressWarnings("serial")//防止接口类黄色警告
public class MyDialog extends JDialog{//建立临时对话框
Connection con; //声明Connection对象
Statement sql; //声明Statement对象
ResultSet res; //声明ResultSet对象
public MyDialog() {
this.setBounds(400,200,600,450); //设置窗体的大小以及位置
this.setTitle("查看数据库"); //设置窗体的标题
String[] columnNames = {"id","name","gender","address","phone","major"}; //定义表格列名数组
String[][] tableValues = {}; //定义表格数据数组
Vector<String> columnName = new Vector<>(); //定义表格列名向量 动态数组
columnName.add("id"); //添加列名
columnName.add("name"); //添加列名
columnName.add("gender"); //添加列名
columnName.add("address"); //添加列名
columnName.add("phone"); //添加列名
columnName.add("major"); //添加列名
Vector<Vector<String>> tableValue = new Vector<>(); //定义表格数据向量
try {
Class.forName("com.mysql.jdbc.Driver"); //加载数据库驱动
System.out.println("数据库驱动加载成功");
}catch(ClassNotFoundException e) {
e.printStackTrace();//捕捉找不到类的错误
}
try { //通过连接访问数据库的url,获取数据库连接对象
con = DriverManager.getConnection("jdbc:mysql:" + "//127.0.0.1:3306/student","root","");
System.out.println("数据库连接成功");
}catch(SQLException e) {
e.printStackTrace();//对错误做预处理
}
try {
sql = con.createStatement(); //实例化Statement对象
res = sql.executeQuery("Select * From student"); //执行SQL语句
while(res.next()) {
String number = res.getString("id"); //获取列名是"id"字段的值
String name = res.getString("name"); //获取列名是"name"字段的值
String sex = res.getString("gender"); //获取列名是"gender"字段的值
String address = res.getString("address"); //获取列名是"address"字段的值
String phone = res.getString("phone"); //获取列名是"phone"字段的值
String major = res.getString("major"); //获取列名是"major"字段的值
Vector<String> rowValue = new Vector<>(); //定义表格行向量
rowValue.add(number); //添加单元格数据
rowValue.add(name); //添加单元格数据
rowValue.add(sex); //添加单元格数据
rowValue.add(address); //添加单元格数据
rowValue.add(phone); //添加单元格数据
rowValue.add(major); //添加单元格数据
tableValue.add(rowValue);
}
}catch(Exception e1) {
System.out.println(e1);
}
JTable table = new JTable(tableValue,columnName); //创建指定列名和数据的表格
JScrollPane scrollPane = new JScrollPane(table); //创建显示表格的滚动面板
this.getContentPane().add(scrollPane,BorderLayout.CENTER); //将滚动面板添加到边界布局中间
setVisible(true); //设置窗体可见
}
}
student类:
public class Student {
String number;
String name;
String sex;
String address;
String phone;
String major;
public String getNumber() {
return number;
}
public void setNumber(String number) {
this.number = number;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public String getMajor() {
return major;
}
public void setMajor(String major) {
this.major = major;
}
}
updatestudent类:
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
@SuppressWarnings("serial")//接口类防止黄色警告
public class UpdateStudent extends JPanel implements ActionListener{//面板容器和监听器接口
Connection con; //声明connection对象
PreparedStatement sql; //声明PreparedStatement对象
ResultSet res; //声明ResultSet对象
String url;
JButton button1,button2; //定义两个按钮组件:录入、重置
JTextField text1,text2,text3,text4,text5,text6,text7; //定义七个文本框
public UpdateStudent() {
try {
Class.forName("com.mysql.jdbc.Driver"); //加载数据库驱动
System.out.println("数据库驱动加载成功");
}catch(ClassNotFoundException e) {
e.printStackTrace();//捕获异常
}
try { //通过连接访问数据库的url,获取数据库连接对象
con = DriverManager.getConnection("jdbc:mysql:" + "//127.0.0.1:3306/student","root","");
// System.out.println(“数据库连接成功”);
}catch(SQLException e) {
e.printStackTrace();
}
button1 = new JButton(“录入”);
button2 = new JButton(“重置”);
text1 = new JTextField(18); //实例化JTextField组件
text2 = new JTextField(18);
text3 = new JTextField(18);
text4 = new JTextField(18);
text5 = new JTextField(18);
text6 = new JTextField(18);
text7 = new JTextField(18);
this.setLayout(null);//清空布局管理器
this.add(button1).setBounds(145,340,80,30); //将JButton组件添加到面板中
this.add(button2).setBounds(265,340,80,30);
this.add(new JLabel("学号:")).setBounds(120,91,50,30);//书写面板
this.add(new JLabel("姓名:")).setBounds(120,131,50,30);
this.add(new JLabel("性别:")).setBounds(120,171,50,30);
this.add(new JLabel("地址:")).setBounds(120,211,50,30);
this.add(new JLabel("电话:")).setBounds(120,251,50,30);
this.add(new JLabel("专业:")).setBounds(120,291,50,30);
this.add(text1).setBounds(165,91,175,30); //将JTextField组件添加到面板中
this.add(text2).setBounds(165,131,175,30);
this.add(text3).setBounds(165,171,175,30);
this.add(text4).setBounds(165,211,175,30);
this.add(text5).setBounds(165,251,175,30);
this.add(text6).setBounds(165,291,175,30);
this.add(text7).setBounds(165,50,175,30);
text7.setEditable(false); //设置文本框为不可见
button1.addActionListener(this); //为3个JButton组件添加监听事件
button2.addActionListener(this);
}
public void actionPerformed(ActionEvent e) {
if(e.getSource() == button1) {
try {
sql = con.prepareStatement("Insert Into student"+" Values(?,?,?,?,?,?)"); //实例化Statement对象
String number = text1.getText(); //获取文本框是"id"字段的值
String name = text2.getText(); //获取文本框是"name"字段的值
String sex = text3.getText(); //获取文本框是"gender"字段的值
String address = text4.getText(); //获取文本框是"address"字段的值
String phone = text5.getText(); //获取文本框是"phone"字段的值
String major = text6.getText(); //获取文本框是"major"字段的值
sql.setString(1,number); //向数据库中添加数据
sql.setString(2,name);
sql.setString(3,sex);
sql.setString(4,address);
sql.setString(5,phone);
sql.setString(6,major);
sql.executeUpdate(); //更新数据
text7.setText("学生信息录入成功");
}catch(Exception e1) {
e1.printStackTrace();
text7.setText("学生信息录入失败");
}
}
if(e.getSource() == button2) {
text1.setText(""); //将文本框置空
text2.setText("");
text3.setText("");
text4.setText("");
text5.setText("");
text6.setText("");
text7.setText("");
}
}
}
标签:setBounds,30,sql,new,学生,add,import,信息管理系统 来源: https://blog.csdn.net/qq_50550989/article/details/111472598