Java基于MySQL实现学生管理系统
程序员文章站
2023-12-13 18:25:16
本文为大家分享了java基于mysql实现学生管理系统,供大家参考,具体内容如下
因为实验室要交作业然后就做了一个学生管理系统,贴个代码纪念一下,做的太急界面什么的也...
本文为大家分享了java基于mysql实现学生管理系统,供大家参考,具体内容如下
因为实验室要交作业然后就做了一个学生管理系统,贴个代码纪念一下,做的太急界面什么的也比较差。
还有一些小细节没有完善不过还是能实现主要的功能的。
window是主界面
package first; import java.sql.*; import java.awt.*; import java.awt.event.*; import javax.swing.*; public class window { public static void main(string[] args){ jframe jframe = new jframe("学生管理系统") ; //window dimension d = new dimension(400,300); point p = new point (250,350); jframe.setsize(d); jframe.setlocation(p); jframe.setdefaultcloseoperation(jframe.exit_on_close); jframe.setvisible(true); jbutton button1 = new jbutton("添加"); jbutton button2 = new jbutton("修改"); jbutton button3 = new jbutton("查询"); jbutton button4 = new jbutton("删除"); jbutton button5 = new jbutton("浏览"); flowlayout flow = new flowlayout(flowlayout.left,10,10); jpanel panel = new jpanel(flow); panel.add(button1); panel.add(button2); panel.add(button3); panel.add(button4); panel.add(button5); jframe.add(panel); button1.addactionlistener(new actionlistener(){ public void actionperformed(actionevent e){ add add = new add(); } }); button2.addactionlistener(new actionlistener(){ public void actionperformed(actionevent e){ change change = new change(); } }); button3.addactionlistener(new actionlistener(){ public void actionperformed(actionevent e){ ask ask = new ask(); } }); button4.addactionlistener(new actionlistener(){ public void actionperformed(actionevent e){ delete delete = new delete(); } }); button5.addactionlistener(new actionlistener(){ public void actionperformed(actionevent e){ look look = new look(); } }); } }
add是添加
package first; import java.sql.*; import java.awt.*; import java.awt.event.*; import javax.swing.*; import com.mysql.jdbc.driver; import first.window; public class add extends jframe { private static final long serialversionuid = -1928970409928880648l; jlabel jlnumber = new jlabel("学号:"); jlabel jlname = new jlabel("姓名:"); jlabel jlsex = new jlabel("性别:"); jlabel jlbirthday = new jlabel("出生日期:"); jlabel jldepartment = new jlabel("学院:"); jtextfield jtnumber = new jtextfield("",20); jtextfield jtname = new jtextfield("",20); jtextfield jtsex = new jtextfield("",20); jtextfield jtbirthday = new jtextfield("",20); jtextfield jtdepartment = new jtextfield("",20); jbutton buttonadd = new jbutton("添加"); jbutton buttonreturn = new jbutton("返回"); public add() { jpanel jpnumber = new jpanel(); jpanel jpname = new jpanel(); jpanel jpsex = new jpanel(); jpanel jpbirthday = new jpanel(); jpanel jpdepartment = new jpanel(); jpanel jpforbutton = new jpanel(new gridlayout(1,1)); jpnumber.add(jlnumber); jpnumber.add(jtnumber); jpname.add(jlname); jpname.add(jtname); jpsex.add(jlsex); jpsex.add(jtsex); jpbirthday.add(jlbirthday); jpbirthday.add(jtbirthday); jpdepartment.add(jldepartment); jpdepartment.add(jtdepartment); jpforbutton.add(buttonadd); jpforbutton.add(buttonreturn); buttonadd.addactionlistener(new actionlistener(){ public void actionperformed(actionevent e){ //add connection conn = null; statement stat = null; preparedstatement ps=null; string sql = "insert into student(number,name,sex,birthday,department) " + "values(?,?,?,?,?)"; try{ class.forname("driver"); system.out.println("jbdc 加载成功!"); }catch(exception a){ system.out.println("jbdc 狗带!"); a.printstacktrace(); } try{ conn=drivermanager.getconnection("jdbc:mysql://localhost:3306/javastu","root","123"); ps=conn.preparestatement(sql); ps.setstring(1,jtnumber.gettext()); ps.setstring(2,jtname.gettext()); ps.setstring(3,jtsex.gettext()); ps.setstring(4,jtbirthday.gettext()); ps.setstring(5,jtdepartment.gettext()); ps.executeupdate(); //system.out.println("mysql 连接成功!"); //stat = conn.createstatement(); //stat.executeupdate(sql); //system.out.println("插入数据成功!"); }catch (sqlexception b){ b.printstacktrace(); }finally{ try{ conn.close(); system.out.println("mysql 关闭成功"); }catch (sqlexception c){ system.out.println("mysql 关闭失败 "); c.printstacktrace(); } } }} ); buttonreturn.addactionlistener(new actionlistener(){ public void actionperformed(actionevent e){ window window = new window(); } }); this.settitle("添加学生信息"); this.setlayout(new gridlayout(9,1)); this.add(jpnumber); this.add(jpname); this.add(jpsex); this.add(jpbirthday); this.add(jpdepartment); this.add(jpforbutton); this.setlocation(400,300); this.setsize(350,300); this.setvisible(true); } }
ask是查询
package first; import java.sql.*; import java.awt.*; import java.awt.event.*; import javax.swing.*; import first.window; public class ask extends jframe { private static final long serialversionuid = -1928970409928880648l; jlabel jlnumber = new jlabel("学号:"); jlabel jlname = new jlabel("姓名:"); jlabel jlsex = new jlabel("性别:"); jlabel jlbirthday = new jlabel("出生日期:"); jlabel jldepartment = new jlabel("学院:"); jtextfield jtnumber = new jtextfield("",20); jlabel jname = new jlabel(); jlabel jsex = new jlabel(); jlabel jbirthday = new jlabel(); jlabel jdepartment = new jlabel(); jbutton buttonask = new jbutton("查询"); jbutton buttonreturn = new jbutton("返回"); public ask() { jpanel jpnumber = new jpanel(); jpanel jpname = new jpanel(); jpanel jpsex = new jpanel(); jpanel jpbirthday = new jpanel(); jpanel jpdepartment = new jpanel(); jpanel jpforbutton = new jpanel(new gridlayout(1,1)); jpnumber.add(jlnumber); jpnumber.add(jtnumber); jpname.add(jlname); jpname.add(jname); jpsex.add(jlsex); jpsex.add(jsex); jpbirthday.add(jlbirthday); jpbirthday.add(jbirthday); jpdepartment.add(jldepartment); jpdepartment.add(jdepartment); jpforbutton.add(buttonask); jpforbutton.add(buttonreturn); buttonask.addactionlistener(new actionlistener(){ public void actionperformed(actionevent e){ connection conn = null; resultset res = null; statement stat = null; string sql = "select number,name,sex,birthday,department from student;"; try{ class.forname("com.mysql.jdbc.driver"); }catch(exception d){ system.out.println("jdbc fall"); d.printstacktrace(); } try{ conn=drivermanager.getconnection("jdbc:mysql://localhost:3306/javastu","root","123"); stat=conn.createstatement(); res=stat.executequery(sql); while (res.next()) { if (res.getstring(1).equals(jtnumber.gettext())) { jname.settext(res.getstring(2)); jsex.settext(res.getstring(3)); jbirthday.settext(res.getstring(4)); jdepartment.settext(res.getstring(5)); break; } } }catch (sqlexception e1) { // todo auto-generated catch block e1.printstacktrace(); } finally{ try{ conn.close(); }catch(sqlexception ar){ ar.printstacktrace(); } }}} ); buttonreturn.addactionlistener(new actionlistener(){ public void actionperformed(actionevent e){ window window = new window(); } }); this.settitle("查询学生信息"); this.setlayout(new gridlayout(9,1)); this.add(jpnumber); this.add(jpname); this.add(jpsex); this.add(jpbirthday); this.add(jpdepartment); this.add(jpforbutton); this.setlocation(400,300); this.setsize(350,300); this.setvisible(true); } }
change是修改
package first; import java.sql.*; import java.awt.*; import java.awt.event.*; import javax.swing.*; import first.window; public class change extends jframe { private static final long serialversionuid = -1928970409928880648l; jlabel jlnumber = new jlabel("学号:"); jlabel jlname = new jlabel("姓名:"); jlabel jlsex = new jlabel("性别:"); jlabel jlbirthday = new jlabel("出生日期:"); jlabel jldepartment = new jlabel("学院:"); jtextfield jtnumber = new jtextfield("",20); jtextfield jtname = new jtextfield("",20); jtextfield jtsex = new jtextfield("",20); jtextfield jtbirthday = new jtextfield("",20); jtextfield jtdepartment = new jtextfield("",20); jbutton buttonchange = new jbutton("修改"); jbutton buttonreturn = new jbutton("返回"); public change() { jpanel jpnumber = new jpanel(); jpanel jpname = new jpanel(); jpanel jpsex = new jpanel(); jpanel jpbirthday = new jpanel(); jpanel jpdepartment = new jpanel(); jpanel jpforbutton = new jpanel(new gridlayout(1,1)); jpnumber.add(jlnumber); jpnumber.add(jtnumber); jpname.add(jlname); jpname.add(jtname); jpsex.add(jlsex); jpsex.add(jtsex); jpbirthday.add(jlbirthday); jpbirthday.add(jtbirthday); jpdepartment.add(jldepartment); jpdepartment.add(jtdepartment); jpforbutton.add(buttonchange); jpforbutton.add(buttonreturn); buttonchange.addactionlistener(new actionlistener(){ public void actionperformed(actionevent e){ string number = jtnumber.gettext(); string name = jtname.gettext(); string sex = jtsex.gettext(); string birthday = jtbirthday.gettext(); string department = jtdepartment.gettext(); connection conn = null; resultset res = null; statement stat = null; string sql = "select number,name,sex,birthday,department from student;"; try{ class.forname("com.mysql.jdbc.driver"); }catch(exception d){ system.out.println("jdbc fall"); d.printstacktrace(); } try{ conn=drivermanager.getconnection("jdbc:mysql://localhost:3306/javastu","root","123"); stat=conn.createstatement(); res=stat.executequery(sql); while (res.next()) { //change if (res.getstring(1).equals(jtnumber.gettext())) { try{ class.forname("com.mysql.jdbc.driver"); }catch(exception d){ system.out.println("jdbc fall"); d.printstacktrace(); } string sql2="update student set name='"+name+"' where number='"+jtnumber.gettext()+"'"; string sql3="update student set sex='"+sex+"' where number='"+jtnumber.gettext()+"'"; string sql4="update student set birthday='"+birthday+"' where number='"+jtnumber.gettext()+"'"; string sql5="update student set department='"+department+"' where number='"+jtnumber.gettext()+"'"; try { conn=drivermanager.getconnection("jdbc:mysql://localhost:3306/javastu","root","123"); stat=conn.createstatement(); stat.executeupdate(sql2); stat.executeupdate(sql3); stat.executeupdate(sql4); stat.executeupdate(sql5); } catch (sqlexception g) { // todo auto-generated catch block g.printstacktrace(); }try{ stat.close(); conn.close(); }catch(sqlexception ar){ ar.printstacktrace(); } break; } //change end } }catch (sqlexception e1) { // todo auto-generated catch block e1.printstacktrace(); } finally{ try{ conn.close(); }catch(sqlexception ar){ ar.printstacktrace(); } } } }); buttonreturn.addactionlistener(new actionlistener(){ public void actionperformed(actionevent e){ window window = new window(); } }); this.settitle("修改学生信息"); this.setlayout(new gridlayout(9,1)); this.add(jpnumber); this.add(jpname); this.add(jpsex); this.add(jpbirthday); this.add(jpdepartment); this.add(jpforbutton); this.setlocation(400,300); this.setsize(350,300); this.setvisible(true); } }
delete是删除
package first; import java.sql.*; import java.awt.*; import java.awt.event.*; import javax.swing.*; import first.window; public class delete extends jframe { private static final long serialversionuid = -1928970409928880648l; jlabel jlnumber = new jlabel("学号:"); jtextfield jtnumber = new jtextfield("",20); jbutton buttondelete = new jbutton("删除"); jbutton buttonreturn = new jbutton("返回"); public delete() { jpanel jpnumber = new jpanel(); jpanel jpforbutton = new jpanel(new gridlayout(1,1)); jpnumber.add(jlnumber); jpnumber.add(jtnumber); jpforbutton.add(buttondelete); jpforbutton.add(buttonreturn); buttondelete.addactionlistener(new actionlistener(){ public void actionperformed(actionevent e){ string number = jtnumber.gettext(); connection conn = null; resultset res = null; statement stat = null; string sql = "delete from student where number='"+number+"'"; try{ class.forname("com.mysql.jdbc.driver"); }catch(exception a){ a.printstacktrace(); } try{ conn=drivermanager.getconnection("jdbc:mysql://localhost:3306/javastu","root","123"); stat = conn.createstatement(); stat.executeupdate(sql); }catch(sqlexception h){ h.printstacktrace(); }finally{ try{ conn.close(); system.out.println("close success!"); }catch(sqlexception j){ system.out.println("close go die!"); j.printstacktrace(); } } } }); buttonreturn.addactionlistener(new actionlistener(){ public void actionperformed(actionevent e){ window window = new window(); } }); this.settitle("删除学生信息"); this.setlayout(new gridlayout(9,1)); this.add(jpnumber); this.add(jpforbutton); this.setlocation(400,300); this.setsize(350,300); this.setvisible(true); } }
look是浏览
package first; import java.sql.*; import java.awt.*; import java.awt.event.*; import javax.swing.*; import java.util.*; import first.window; public class look extends jframe { private static final long serialversionuid = -1928970409928880648l; connection conn = null; preparedstatement ps = null; resultset res = null; //jbutton buttonlook = new jbutton("浏览"); //jbutton buttonreturn = new jbutton("返回"); jtable jtable; jscrollpane jscrollpane = new jscrollpane(); vector columnnames = null; vector rowdata = null; public look() { jpanel jpforbutton = new jpanel(new gridlayout(1,1)); columnnames = new vector(); columnnames.add("学号"); columnnames.add("姓名"); columnnames.add("性别"); columnnames.add("出生日期"); columnnames.add("学院"); rowdata = new vector(); //jpforbutton.add(buttonlook); //jpforbutton.add(buttonreturn); try { class.forname("com.mysql.jdbc.driver"); conn = drivermanager.getconnection("jdbc:mysql://localhost:3306/javastu","root","123"); ps = conn.preparestatement("select * from student"); res = ps.executequery(); while (res.next()) { vector hang = new vector(); hang.add(res.getstring(1)); hang.add(res.getstring(2)); hang.add(res.getstring(3)); hang.add(res.getstring(4)); hang.add(res.getstring(5)); rowdata.add(hang); } system.out.println("load ok!"); }catch (exception q){ q.printstacktrace(); system.out.println("go die"); }finally{ try{ res.close(); ps.close(); conn.close(); system.out.println("close ok"); }catch (sqlexception o){ o.printstacktrace(); system.out.println("go die 2"); } } jtable = new jtable(rowdata,columnnames); jscrollpane = new jscrollpane(jtable); this.add(jscrollpane); this.settitle("浏览学生信息"); this.setlayout(new gridlayout(2,5)); this.add(jpforbutton); this.setlocation(300,300); this.setsize(500,300); this.setvisible(true); this.setresizable(false); } }
一些运行的界面:
更多学习资料请关注专题《管理系统开发》。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。