关注博主不迷路,博主带你码代码!
这是一个学生信息管理系统,学生信息主要包括:学号,姓名,性别,出生日期,班级;暂时就只有这些信息,若后续需要,可以相应的进行添加。
该项目主要采用的技术是 java gui 设计,这个地方主要用 java 工具 windowsbuilder 进行布局,(windowsbuilder没有安装或者不会安装的,可以看看我的这篇博客 eclipse 安装 windowbuilder插件 ),mysql 数据库(mysql 数据库没有安装的 或者数据库使用软件 navicat 没有安装的可以看看我的这篇博客 2021 mysql 8.0.26 安装教程(最详细的傻瓜教程) ),以及使用 java 中的 jdbc 将数据库链接起来。
由于在项目中没有创建学生数据库的功能,所以学生数据库必须要先建立好,下面给出如何建立好mysql学生数据库的相关代码 图片
-- 创建数据库create database niitdefault character set utf8mb4;show databases;-- 使用数据库niituse niit;-- 创建表studentcreate table student( id int primary key, name varchar(4), gender char(1), dob date, batch int);-- 插入记录insert into student values(1,'独孤求败','男','1988-1-2',1),(2,'周芷若','女','1988-12-3',1),(3,'金毛狮王','男','1978-11-1',1),(4,'蒙挚','男','1989-2-5',2),(5,'夏春','女','1998-4-3',2),(6,'梅长苏','男','1999-12-6',2);-- 查看表中所有记录 语法:select * from 表名select * from student;
ag凯发k8国际主页面
添加学生
修改信息
删除学生
查询信息
package studentsmanager;import java.awt.color;import java.awt.eventqueue;import java.awt.font;import javax.swing.jbutton;import javax.swing.jframe;import javax.swing.jlabel;import javax.swing.jpanel;import javax.swing.border.emptyborder;import java.awt.event.actionlistener;import java.awt.event.actionevent;public class mainframe extends jframe { private jpanel contentpane; /** * launch the application. */ // 有了main函数才可以单独运行 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("学生管理系统"); // 由于这是ag凯发k8国际主页面,所有当ag凯发k8国际主页面关闭的时候,程序就直接退出 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); }}
package studentsmanager;import java.awt.eventqueue;import java.awt.font;import java.awt.event.actionevent;import java.awt.event.actionlistener;import java.sql.sqlexception;import javax.swing.boxlayout;import javax.swing.jbutton;import javax.swing.jframe;import javax.swing.jlabel;import javax.swing.joptionpane;import javax.swing.jpanel;import javax.swing.jtextfield;import javax.swing.border.emptyborder;public class addframe extends jframe { private jpanel contentpane; private jtextfield idfield; private jtextfield namefield; private jtextfield genderfield; private jtextfield dobfield; private jtextfield batchfield; /** * launch the application. */ public static void main(string[] args) { eventqueue.invokelater(new runnable() { public void run() { try { addframe frame = new addframe(); frame.setvisible(true); } catch (exception e) { e.printstacktrace(); } } }); } /** * create the frame. */ public addframe() { // 和前面类似 setresizable(false); settitle("添加学生"); // 这个地方,添加学生只是这个程序的一部分,所以当关闭这部分的时候,程序不直接退出,只是关闭该部分程序 setdefaultcloseoperation(jframe.dispose_on_close); setbounds(100, 100, 450, 470); setlocationrelativeto(null); contentpane = new jpanel(); contentpane.setborder(new emptyborder(5, 5, 5, 5)); setcontentpane(contentpane); contentpane.setlayout(new boxlayout(contentpane, boxlayout.y_axis)); jpanel panel = new jpanel(); contentpane.add(panel); panel.setlayout(null); jlabel titlelabel = new jlabel("请输入新学生的信息:"); titlelabel.setfont(new font("宋体", font.bold, 20)); titlelabel.setbounds(71, 34, 208, 45); panel.add(titlelabel); jlabel idlabel = new jlabel("学号:"); idlabel.setfont(new font("宋体", font.plain, 16)); idlabel.setbounds(71, 105, 50, 30); panel.add(idlabel); idfield = new jtextfield(); idfield.setbounds(143, 99, 240, 45); panel.add(idfield); idfield.setcolumns(10); jlabel namelabel = new jlabel("姓名:"); namelabel.setfont(new font("宋体", font.plain, 16)); namelabel.setbounds(71, 160, 50, 30); panel.add(namelabel); namefield = new jtextfield(); namefield.setcolumns(10); namefield.setbounds(143, 154, 240, 45); panel.add(namefield); jlabel genderlabel = new jlabel("性别:"); genderlabel.setfont(new font("宋体", font.plain, 16)); genderlabel.setbounds(71, 215, 50, 30); panel.add(genderlabel); genderfield = new jtextfield(); genderfield.setcolumns(10); genderfield.setbounds(143, 209, 240, 45); panel.add(genderfield); jlabel doblabel = new jlabel("出生日期:"); doblabel.setfont(new font("宋体", font.plain, 16)); doblabel.setbounds(41, 270, 80, 30); panel.add(doblabel); dobfield = new jtextfield(); dobfield.setcolumns(10); dobfield.setbounds(143, 264, 240, 45); panel.add(dobfield); jlabel batchlabel = new jlabel("班级:"); batchlabel.setfont(new font("宋体", font.plain, 16)); batchlabel.setbounds(71, 325, 50, 30); panel.add(batchlabel); batchfield = new jtextfield(); batchfield.setcolumns(10); batchfield.setbounds(143, 319, 240, 45); panel.add(batchfield); jbutton addbutton = new jbutton("添加"); // 添加鼠标监听事件 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); }}
添加成功:
添加失败:
修改信息
package studentsmanager;import java.awt.eventqueue;import java.awt.font;import javax.swing.boxlayout;import javax.swing.jbutton;import javax.swing.jframe;import javax.swing.jlabel;import javax.swing.joptionpane;import javax.swing.jpanel;import javax.swing.jtextfield;import javax.swing.border.emptyborder;import java.awt.event.actionlistener;import java.sql.sqlexception;import java.awt.event.actionevent;public class changeframe extends jframe { private jpanel contentpane; private jtextfield idfield; private jtextfield namefield; private jtextfield genderfield; private jtextfield dobfield; private jtextfield batchfield; private jtextfield searchfield; /** * launch the application. */ public static void main(string[] args) { eventqueue.invokelater(new runnable() { public void run() { try { changeframe frame = new changeframe(); frame.setvisible(true); } catch (exception e) { e.printstacktrace(); } } }); } /** * create the frame. */ public changeframe() { setresizable(false); settitle("修改信息"); setdefaultcloseoperation(jframe.dispose_on_close); setbounds(100, 100, 450, 470); setlocationrelativeto(null); contentpane = new jpanel(); contentpane.setborder(new emptyborder(5, 5, 5, 5)); setcontentpane(contentpane); contentpane.setlayout(new boxlayout(contentpane, boxlayout.y_axis)); jpanel panel = new jpanel(); contentpane.add(panel); panel.setlayout(null); jlabel lblnewlabel = new jlabel("请输入要修改的学生的学号:"); lblnewlabel.setfont(new font("宋体", font.plain, 16)); lblnewlabel.setbounds(71, 0, 208, 29); panel.add(lblnewlabel); jlabel lblnewlabel_1 = new jlabel("学号:"); lblnewlabel_1.setfont(new font("宋体", font.plain, 16)); lblnewlabel_1.setbounds(71, 105, 50, 30); panel.add(lblnewlabel_1); idfield = new jtextfield(); idfield.seteditable(false); idfield.setbounds(143, 99, 240, 45); panel.add(idfield); idfield.setcolumns(10); jlabel lblnewlabel_1_1 = new jlabel("姓名:"); lblnewlabel_1_1.setfont(new font("宋体", font.plain, 16)); lblnewlabel_1_1.setbounds(71, 160, 50, 30); panel.add(lblnewlabel_1_1); namefield = new jtextfield(); namefield.setcolumns(10); namefield.setbounds(143, 154, 240, 45); panel.add(namefield); jlabel lblnewlabel_1_1_1 = new jlabel("性别:"); lblnewlabel_1_1_1.setfont(new font("宋体", font.plain, 16)); lblnewlabel_1_1_1.setbounds(71, 215, 50, 30); panel.add(lblnewlabel_1_1_1); genderfield = new jtextfield(); genderfield.setcolumns(10); genderfield.setbounds(143, 209, 240, 45); panel.add(genderfield); jlabel lblnewlabel_1_1_1_1 = new jlabel("出生日期:"); lblnewlabel_1_1_1_1.setfont(new font("宋体", font.plain, 16)); lblnewlabel_1_1_1_1.setbounds(41, 270, 80, 30); panel.add(lblnewlabel_1_1_1_1); dobfield = new jtextfield(); dobfield.setcolumns(10); dobfield.setbounds(143, 264, 240, 45); panel.add(dobfield); jlabel lblnewlabel_1_1_1_2 = new jlabel("班级:"); lblnewlabel_1_1_1_2.setfont(new font("宋体", font.plain, 16)); lblnewlabel_1_1_1_2.setbounds(71, 325, 50, 30); panel.add(lblnewlabel_1_1_1_2); batchfield = new jtextfield(); batchfield.setcolumns(10); batchfield.setbounds(143, 319, 240, 45); panel.add(batchfield); jbutton changebutton = new jbutton("修改"); changebutton.addactionlistener(new actionlistener() { public void actionperformed(actionevent arg0) { int id = integer.parseint(searchfield.gettext()); string namestring = namefield.gettext(); string gendestring = genderfield.gettext(); string dobstring = dobfield.gettext(); int batch = integer.parseint(batchfield.gettext()); sqlhelp sqlhelp = new sqlhelp(); try { sqlhelp.changestudent(id, namestring, gendestring, dobstring, batch); joptionpane.showmessagedialog(changeframe.this, "修改成功!"); } catch (sqlexception e) { joptionpane.showmessagedialog(changeframe.this, "修改失败!"); 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); }}
修改前查询到学生信息
修改成功
修改失败
失败的情况是数据链接出现了问题
未查询到学生信息
package studentsmanager;import java.awt.eventqueue;import java.awt.font;import javax.swing.boxlayout;import javax.swing.jbutton;import javax.swing.jframe;import javax.swing.jlabel;import javax.swing.joptionpane;import javax.swing.jpanel;import javax.swing.jtextfield;import javax.swing.border.emptyborder;import java.awt.event.actionlistener;import java.sql.sqlexception;import java.awt.event.actionevent;public class deleteframe extends jframe { private jpanel contentpane; private jtextfield idfield; private jtextfield namefield; private jtextfield genderfield; private jtextfield dobfield; private jtextfield batchfield; private jtextfield searchfield; /** * launch the application. */ public static void main(string[] args) { eventqueue.invokelater(new runnable() { public void run() { try { deleteframe frame = new deleteframe(); frame.setvisible(true); } catch (exception e) { e.printstacktrace(); } } }); } /** * create the frame. */ public deleteframe() { setresizable(false); settitle("删除学生"); setdefaultcloseoperation(jframe.dispose_on_close); setbounds(100, 100, 450, 470); setlocationrelativeto(null); contentpane = new jpanel(); contentpane.setborder(new emptyborder(5, 5, 5, 5)); setcontentpane(contentpane); contentpane.setlayout(new boxlayout(contentpane, boxlayout.y_axis)); jpanel panel = new jpanel(); contentpane.add(panel); panel.setlayout(null); jlabel lblnewlabel = new jlabel("请输入要删除的学生的学号:"); lblnewlabel.setfont(new font("宋体", font.plain, 16)); lblnewlabel.setbounds(71, 0, 208, 29); panel.add(lblnewlabel); jlabel lblnewlabel_1 = new jlabel("学号:"); lblnewlabel_1.setfont(new font("宋体", font.plain, 16)); lblnewlabel_1.setbounds(71, 105, 50, 30); panel.add(lblnewlabel_1); idfield = new jtextfield(); idfield.seteditable(false); idfield.setbounds(143, 99, 240, 45); panel.add(idfield); idfield.setcolumns(10); jlabel lblnewlabel_1_1 = new jlabel("姓名:"); lblnewlabel_1_1.setfont(new font("宋体", font.plain, 16)); lblnewlabel_1_1.setbounds(71, 160, 50, 30); panel.add(lblnewlabel_1_1); namefield = new jtextfield(); namefield.setcolumns(10); namefield.setbounds(143, 154, 240, 45); panel.add(namefield); jlabel lblnewlabel_1_1_1 = new jlabel("性别:"); lblnewlabel_1_1_1.setfont(new font("宋体", font.plain, 16)); lblnewlabel_1_1_1.setbounds(71, 215, 50, 30); panel.add(lblnewlabel_1_1_1); genderfield = new jtextfield(); genderfield.setcolumns(10); genderfield.setbounds(143, 209, 240, 45); panel.add(genderfield); jlabel lblnewlabel_1_1_1_1 = new jlabel("出生日期:"); lblnewlabel_1_1_1_1.setfont(new font("宋体", font.plain, 16)); lblnewlabel_1_1_1_1.setbounds(41, 270, 80, 30); panel.add(lblnewlabel_1_1_1_1); dobfield = new jtextfield(); dobfield.setcolumns(10); dobfield.setbounds(143, 264, 240, 45); panel.add(dobfield); jlabel lblnewlabel_1_1_1_2 = new jlabel("班级:"); lblnewlabel_1_1_1_2.setfont(new font("宋体", font.plain, 16)); lblnewlabel_1_1_1_2.setbounds(71, 325, 50, 30); panel.add(lblnewlabel_1_1_1_2); batchfield = new jtextfield(); batchfield.setcolumns(10); batchfield.setbounds(143, 319, 240, 45); panel.add(batchfield); jbutton deletebutton = new jbutton("删除"); deletebutton.addactionlistener(new actionlistener() { public void actionperformed(actionevent e) { int id = integer.parseint(searchfield.gettext()); try { sqlhelp sqlhelp = new sqlhelp(); sqlhelp.deletestudent(id); joptionpane.showmessagedialog(deleteframe.this, "删除成功!"); } catch (sqlexception e1) { joptionpane.showmessagedialog(deleteframe.this, "删除失败"); e1.printstacktrace(); } } }); deletebutton.setfont(new font("宋体", font.plain, 18)); deletebutton.setbounds(182, 389, 97, 33); panel.add(deletebutton); searchfield = new jtextfield(); searchfield.setbounds(71, 39, 208, 45); panel.add(searchfield); searchfield.setcolumns(10); jbutton searchbutton = new jbutton("查找"); searchbutton.addactionlistener(new actionlistener() { public void actionperformed(actionevent arg0) { int id = integer.parseint(searchfield.gettext()); try { sqlhelp sqlhelp = new sqlhelp(); student student = sqlhelp.querystudent(id); if(student != null) { idfield.settext(string.valueof(id)); namefield.settext(student.getname()); genderfield.settext(student.getgender()); dobfield.settext(student.getdob()); batchfield.settext(string.valueof(student.getbatch())); } else { joptionpane.showmessagedialog(deleteframe.this, "无此学生"); } } catch (sqlexception e) { e.printstacktrace(); } } }); searchbutton.setfont(new font("宋体", font.plain, 18)); searchbutton.setbounds(289, 42, 97, 39); panel.add(searchbutton); }}
删除成功
删除失败
删除失败的时候是数据库链接出现了问题
查询到学生信息
未查询到学生
package studentsmanager;import java.awt.eventqueue;import java.awt.font;import java.awt.event.actionevent;import java.awt.event.actionlistener;import java.sql.sqlexception;import javax.swing.boxlayout;import javax.swing.jbutton;import javax.swing.jframe;import javax.swing.jlabel;import javax.swing.joptionpane;import javax.swing.jpanel;import javax.swing.jtextfield;import javax.swing.border.emptyborder;public class queryframe extends jframe { private jpanel contentpane; private jtextfield idfield; private jtextfield namefield; private jtextfield genderfield; private jtextfield dobfield; private jtextfield batchfield; private jtextfield searchfield; /** * launch the application. */ public static void main(string[] args) { eventqueue.invokelater(new runnable() { public void run() { try { queryframe frame = new queryframe(); frame.setvisible(true); } catch (exception e) { e.printstacktrace(); } } }); } /** * create the frame. */ public queryframe() { setresizable(false); settitle("查询信息"); setdefaultcloseoperation(jframe.dispose_on_close); setbounds(100, 100, 450, 470); setlocationrelativeto(null); contentpane = new jpanel(); contentpane.setborder(new emptyborder(5, 5, 5, 5)); setcontentpane(contentpane); contentpane.setlayout(new boxlayout(contentpane, boxlayout.y_axis)); jpanel panel = new jpanel(); contentpane.add(panel); panel.setlayout(null); jlabel lblnewlabel = new jlabel("请输入要查找的学生的学号:"); lblnewlabel.setfont(new font("宋体", font.plain, 16)); lblnewlabel.setbounds(71, 0, 208, 29); panel.add(lblnewlabel); jlabel lblnewlabel_1 = new jlabel("学号:"); lblnewlabel_1.setfont(new font("宋体", font.plain, 16)); lblnewlabel_1.setbounds(71, 105, 50, 30); panel.add(lblnewlabel_1); idfield = new jtextfield(); idfield.seteditable(false); idfield.setbounds(143, 99, 240, 45); panel.add(idfield); idfield.setcolumns(10); jlabel lblnewlabel_1_1 = new jlabel("姓名:"); lblnewlabel_1_1.setfont(new font("宋体", font.plain, 16)); lblnewlabel_1_1.setbounds(71, 160, 50, 30); panel.add(lblnewlabel_1_1); namefield = new jtextfield(); namefield.setcolumns(10); namefield.setbounds(143, 154, 240, 45); panel.add(namefield); jlabel lblnewlabel_1_1_1 = new jlabel("性别:"); lblnewlabel_1_1_1.setfont(new font("宋体", font.plain, 16)); lblnewlabel_1_1_1.setbounds(71, 215, 50, 30); panel.add(lblnewlabel_1_1_1); genderfield = new jtextfield(); genderfield.setcolumns(10); genderfield.setbounds(143, 209, 240, 45); panel.add(genderfield); jlabel lblnewlabel_1_1_1_1 = new jlabel("出生日期:"); lblnewlabel_1_1_1_1.setfont(new font("宋体", font.plain, 16)); lblnewlabel_1_1_1_1.setbounds(41, 270, 80, 30); panel.add(lblnewlabel_1_1_1_1); dobfield = new jtextfield(); dobfield.setcolumns(10); dobfield.setbounds(143, 264, 240, 45); panel.add(dobfield); jlabel lblnewlabel_1_1_1_2 = new jlabel("班级:"); lblnewlabel_1_1_1_2.setfont(new font("宋体", font.plain, 16)); lblnewlabel_1_1_1_2.setbounds(71, 325, 50, 30); panel.add(lblnewlabel_1_1_1_2); batchfield = new jtextfield(); batchfield.setcolumns(10); batchfield.setbounds(143, 319, 240, 45); panel.add(batchfield); searchfield = new jtextfield(); searchfield.setbounds(71, 39, 208, 45); panel.add(searchfield); searchfield.setcolumns(10); jbutton searchbutton = new jbutton("查找"); searchbutton.addactionlistener(new actionlistener() { public void actionperformed(actionevent arg0) { // 1.读出要查询的学生输入的id // 因为学生的学号是唯一的,所以我们根据学号查询后,只会有一条数据 // 转化一下数据类型 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); }}
sqlhelp 工具类
package studentsmanager;import java.sql.connection;import java.sql.drivermanager;import java.sql.preparedstatement;import java.sql.resultset;import java.sql.sqlexception;public class sqlhelp { public static final string jdbc_driver = "com.mysql.cj.jdbc.driver"; public static final string db_url = "jdbc:mysql://localhost:3306/niit?usessl=false&allowpublickeyretrieval=true&servertimezone=utc"; public static final string username = "root"; public static final string password = "011216"; private connection connection = null; private preparedstatement pstatement = null; private resultset rset = null; // 加载驱动 // 静态初始化块(只执行一次) 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 studentsmanager;public class student { private string name; private string gender; private string dob; private int batch; public student(string name, string gender, string dob, int batch) { this.name = name; this.gender = gender; this.dob = dob; this.batch = batch; } public string getname() { return name; } public string getgender() { return gender; } public string getdob() { return dob; } public int getbatch() { return batch; } }
这算一个比较完整的项目了,既有 gui 又有 mysql 数据库的已经 java 的 jdbc ,虽然已经有了 gui 界面,但很明显,没有进行任何美化,就只是简单的一个界面,勉勉强强可以看的过去,使用 jdbc 链接数据库的时候,没有实现可以直接在程序创建数据库,只能从数据库方创建数据库,这是一个缺点,同时使用 jdbc 链接数据库的时候,所使用的代码依旧是 mysql 数据库的代码,(我 mysql 学的不好,增删改查啥的数据库代码都还记不清楚 哭了…)还有就是当程序在进行修改和删除的时候,修改失败和删除失败的时候,都是因为数据链接失败了才进行弹窗,不知道怎么改成无法修改或者无法删除的时候提示,(又哭了…)
欢迎个位大佬指出错误,同时也欢迎各位小白借鉴,有问题的话,评论或者私聊都是可以的,看了就会回复!
最后一句:
关注博主不迷路,博主带你码代码!
原文地址:https://blog.csdn.net/qq_52354698/article/details/121582956
ag凯发k8国际的版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。