评论

收藏

[Java] JDBC+GUI实现简单学生管理系统

编程语言 编程语言 发布于:2021-09-18 17:47 | 阅读数:503 | 评论:0

这篇文章主要为大家详细介绍了JDBC+GUI实现简单学生管理系统,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
刚学完jdbc不久,做了一个简单的学生管理系统,可能还有不完善的地方,望各路大神见谅。废话不多说,我先贴个图让大家让大家瞅瞅,觉得是你想要的再看下去吧。
DSC0000.jpg DSC0001.jpg

DSC0002.jpg

DSC0003.jpg DSC0004.jpg

我是以管理者的身份去做的,适合初学者去学习。
在做之前,先捋一遍思路,简单来说分为三大步。
一、在数据库里建student表存放学生信息
二、用jdbc来连接、操作数据库
三、展示student数据,实现增删改查功能。
思路是非常简单的,但是要实现还是有很多细节需要注意,下面我就贴上我的代码,结合着代码给大家一步步的分析说明。
实现:
一、在数据库建表:这个不用细说,直接贴图。
DSC0005.jpg

二、用jdbc连接数据库:这一块对于刚刚学jdbc的同学来说可能比较绕,所以我把这一块又分成了四部分(最后的db.properties跟com.student.db一起的),我会逐个说明。看图。
DSC0006.jpg

(1)com.student.db包里有两个类,一个是dbhelper 一个是dbmanager,这俩类是用jdbc连接数据库的,固定写法。
dbmanager类
package com.student.db;
 
import java.sql.connection;
import java.sql.preparedstatement;
import java.sql.resultset;
import java.sql.sqlexception;
import java.util.arraylist;
import java.util.list;
import com.student.mapper.imapper;
public class dbmanager {
//这里把jdbc连接数据库的步骤(找驱动,建连接,建通道,执行sql)封装在dbhelper类里面,在dbmanager里用getconnection()调用。这样写的目的是方便
 public connection getconnection(){
 try {
 return dbhelper.getconnection();//得到dbhelper类里面写好的连接
 } catch (classnotfoundexception e) {
 e.printstacktrace();
 } catch (sqlexception e) {
 e.printstacktrace();
 }
 return null;
 }
//增删改结果集。因为sql语句是变化的,所以设为参数比较方便。params是占位符的,没学的可以忽略。
 public int executeupdate(string sql,object[] params){
 connection conn=null;
 preparedstatement pst=null;
 try {
 conn=getconnection();//连接
 pst=conn.preparestatement(sql);//通道
 if(params != null){//占位符的应用。
 for(int i=0;i<params.length;i++){
 pst.setobject(i+1,params[i]);//往通道里放数据,占位符下标从1开始。
 }
 }
 return pst.executeupdate();
 
 } catch (sqlexception e) {
 e.printstacktrace();
 }
 return -1;
 }
//查询结果集。比增删改要复杂一些,慢慢看。
//这里的imapper是将所有可能的用到的类都放进去,方便以后继承使用。(现在我们写的是student信息,以后可能会有teacher信息,class信息等等)
//用接口是因为接口多继承,方便维护升级
 public list executequery(string sql,imapper mapper,object []params){
 connection conn=null;
 preparedstatement pst=null;
 resultset rst=null;//查询结果集
 list list=new arraylist();//用一个集合存放student信息
 try {
 conn=getconnection();
 pst=conn.preparestatement(sql);
 if(params != null){
 for(int i=0;i<params.length;i++){
 pst.setobject(i+1,params[i]);
 }
 }
 rst=pst.executequery();//把通道里的数据放入结果集
 list=mapper.map(rst);//imapper里有个map接口,里面存着结果集数据。把结果集的数据放入list集合
 } catch (sqlexception e) {
 e.printstacktrace();
 }
 return list;
 }
 public int count(string sql){//分页查询 count代表页数。
 connection conn=null;
 preparedstatement pst=null;
 resultset rst=null;
 try {
 conn=getconnection();
 pst=conn.preparestatement(sql);
 rst=pst.executequery();
 while(rst.next()){
 return rst.getint(1);//sql语句是select count(*) from stu,显示的是count值,就是一行。
 }
 } catch (sqlexception e) {
 e.printstacktrace();
 }
 return -1; 
 }
 
}
dbhelper类。在写之前先建一个properties文件,名字为db.properties(如图),注意不要建在包里面。
DSC0007.jpg
package com.student.db;
 
