java+SQL server2008学生信息管理系统源码
程序员文章站
2023-12-09 14:09:15
本文实例为大家分享了java学生信息管理系统源码的具体代码,供大家参考,具体内容如下
1、studetmanage类(主界面)
package com.sms3...
本文实例为大家分享了java学生信息管理系统源码的具体代码,供大家参考,具体内容如下
1、studetmanage类(主界面)
package com.sms3; import java.awt.*; import javax.swing.*; import java.awt.event.*; public class studentmanage extends jframe implements actionlistener { /** * @param args */ public static void main(string[] args) { // todo auto-generated method stub new studentmanage(); } //========面板控件 private jlabel querylab = null; private jtextfield querytxt = null; private jbutton querybtn = null; private jbutton allbtn = null; private jtable resulttb = null; private jscrollpane jsp = null; private jbutton addbtn = null; private jbutton deletebtn = null; private jbutton updatebtn = null; private jpanel top = null; private jpanel bottom = null; //======== private stumodel sm = null; //构造函数 public studentmanage() { /***************************初始化面板控件***********************/ //========查询栏 querylab = new jlabel("请输入姓名:"); querytxt = new jtextfield(10); querybtn = new jbutton("查询"); allbtn = new jbutton("全部"); //......添加查询栏监听 querybtn.addactionlistener(this); querybtn.setactioncommand("query"); allbtn.addactionlistener(this); allbtn.setactioncommand("all"); //========增删改栏 addbtn = new jbutton("添加"); deletebtn = new jbutton("删除"); updatebtn = new jbutton("修改"); //......添加增删改栏监听 addbtn.addactionlistener(this); addbtn.setactioncommand("add"); deletebtn.addactionlistener(this); deletebtn.setactioncommand("delete"); updatebtn.addactionlistener(this); updatebtn.setactioncommand("update"); //========创建窗口整体布局 //......顶层查询栏 top = new jpanel(); top.add(querylab); top.add(querytxt); top.add(querybtn); top.add(allbtn); //......底层增删改栏 bottom = new jpanel(); bottom.add(addbtn); bottom.add(deletebtn); bottom.add(updatebtn); //......中间层显示栏 sm = new stumodel(); string sql = "select * from stu"; sm.querystu(sql, null); resulttb = new jtable(sm); jsp = new jscrollpane(resulttb); //......构建整体布局 this.add(top,borderlayout.north); this.add(jsp,borderlayout.center); this.add(bottom,borderlayout.south); //========设置窗口属性 this.setsize(400, 300); this.setdefaultcloseoperation(jframe.exit_on_close); this.setvisible(true); this.setresizable(false); } //监听 @override public void actionperformed(actionevent e) { // todo auto-generated method stub if(e.getactioncommand().equals("query")) { /*********************查询***********************/ //========获取输入学生的姓名 string name = querytxt.gettext().trim(); if(name.length() != 0) { //========姓名输入有效时,执行查询 //......定义参数 string sql = "select * from stu where stuname=?"; string []paras = {name}; //......更新模型 jtableupdate(sql, paras); } else { //========姓名为空时,设置提醒 joptionpane.showmessagedialog(this, "姓名输入不能为空"); } } else if(e.getactioncommand().equals("add")) { /*********************添加***********************/ new stuadddialog(this, "添加学生信息", true); string sql = "select * from stu"; jtableupdate(sql, null); } else if(e.getactioncommand().equals("all")) { /*********************全部显示***********************/ string sql = "select * from stu"; jtableupdate(sql, null); } else if(e.getactioncommand().equals("delete")) { /*********************删除***********************/ //========获取选择行号 int rownum = this.resulttb.getselectedrow(); if(rownum == -1) { joptionpane.showmessagedialog(this, "请选择一行"); return ; } //========获取学生id号 string stuid = (string)sm.getvalueat(rownum, 0); //========删除学生 string sql = "delete from stu where stuid=?"; string []paras = {stuid}; stumodel tmp = new stumodel(); tmp.cudstu(sql, paras); //========更新模型 sql = "select * from stu"; jtableupdate(sql, null); } else if(e.getactioncommand().equals("update")) { /*********************修改***********************/ //========获取选择行号 int rownum = this.resulttb.getselectedrow(); if(rownum == -1) { joptionpane.showmessagedialog(this, "请选择一行"); return ; } new stuupdatedialog(this, "修改学生信息", true, sm, rownum); string sql = "select * from stu"; jtableupdate(sql, null); } } //========更新jtable内数据 public void jtableupdate(string sql, string[] paras) { //......创建模型 sm = new stumodel(); sm.querystu(sql, paras); //......更新显示 resulttb.setmodel(sm); } }
2、stumodel类(学生数据库模型)
package com.sms3; import java.sql.resultset; import java.util.vector; import javax.swing.table.abstracttablemodel; public class stumodel extends abstracttablemodel{ private vector columnnames; private vector rowdates; // public stumodel() { string sql = "select * from stu"; string []paras = {}; } //========增删改学生 public boolean cudstu(string sql, string []paras) { return new sqlhelper().cudexecute(sql, paras); } //========查询学生 public void querystu(string sql, string []paras) { sqlhelper sqlhelper = null; //========初始化jtable信息 columnnames = new vector(); rowdates = new vector(); columnnames.add("学号"); columnnames.add("名字"); columnnames.add("性别"); columnnames.add("年龄"); columnnames.add("籍贯"); columnnames.add("系别"); try { sqlhelper = new sqlhelper(); resultset rs = sqlhelper.queryexecute(sql, paras); while(rs.next()) { vector row = new vector(); row.add(rs.getstring(1)); row.add(rs.getstring(2)); row.add(rs.getstring(3)); row.add(rs.getstring(4)); row.add(rs.getstring(5)); row.add(rs.getstring(6)); rowdates.add(row); } } catch (exception e) { // todo: handle exception } finally { sqlhelper.close(); } } @override public int getcolumncount() { // todo auto-generated method stub return this.columnnames.size(); } @override public int getrowcount() { // todo auto-generated method stub return this.rowdates.size(); } @override public object getvalueat(int row, int col) { // todo auto-generated method stub if(!rowdates.isempty()) return ((vector)this.rowdates.get(row)).get(col); else return null; } @override public string getcolumnname(int column) { // todo auto-generated method stub return (string)this.columnnames.get(column); } }
3、stuadddialog类(添加学生信息子界面)
package com.sms3; import java.awt.borderlayout; import java.awt.dialog; import java.awt.frame; import java.awt.gridlayout; import java.awt.event.actionevent; import java.awt.event.actionlistener; import javax.swing.*; public class stuadddialog extends jdialog implements actionlistener{ //=========面板控件 //......左侧标题栏 private jlabel idlab,namelab,sexlab,agelab,jglab,deptlab; //......右侧信息选择填写栏 private jtextfield idtxt,nametxt,sextxt,agetxt,jgtxt,depttxt; //......添加和取消按钮 private jbutton addbtn,cancelbtn; //......布局控件 private jpanel left,center,bottom; //构造函数 public stuadddialog(frame owner, string title, boolean modal) { //========重写父类方法 super(owner, title, modal); //========左侧标签栏 idlab = new jlabel("学号: "); namelab = new jlabel("姓名: "); sexlab = new jlabel("性别: "); agelab = new jlabel("年龄: "); jglab = new jlabel("籍贯: "); deptlab = new jlabel("系别: "); //========右侧信息填写栏 idtxt = new jtextfield(); nametxt = new jtextfield(); sextxt = new jtextfield(); agetxt = new jtextfield(); jgtxt = new jtextfield(); depttxt = new jtextfield(); //========添加和取消按钮 addbtn = new jbutton("添加"); cancelbtn = new jbutton("取消"); //......添加监听 addbtn.addactionlistener(this); addbtn.setactioncommand("add"); cancelbtn.addactionlistener(this); cancelbtn.setactioncommand("cancel"); //========创建布局 //......创建左边栏 left = new jpanel(); left.setlayout(new gridlayout(6, 1)); left.add(idlab); left.add(namelab); left.add(sexlab); left.add(agelab); left.add(jglab); left.add(deptlab); //......创建右边栏 center = new jpanel(); center.setlayout(new gridlayout(6, 1)); center.add(idtxt); center.add(nametxt); center.add(sextxt); center.add(agetxt); center.add(jgtxt); center.add(depttxt); //========底层添加和取消按钮 bottom = new jpanel(); bottom.add(addbtn); bottom.add(cancelbtn); //========整体布局 this.add(left,borderlayout.west); this.add(center,borderlayout.center); this.add(bottom,borderlayout.south); //========设置窗口属性 this.setsize(300, 250); this.setresizable(false); this.setvisible(true); } @override public void actionperformed(actionevent e) { // todo auto-generated method stub if(e.getactioncommand().equals("add")) { /***********************添加学生信息**************************/ stumodel tmp = new stumodel(); string sql = "insert into stu values(?,?,?,?,?,?)"; string []paras = {idtxt.gettext(),nametxt.gettext(),sextxt.gettext(), agetxt.gettext(),jgtxt.gettext(),depttxt.gettext()}; if(!tmp.cudstu(sql, paras)) joptionpane.showmessagedialog(this, "添加学生信息失败"); //========关闭窗口 this.dispose(); } else if(e.getactioncommand().equals("cancel")) { //========关闭窗口 this.dispose(); } } }
4、stuupdatedialog类(修改学生信息子界面)
package com.sms3; import java.awt.borderlayout; import java.awt.frame; import java.awt.gridlayout; import java.awt.event.actionevent; import java.awt.event.actionlistener; import javax.swing.jbutton; import javax.swing.jdialog; import javax.swing.jlabel; import javax.swing.joptionpane; import javax.swing.jpanel; import javax.swing.jtextfield; import javax.swing.table.abstracttablemodel; public class stuupdatedialog extends jdialog implements actionlistener{ //=========面板控件 //......左侧标题栏 private jlabel idlab,namelab,sexlab,agelab,jglab,deptlab; //......右侧信息选择填写栏 private jtextfield idtxt,nametxt,sextxt,agetxt,jgtxt,depttxt; //......添加和取消按钮 private jbutton addbtn,cancelbtn; //......布局控件 private jpanel left,center,bottom; //构造函数 public stuupdatedialog(frame owner, string title, boolean modal, stumodel sm, int rownum) { //========重写父类方法 super(owner, title, modal); //========左侧标签栏 idlab = new jlabel("学号: "); namelab = new jlabel("姓名: "); sexlab = new jlabel("性别: "); agelab = new jlabel("年龄: "); jglab = new jlabel("籍贯: "); deptlab = new jlabel("系别: "); //========右侧信息填写栏 idtxt = new jtextfield(); idtxt.settext((string)sm.getvalueat(rownum, 0)); idtxt.seteditable(false); nametxt = new jtextfield(); nametxt.settext((string)sm.getvalueat(rownum, 1)); sextxt = new jtextfield(); sextxt.settext((string)sm.getvalueat(rownum, 2)); agetxt = new jtextfield(); agetxt.settext((string)sm.getvalueat(rownum, 3)); jgtxt = new jtextfield(); jgtxt.settext((string)sm.getvalueat(rownum, 4)); depttxt = new jtextfield(); depttxt.settext((string)sm.getvalueat(rownum, 5)); //========添加和取消按钮 addbtn = new jbutton("修改"); cancelbtn = new jbutton("取消"); //......添加监听 addbtn.addactionlistener(this); addbtn.setactioncommand("update"); cancelbtn.addactionlistener(this); cancelbtn.setactioncommand("cancel"); //========创建布局 //......创建左边栏 left = new jpanel(); left.setlayout(new gridlayout(6, 1)); left.add(idlab); left.add(namelab); left.add(sexlab); left.add(agelab); left.add(jglab); left.add(deptlab); //......创建右边栏 center = new jpanel(); center.setlayout(new gridlayout(6, 1)); center.add(idtxt); center.add(nametxt); center.add(sextxt); center.add(agetxt); center.add(jgtxt); center.add(depttxt); //========底层添加和取消按钮 bottom = new jpanel(); bottom.add(addbtn); bottom.add(cancelbtn); //========整体布局 this.add(left,borderlayout.west); this.add(center,borderlayout.center); this.add(bottom,borderlayout.south); //========设置窗口属性 this.setsize(300, 250); this.setresizable(false); this.setvisible(true); } @override public void actionperformed(actionevent e) { // todo auto-generated method stub if(e.getactioncommand().equals("update")) { /***********************修改学生信息**************************/ stumodel tmp = new stumodel(); string sql = "update stu set stuname=?,stusex=?,stuage=?,stujg=?,studept=? where stuid=?"; string []paras = {nametxt.gettext(),sextxt.gettext(),agetxt.gettext(), jgtxt.gettext(),depttxt.gettext(),idtxt.gettext()}; if(!tmp.cudstu(sql, paras)) joptionpane.showmessagedialog(this, "修改学生信息失败"); //========关闭窗口 this.dispose(); } else if(e.getactioncommand().equals("cancel")) { //========关闭窗口 this.dispose(); } } }
5、sqlhelper类(最底层数据库类)
package com.sms3; import java.sql.*; public class sqlhelper { //========数据库 private connection ct = null; private preparedstatement ps = null; private resultset rs = null; private string driver = "com.microsoft.sqlserver.jdbc.sqlserverdriver"; private string url = "jdbc:sqlserver://127.0.0.1:1433;database=studentman"; private string user = "sa"; private string passwd = "****"; //========查询 public resultset queryexecute(string sql, string []paras) { try { //========1、加载驱动 class.forname(driver); //========2、连接 ct = drivermanager.getconnection(url, user, passwd); //========3、创建preparedstatement ps = ct.preparestatement(sql); //========4、给问号赋值 if(paras != null) { for(int i = 0; i < paras.length; i++) { ps.setstring(i + 1, paras[i]); } } //========5、执行 rs = ps.executequery(); } catch (exception e) { // todo: handle exception e.printstacktrace(); } finally { //this.close(); } //========返回值 return rs; } //========增删改 public boolean cudexecute(string sql, string []paras) { boolean b = true; try { //========1、加载驱动 class.forname(driver); //========2、连接 ct = drivermanager.getconnection(url, user, passwd); //========3、创建preparedstatement ps = ct.preparestatement(sql); //========4、给问号赋值 for(int i = 0; i < paras.length; i++) { ps.setstring(i + 1, paras[i]); } //========5、执行 if(ps.executeupdate() != 1) b = false; } catch (exception e) { // todo: handle exception b = false; e.printstacktrace(); } finally { this.close(); } //========返回值 return b; } //========关闭资源 public void close() { try { if(rs!=null) rs.close(); if(ps!=null) ps.close(); if(ct!=null) ct.close(); } catch (exception e2) { // todo: handle exception e2.printstacktrace(); } } }
主界面
添加学生信息界面
修改学生信息界面
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。