import java.io.ioexception;
import java.io.inputstream;
import java.sql.connection;
import java.sql.drivermanager;
import java.sql.sqlexception;
import java.util.properties;
public class dbhelper {
 private static string driver;
 private static string url;
 private static string user;
 private static string password;
 static{
 properties pro=new properties();
 inputstream in=dbhelper.class.getclassloader()
 .getresourceasstream("db.properties");//读取文件数据
 try {
 pro.load(in);
 } catch (ioexception e) {
 e.printstacktrace();
 }
 driver=pro.getproperty("driver");
 url=pro.getproperty("url");
 user=pro.getproperty("user");
 password=pro.getproperty("password");
 
 }
 
 public static connection getconnection() throws classnotfoundexception, sqlexception{
 class.forname(driver);//找驱动
 return drivermanager.getconnection(url, user, password);//建连接
 }
 
}
(2)com.student.vo包。这里面有一个vo类,我们是要把数据库里的数据放到java里展示,用一个类对象把数据库里的信息一一对应起来就可以很容易的操作。数据库里的一个列对应类对象的一个属性。
package com.student.vo;
 
public class student {
 private string stuid;
 private string name;
 private string age;
 private string sex;
 
 public string getstuid(){
 return stuid;
 }
 public void setstuid(string stuid){
 this.stuid=stuid;
 }
 public string getname() {
 return name;
 }
 public void setname(string name) {
 this.name = name;
 }
 public string getage() {
 return age;
 }
 public void setage(string age) {
 this.age = age;
 }
 public string getsex() {
 return sex;
 }
 public void setsex(string sex) {
 this.sex = sex;
 }
 public student(string stuid,string name,string sex,string age){
 super();
 this.stuid=stuid;
 this.name=name;
 this.age=age;
 this.sex=sex;
 }
 public student(){
 super();
 }
}
(3)com.student.mapper包。这里面一个接口,一个实现类。
接口:
package com.student.mapper;
 
import java.sql.resultset;
import java.util.list;
 
public interface imapper {
 list map(resultset rst);//声明一个方法存着结果集。
}
实现类:
package com.student.mapper;
 
import java.sql.resultset;
import java.sql.sqlexception;
import java.util.arraylist;
import java.util.list;
 
import com.student.vo.student;
public class stumapper implements imapper {//实现接口方法
 public list map(resultset rst) {
 list<student> list=new arraylist<student>();//建一个集合,里面是student类里的信息。
 try {
 while(rst.next()){//
 student stu=new student();
 stu.setstuid(rst.getstring("stuid"));//类对象每一个属性对应数据库的每一列。
 stu.setname(rst.getstring("stuname"));
 stu.setage(rst.getstring("age"));
 stu.setsex(rst.getstring("sex"));
 list.add(stu);//把类对象放到集合里
 }
 } catch (sqlexception e) {
 e.printstacktrace();
 }
 return list;
 }
 
}
(4)com.student.dao包:这里面的studao类放着增删改查分页等功能
package com.student.dao;
 
import java.util.list;
 
import com.student.db.dbmanager;
import com.student.mapper.imapper;
import com.student.mapper.stumapper;
import com.student.vo.student;
 
public class studao {
 public list<student> check(){//查看
 string sql="select * from student";//sql语句
 dbmanager db=new dbmanager();
 imapper mapper=new stumapper();//实现stumapper
 list<student> list=db.executequery(sql, mapper,null);//null是指占位符为null,因为查看的是所有信息
 return list;
 }
 public boolean add(student stu){//添加
 string sql="insert into student values(?,?,?,?)";
 object[] params={stu.getstuid(),stu.getname(),stu.getage(),stu.getsex()};
 dbmanager db=new dbmanager();
 int i=db.executeupdate(sql, params);
 if(i>=0){
 system.out.println("成功");
 }else{
 system.out.println("失败");
 }
 return true;
 }
 public boolean update(student stu){//修改
 string sql="update student set stuname=?,age=?,sex=? where stuid=?";
 object params[]={stu.getname(),stu.getage(),stu.getsex(),stu.getstuid()};
 dbmanager db=new dbmanager();
 int i=db.executeupdate(sql, params);
 if(i>=0){
 system.out.println("成功");
 }else{
 system.out.println("失败");
 }
 return true;
 }
 public boolean delete(student stu){//删除
 string sql="delete from student where stuid=?";
 object params[]={stu.getstuid()};
 dbmanager db=new dbmanager();
 int i=db.executeupdate(sql, params);
 if(i>=0){
 system.out.println("成功");
 }else{
 system.out.println("失败");
 }
 return true;
 }
 public list<student> findpage(int pagesize,int pagenow){//分页
 string sql="select * from (select rownum rn ,stu .* from stu) "
 + "where rownum<=? and rn>?";//分页公式
 object []params={pagesize,(pagenow-1)*pagesize};
 dbmanager db=new dbmanager();
 imapper mapper=new stumapper();
 return db.executequery(sql, mapper, params);
 }
 public int findcount(){
 string sql="select count(*) from stu";
 dbmanager db=new dbmanager();
 return db.count(sql);
 }
 
}
当把这一块写完之后,其实大部分就已经完成了,jdbc连接数据库基本上是固定的,多写几遍就明白了。
三、展示student信息,实现增删改查。看图:
DSC0008.jpg

(1)com.student.show包,展示界面:这里面内容比较多,但是都很容易理解
package com.student.show;
 
 
import java.awt.event.actionevent;
import java.awt.event.actionlistener;
import java.sql.sqlexception;
import java.sql.statement;
import java.util.arraylist;
import java.util.list;
 
 
import javax.swing.jbutton;
import javax.swing.jframe;
import javax.swing.joptionpane;
import javax.swing.jscrollpane;
import javax.swing.jtable;
 
 
import com.student.add.add;
import com.student.check.check;
import com.student.dao.studao;
import com.student.delete.delete;
import com.student.update.update;
import com.student.vo.student;
 
 
public class show extends jframe {
 public static int pagesize=5;//每页显示5条信息
 public static int pagenow=1;//当前页为第一页
 public show() {
 setsize(500, 430);
 setvisible(true);
 setdefaultcloseoperation(exit_on_close);//点x号就是关闭
 setresizable(false);//不可改变窗口大小
 setlocationrelativeto(null);//默认居中显示
 setlayout(null);//采用坐标布局
 
 studao dao = new studao();//前面我们已经把增删改查分页写到studao里面,现在就直接拿出来用
 list<student> list =dao.findpage(pagesize, pagenow);
 student stu = new student();
 for (int i = 0; i < list.size(); i++) {
 stu = list.get(i);
 }
 string[] rowname = { "学号", "姓名", "年龄", "性别" };//从这里开始是二维数组的遍历使用
 object[][] data = new object[list.size()][4];
 for (int i = 0; i < list.size(); i++) {
 student s = list.get(i);
 object st[] = { s.getstuid(), s.getname(), s.getage(), s.getsex() };
 data[i] = st;
 }
 final jtable table = new jtable(data,rowname);
 jscrollpane jsp=new jscrollpane(table);//这一步不能省去,否则显示不出列名
 jsp.setbounds(20, 10, 400, 200);
 add(jsp);
 
 
 jbutton jb11=new jbutton("首页");
 jb11.setbounds(40,220,80,30);
 add(jb11);
 jbutton jb22=new jbutton("上一页");
 jb22.setbounds(130,220,80,30);
 add(jb22);
 jbutton jb33=new jbutton("下一页");
 jb33.setbounds(220,220,80,30);
 add(jb33);
 jbutton jb44=new jbutton("尾页");
 jb44.setbounds(310,220,80,30);
 add(jb44);
 
 
 jbutton jb1 = new jbutton("查看信息");
 jb1.setbounds(50, 270, 100, 30);
 add(jb1);
 jbutton jb2 = new jbutton("修改信息");
 jb2.setbounds(280, 270, 100, 30);
 add(jb2);
 jbutton jb3 = new jbutton("添加信息");
 jb3.setbounds(50, 320, 100, 30);
 add(jb3);
 jbutton jb4 = new jbutton("删除信息");
 jb4.setbounds(280, 320, 100, 30);
 add(jb4);
 jbutton jb5 = new jbutton("退出");
 jb5.setbounds(280, 360, 100, 30);
 add(jb5);
 
 
 jb1.addactionlistener(new actionlistener() {//查看 
 public void actionperformed(actionevent event) {
 int row = table.getselectedrow();//选中第几行
 int index = 0;
 if(row==-1){
 joptionpane.showmessagedialog(null,"您没有选中信息");
 return;
 }
 string id = (string) table.getvalueat(row, index);// 跟check联系起来
 check check=new check(id);
 check.setvisible(true);
 setvisible(false); 
 }
 });
 
 
 jb2.addactionlistener(new actionlistener() {//修改
 public void actionperformed(actionevent event) {
 int row = table.getselectedrow();
 int index = 0;
 if(row==-1){
 joptionpane.showmessagedialog(null,"您没有选中信息");
 return;
 }
 string id = (string) table.getvalueat(row, index);// 跟update联系起来
 update up=new update(id);
 up.setvisible(true);
 setvisible(false);
 
 
 }
 });
 
 
 jb3.addactionlistener(new actionlistener() {//添加
 public void actionperformed(actionevent event) {
 add add = new add();
 add.setvisible(true);
 setvisible(false);
 }
 });
 jb4.addactionlistener(new actionlistener() {//删除
 public void actionperformed(actionevent event) {
 int row = table.getselectedrow();
 int index = 0;
 if(row==-1){
 joptionpane.showmessagedialog(null,"您没有选中信息");
 return;
 }
 string num=(string) table.getvalueat(row, index);
 delete d=new delete(num);
 d.setvisible(true);
 setvisible(false);
 
 }
 });
 jb11.addactionlistener(new actionlistener() {//首页
 public void actionperformed(actionevent event) {
 pagenow=1;
 show show=new show();
 setvisible(false);
 show.setvisible(true);
 }
 });
 jb22.addactionlistener(new actionlistener() {//上一页
 public void actionperformed(actionevent event) {
 if(pagenow != 1){
 pagenow=pagenow-1;
 }else{
 return;
 }
 show show=new show();
 setvisible(false);
 show.setvisible(true);
 }
 });
 jb33.addactionlistener(new actionlistener() {//下一页
 public void actionperformed(actionevent event) {
 studao dao=new studao();
 int count=dao.findcount();
 int pagecount=(count-1)/pagesize+1;//pagecount表示最后一页
 if(pagenow != pagecount){
 pagenow=pagenow+1;
 }else{
 return;
 }
 show show=new show();
 setvisible(false);
 show.setvisible(true);
 }
 });
 jb44.addactionlistener(new actionlistener() {//尾页
 public void actionperformed(actionevent event) {
 studao dao=new studao();
 int count=dao.findcount();
 int pagecount=(count-1)/pagesize+1;
 pagenow=pagecount;
 show show=new show();
 setvisible(false);
 show.setvisible(true);
 }
 });
 }
 
 
 public static void main(string args[]) {
 show s = new show();
 }
}
(2)增删改查:大同小异,因为我们在studao里面已经写好了,在用的时候就方便多了。
DSC0009.jpg DSC00010.jpg

①添加:
package com.student.add;
 
 
import java.sql.sqlexception;
import java.util.list;
 
import javax.swing.jbutton;
import javax.swing.jframe;
import javax.swing.jlabel;
import javax.swing.joptionpane;
import javax.swing.jtextfield;
 
import com.student.dao.studao;
import com.student.db.dbmanager;
import com.student.show.show;
import com.student.vo.student;
 
 
public class add extends jframe{
 public add(){
 setsize(300,400);
 setvisible(true);
 setdefaultcloseoperation(exit_on_close);
 setresizable(false);
 setlocationrelativeto(null);
 setlayout(null);
 
 jlabel j0=new jlabel("添加信息");
 j0.setbounds(100,20,80,30);
 add(j0);
 
 jlabel j1=new jlabel("学号:");
 j1.setbounds(30,70,50,30);
 add(j1);
 
 final jtextfield jt1=new jtextfield();
 jt1.setbounds(100,70,130,30);
 add(jt1);
 
 jlabel j2=new jlabel("姓名:");
 j2.setbounds(30,120,50,30);
 add(j2);
 
 final jtextfield jt2=new jtextfield();
 jt2.setbounds(100,120,130,30);
 add(jt2);
 
 jlabel j3=new jlabel("性别:");
 j3.setbounds(30,170,50,30);
 add(j3);
 
 final jtextfield jt3=new jtextfield();
 jt3.setbounds(100,170,130,30);
 add(jt3);
 
 jlabel j4=new jlabel("年龄:");
 j4.setbounds(30,220,50,30);
 add(j4);
 
 final jtextfield jt4=new jtextfield();
 jt4.setbounds(100,220,130,30);
 add(jt4);
 
 jbutton jb1=new jbutton("添加");
 jb1.setbounds(50,280,80,30);
 add(jb1);
 
 jbutton jb2=new jbutton("返回");
 jb2.setbounds(150,280,80,30);
 add(jb2);
 
 jb1.addactionlistener(new actionlistener(){
 public void actionperformed(actionevent event){
 string a=jt1.gettext();//获取输入的信息
 string b=jt2.gettext();
 string c=jt3.gettext();
 string d=jt4.gettext();
 student stu=new student(a,b,c,d);
 studao dao=new studao();
 list<student> list=dao.check();//调用studao里面的check()方法
 for(student st:list){//遍历集合
 if(st.getstuid().equals(a)){
 joptionpane.showmessagedialog(null,"该账号存在");
 return;
 }
 }
 dao.add(stu);
 joptionpane.showmessagedialog(null,"添加成功");
 show show=new show();
 show.setvisible(true);
 setvisible(false);
 }
 });
 jb2.addactionlistener(new actionlistener(){
 public void actionperformed(actionevent event){
 show s=new show();
 s.setvisible(true);
 setvisible(false);
 }
 });
 }
 public static void main(string []args){
 add add=new add();
 }
}
②修改:
package com.student.update;
 
import java.awt.event.actionevent;
import java.awt.event.actionlistener;
 
import java.util.list;
 
import javax.swing.jbutton;
import javax.swing.jframe;
import javax.swing.jlabel;
import javax.swing.joptionpane;
import javax.swing.jtextfield;
 
import com.student.dao.studao;
import com.student.db.dbmanager;
import com.student.show.show;
import com.student.vo.student;
 
public class update extends jframe{
 public update(final string id){
 setsize(300,400);
 setvisible(true);
 setdefaultcloseoperation(exit_on_close);
 setresizable(false);
 setlocationrelativeto(null);
 setlayout(null);
 
 jlabel j0=new jlabel("修改信息");
 j0.setbounds(100,20,80,30);
 add(j0);
 
 jlabel j1=new jlabel("学号:");
 j1.setbounds(30,70,50,30);
 add(j1);
 
 final jlabel jt1=new jlabel();
 jt1.setbounds(100,70,130,30);
 add(jt1);
 
 jlabel j2=new jlabel("姓名:");
 j2.setbounds(30,120,50,30);
 add(j2);
 
 final jtextfield jt2=new jtextfield();
 jt2.setbounds(100,120,130,30);
 add(jt2);
 
 jlabel j3=new jlabel("年龄:");
 j3.setbounds(30,170,50,30);
 add(j3);
 
 final jtextfield jt3=new jtextfield();
 jt3.setbounds(100,170,130,30);
 add(jt3);
 
 jlabel j4=new jlabel("性别:");
 j4.setbounds(30,220,50,30);
 add(j4);
 
 final jtextfield jt4=new jtextfield();
 jt4.setbounds(100,220,130,30);
 add(jt4);
 
 jbutton jb1=new jbutton("修改");
 jb1.setbounds(50,280,80,30);
 add(jb1);
 
 jbutton jb2=new jbutton("返回");
 jb2.setbounds(150,280,80,30);
 add(jb2);
 
 studao dao=new studao();
 list<student> list=dao.check();
 student stu=new student();
 for(int i=0;i<list.size();i++){//遍历,找到与id相同的学号。
 stu=list.get(i);
 if(stu.getstuid().equals(id)){//id是参数,跟前面show联系起来。
 break;
 }
 }
 jt1.settext(stu.getstuid());
 jt2.settext(stu.getname());
 jt3.settext(stu.getage());
 jt4.settext(stu.getsex());
 
 jb1.addactionlistener(new actionlistener(){
 public void actionperformed(actionevent event){
 studao dao=new studao();
 student stu=new student();
 stu.setstuid(id);
 stu.setname(jt2.gettext());
 stu.setage(jt3.gettext());
 stu.setsex(jt4.gettext());
 dao.update(stu);//studao里的update()已经写好如何修改,这里直接用
 joptionpane.showmessagedialog(null,"修改成功");
 show show=new show();
 show.setvisible(true);
 setvisible(false);
 }
 });
 jb2.addactionlistener(new actionlistener(){
 public void actionperformed(actionevent event){
 show s=new show();
 s.setvisible(true);
 setvisible(false);
 }
 });
 }
 
 
}
③查看:
package com.student.check;
 
import java.awt.event.actionevent;
import java.awt.event.actionlistener;
import java.util.list;
 
import javax.swing.jbutton;
import javax.swing.jframe;
import javax.swing.jlabel;
import javax.swing.jtextfield;
 
import com.student.dao.studao;
import com.student.show.show;
import com.student.vo.student;
 
public class check extends jframe{
 public check(string id) {
 setsize(300,400);
 setvisible(true);
 setdefaultcloseoperation(exit_on_close);
 setresizable(false);
 setlocationrelativeto(null);
 setlayout(null);
 
 jlabel j0=new jlabel("学生信息");
 j0.setbounds(100,20,80,30);
 add(j0);
 
 jlabel j1=new jlabel("学号:");
 j1.setbounds(30,70,50,30);
 add(j1);
 
 final jlabel jt1=new jlabel();
 jt1.setbounds(100,70,130,30);
 add(jt1);
 
 jlabel j2=new jlabel("姓名:");
 j2.setbounds(30,120,50,30);
 add(j2);
 
 final jlabel jt2=new jlabel();
 jt2.setbounds(100,120,130,30);
 add(jt2);
 
 jlabel j3=new jlabel("年龄:");
 j3.setbounds(30,170,50,30);
 add(j3);
 
 final jlabel jt3=new jlabel();
 jt3.setbounds(100,170,130,30);
 add(jt3);
 
 jlabel j4=new jlabel("性别:");
 j4.setbounds(30,220,50,30);
 add(j4);
 
 final jlabel jt4=new jlabel();
 jt4.setbounds(100,220,130,30);
 add(jt4);
 
 jbutton jb1=new jbutton("确认");
 jb1.setbounds(50,280,80,30);
 add(jb1);
 
 jbutton jb2=new jbutton("返回");
 jb2.setbounds(150,280,80,30);
 add(jb2);
 
 studao dao=new studao();
 list<student> list=dao.check();
 student stu=new student();
 for(int i=0;i<list.size();i++){
 stu=list.get(i);
 if(stu.getstuid().equals(id)){
 break;
 }
 }
 jt1.settext(stu.getstuid());
 jt2.settext(stu.getname());
 jt3.settext(stu.getage());
 jt4.settext(stu.getsex());
 
 jb1.addactionlistener(new actionlistener(){
 public void actionperformed(actionevent event){
 show s=new show();
 s.setvisible(true);
 setvisible(false);
 }
 });
 jb2.addactionlistener(new actionlistener(){
 public void actionperformed(actionevent event){
 show s=new show();
 s.setvisible(true);
 setvisible(false);
 }
 });
 
 }
 
}
④删除:
package com.student.delete;
 
import java.awt.event.actionevent;
import java.awt.event.actionlistener;
import java.util.list;
 
import javax.swing.jbutton;
import javax.swing.jframe;
import javax.swing.jlabel;
import javax.swing.joptionpane;
import javax.swing.jtextfield;
 
import com.student.dao.studao;
import com.student.db.dbmanager;
import com.student.show.show;
import com.student.vo.student;
 
 
public class delete extends jframe{
 public delete(final string num){
 setsize(300,400);
 setvisible(true);
 setdefaultcloseoperation(exit_on_close);
 setresizable(false);
 setlocationrelativeto(null);
 setlayout(null);
 
 jlabel j0=new jlabel("您确认要删除该信息吗");
 j0.setbounds(100,20,200,30);
 add(j0);
 
 jlabel j1=new jlabel("学号:");
 j1.setbounds(30,70,50,30);
 add(j1);
 
 final jlabel jt1=new jlabel();
 jt1.setbounds(100,70,130,30);
 add(jt1);
 
 jlabel j2=new jlabel("姓名:");
 j2.setbounds(30,120,50,30);
 add(j2);
 
 final jlabel jt2=new jlabel();
 jt2.setbounds(100,120,130,30);
 add(jt2);
 
 jlabel j3=new jlabel("年龄:");
 j3.setbounds(30,170,50,30);
 add(j3);
 
 final jlabel jt3=new jlabel();
 jt3.setbounds(100,170,130,30);
 add(jt3);
 
 jlabel j4=new jlabel("性别:");
 j4.setbounds(30,220,50,30);
 add(j4);
 
 final jlabel jt4=new jlabel();
 jt4.setbounds(100,220,130,30);
 add(jt4);
 
 jbutton jb1=new jbutton("确认");
 jb1.setbounds(20,280,80,30);
 add(jb1);
 
 jbutton jb2=new jbutton("返回");
 jb2.setbounds(180,280,80,30);
 add(jb2);
 
 studao dao=new studao();
 list<student> list=dao.check();
 student stu=new student();
 for(int i=0;i<list.size();i++){
 stu=list.get(i);
 if(stu.getstuid().equals(num)){
 break;
 }
 }
 
 jt1.settext(stu.getstuid());
 jt2.settext(stu.getname());
 jt3.settext(stu.getage());
 jt4.settext(stu.getsex());
 
 jb1.addactionlistener(new actionlistener(){
 public void actionperformed(actionevent event){
 studao dao=new studao();
 student stu=new student();
 stu.setstuid(num);
 dao.delete(stu);
 joptionpane.showmessagedialog(null,"删除成功");
 show show=new show();
 show.setvisible(true);
 setvisible(false);
 }
 });
 jb2.addactionlistener(new actionlistener(){
 public void actionperformed(actionevent event){
 show s=new show();
 s.setvisible(true);
 setvisible(false);
 }
 });
 }
 
}
最后贴一下登录页面,因为是以管理者的身份登录的不需要判断,就非常简单:
DSC00011.jpg
package com.student.login;
 
import java.awt.event.actionevent;
import java.awt.event.actionlistener;
 
import javax.swing.jbutton;
import javax.swing.jframe;
import javax.swing.jlabel;
import javax.swing.joptionpane;
import javax.swing.jpasswordfield;
import javax.swing.jtextfield;
import com.student.show.show;
 
public class login extends jframe{
 public login(){
 setsize(300,250);
 setvisible(true);
 setdefaultcloseoperation(exit_on_close);
 setresizable(false);
 setlocationrelativeto(null);
 setlayout(null);
 
 jlabel j=new jlabel("登录窗口");
 j.setbounds(100,20,80,30);
 add(j);
 
 jlabel j1=new jlabel("用户名:");
 j1.setbounds(50,80,60,30);
 add(j1);
 
 final jtextfield jt1=new jtextfield();
 jt1.setbounds(120,80,120,30);
 add(jt1);
 
 jlabel j2=new jlabel("密 码:");
 j2.setbounds(50,130,60,30);
 add(j2);
 
 final jpasswordfield jp=new jpasswordfield();
 jp.setbounds(120,130,120,30);
 add(jp);
 
 jbutton jb1=new jbutton("登录");
 jb1.setbounds(70,180,60,30);
 add(jb1);
 
 jbutton jb2=new jbutton("重置");
 jb2.setbounds(170,180,60,30);
 add(jb2);
 
 jb1.addactionlistener(new actionlistener(){
 public void actionperformed(actionevent event){
 string id=jt1.gettext();
 char ch[]=jp.getpassword();
 string pass=new string(ch);
 if(id.equals(abcdefj){//设置用户名为abcdefj
 if(pass.equals(123456)){//设置密码为123456
 joptionpane.showmessagedialog(null,"登录成功");
 show s=new show();//成功后跳到show
 s.setvisible(true);
 setvisible(false);
 }else{
 joptionpane.showmessagedialog(null,"密码错误");
 jt1.settext("");
 return;
 }
 }else{
 joptionpane.showmessagedialog(null,"您输入的账号有误");
 jt1.settext("");
 jp.settext("");
 return;
 }
 }
 });
 }
 public static void main(string []args){
 login lo=new login();
 }
}
写在最后:
刚开始学的时候感觉很绕,尤其是jdbc那,后来发现,是因为前面java基础掌握的不行,我又回去好好复习了java基础,才发现jdbc是死的,固定的写法,背过就行了。所以再做这个学生管理系统,就感觉不复杂了。先有一个大的思路,然后顺着思路往下走,逐步实现每个功能。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持CodeAE代码之家
原文链接:https://blog.csdn.net/qq_41717599/article/details/79976755

关注下面的标签,发现更多相似文章