java(swing)+ mysql实现学生信息管理系统源码
程序员文章站
2023-12-19 13:44:58
本文实例为大家分享了java实现学生信息管理系统源码,供大家参考,具体内容如下
import java.awt.borderlayout;
import jav...
本文实例为大家分享了java实现学生信息管理系统源码,供大家参考,具体内容如下
import java.awt.borderlayout; import java.awt.color; import java.awt.font; import java.awt.gridlayout; 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 java.util.arraylist; import java.util.list; import javax.swing.borderfactory; import javax.swing.jbutton; import javax.swing.jframe; import javax.swing.joptionpane; import javax.swing.jpanel; import javax.swing.jscrollpane; import javax.swing.jtable; import javax.swing.jtextarea; import javax.swing.table.abstracttablemodel; import javax.swing.text.badlocationexception; /* drop database if exists `myproject`; create database myproject default charset utf8 collate utf8_general_ci; use abc; set names utf8mb4; set foreign_key_checks = 0; drop table if exists `student`; create table `student` ( `id` varchar(36) not null, `name` varchar(36) not null, `age` varchar(36) not null ) engine=innodb default charset=utf8; set foreign_key_checks = 1; * * */ public class test extends jframe { private static final long serialversionuid = 1l; private jtable table; private jpanel panel; private jscrollpane scrollpane; private jbutton button1, button2, button3; private jtextarea text1, text2, text3; private list<student> stu; public test() throws badlocationexception, sqlexception { super("学生信息"); this.setsize(500, 340); this.add(getjscrollpane(stu), borderlayout.center); this.add(getjpanel(), borderlayout.south); this.setresizable(true); this.setlocation(300, 300); this.setdefaultcloseoperation(jframe.exit_on_close); } // 设置jscrollpane方法 private jscrollpane getjscrollpane(list<student> stu) throws sqlexception { if (scrollpane == null) { scrollpane = new jscrollpane(); scrollpane.setviewportview(getjtable(stu)); } return scrollpane; } // 设置jpanel方法 private jpanel getjpanel() { if (panel == null) { panel = new jpanel(); panel.setlayout(new gridlayout(2, 3)); text1 = new jtextarea(); text2 = new jtextarea(); text3 = new jtextarea(); button1 = new jbutton("添加"); button2 = new jbutton("删除"); button3 = new jbutton("更新"); button1.addactionlistener(new insert()); button2.addactionlistener(new delete()); button3.addactionlistener(new update()); text1.setborder(borderfactory.createlineborder(color.gray, 2)); text2.setborder(borderfactory.createlineborder(color.gray, 2)); text3.setborder(borderfactory.createlineborder(color.gray, 2)); text1.setfont(new font("宋体", font.bold, 16)); text2.setfont(new font("宋体", font.bold, 16)); text3.setfont(new font("宋体", font.bold, 16)); text1.settext("id"); text2.settext("name"); text3.settext("age"); panel.add(text1); panel.add(text2); panel.add(text3); panel.add(button1); panel.add(button2); panel.add(button3); } return panel; } // 设置jtable方法 private void setjtable(jtable table) { table.setfont(new font("宋体", font.bold, 18)); table.setrowheight(30); } // 获取jtable对象方法(该方法具体就是获得jtable对象的时候 一并从数据取出学生信息并放入jtable表格中) private jtable getjtable(list<student> stu) throws sqlexception { if (table == null) { jdbcdaoimpl jdbc = new jdbcdaoimpl(); resultset rs = jdbc.search(); stu = select(rs); jdbc.closeconnection(); table = new jtable(new table(stu)); setjtable(table); } return table; } // 设置学生信息方法(该方法是用户增加 删除 更新用户操作的具体实现方法 包含了完整性检查) private student setstu() { if (text1.gettext().equals("") || text2.gettext().equals("") || text3.gettext().equals("")) { return null; } else { student sd = new student(); sd.setid(text1.gettext()); sd.setname(text2.gettext()); sd.setage(text3.gettext()); return sd; } } // 重置输入框为空 private void resettext() { text1.settext(""); text2.settext(""); text3.settext(""); } // 刷新学生信息方法(该方法是重新读取数据库学生的信息 然后返回一个学生的集合 用于刷新jtable表格对象中的数据) private list<student> select(resultset rs) throws sqlexception { list<student> st = new arraylist<student>(); while (rs.next()) { student s = new student(); s.setid(rs.getstring(1)); s.setname(rs.getstring(2)); s.setage(rs.getstring(3)); st.add(s); } return st; } // 添加按钮-监听器(该方法是对添加按钮实现的具体方法 ) class insert implements actionlistener { @override public void actionperformed(actionevent e) { stu = new arraylist<student>(); student sd = new student(); jdbcdaoimpl jdbc = new jdbcdaoimpl(); sd = setstu(); if (sd != null) { jdbc.insert(sd); resultset rs = jdbc.search(); try { stu = select(rs); } catch (sqlexception e1) { e1.printstacktrace(); } jdbc.closeconnection(); jtable table = new jtable(new table(stu));//新建一个jtable 对象 用来盛放增加后的学生信息 setjtable(table);//设置jtable信息 test.this.scrollpane.setviewportview(table);//把jtable设置到panel resettext(); } else { joptionpane.showmessagedialog(test.this, "输入数据不完整"); } } } // 删除按钮-监听器(该方法是对删除按钮实现的具体方法) class delete implements actionlistener { @override public void actionperformed(actionevent e) { stu = new arraylist<student>(); student sd = new student(); jdbcdaoimpl jdbc = new jdbcdaoimpl(); sd = setstu(); if (sd != null) { jdbc.delete(sd); resultset rs = jdbc.search(); try { stu = select(rs); } catch (sqlexception e1) { e1.printstacktrace(); } jdbc.closeconnection(); jtable table = new jtable(new table(stu));//新建一个jtable 对象 用来盛放增加后的学生信息 setjtable(table);//设置jtable信息 test.this.scrollpane.setviewportview(table);//把jtable设置到panel resettext(); } else { joptionpane.showmessagedialog(test.this, "输入数据不完整"); } } } // 更新按钮-监听器(该方法是对更新按钮实现的具体方法) class update implements actionlistener { @override public void actionperformed(actionevent e) { stu = new arraylist<student>(); student sd = new student(); jdbcdaoimpl jdbc = new jdbcdaoimpl(); sd = setstu(); if (sd != null) { jdbc.update(sd); resultset rs = jdbc.search(); try { stu = select(rs); } catch (sqlexception e1) { e1.printstacktrace(); } jdbc.closeconnection(); jtable table = new jtable(new table(stu));//新建一个jtable 对象 用来盛放增加后的学生信息 setjtable(table);//设置jtable信息 test.this.scrollpane.setviewportview(table);//把jtable设置到panel resettext(); } else { joptionpane.showmessagedialog(test.this, "输入数据不完整"); } } } // student类 (用于封装数据信息和数据库表进行映射) public class student { // 学生的id name age信息 private string id; private string name; private string age; // get&set方法 public string getid() { return id; } public void setid(string id) { this.id = id; } 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; } } // jtable 表模式类 (jtable对象 初始化的时候通过 这个table获取表格的行数、列数、列标题、以及每个单元格存放的数据 具体使用原因放在开头的备注了) public class table extends abstracttablemodel { list<student> stu = new arraylist<student>(); public table(list s) { this.stu = s; } public list<student> getstu() { return stu; } public void setstu(list<student> stu) { this.stu = stu; } @override // 获取行数 public int getrowcount() { return stu.size(); } @override // 获取列数 public int getcolumncount() { // todo auto-generated method stub return 3; } @override public boolean iscelleditable(int rowindex, int columnindex) { return true; } @override // 获取列名字 public string getcolumnname(int col) { string res = ""; switch (col) { case 0: res = "id"; break; case 1: res = "name"; break; case 2: res = "age"; break; default: break; } return res; } @override // 获取具体值 public object getvalueat(int rowindex, int columnindex) { // todo auto-generated method stub object res = ""; student temp = stu.get(rowindex); switch (columnindex) { case 0: res = temp.getid(); break; case 1: res = temp.getname(); break; case 2: res = temp.getage(); break; default: break; } return res; } } // jdbcdao类 配置连接数据的信息,链接释放操作和基本增删改查操作 public class jdbcdaoimpl { string driver = "com.mysql.jdbc.driver"; string url = "jdbc:mysql://localhost:3306/test1?useunicode=true&characterencoding=utf-8&usessl=false"; string user = "root"; string passwd = "123456"; connection conn = null; statement stmt = null; resultset rs = null; // 数据库连接开始 public connection getconnection() { try { class.forname("com.mysql.jdbc.driver"); conn = drivermanager.getconnection(url, user, passwd); stmt = conn.createstatement(); } catch (exception e) { e.printstacktrace(); } return conn; } // 数据库连接释放 public void closeconnection() { if (rs != null) { try { rs.close(); stmt.close(); conn.close(); } catch (sqlexception e) { // todo auto-generated catch block e.printstacktrace(); } } if (rs == null) { try { stmt.close(); conn.close(); } catch (sqlexception e) { e.printstacktrace(); } } } // 查找操作 public resultset search() { getconnection(); try { string sql = "select * from student"; rs = stmt.executequery(sql); } catch (sqlexception e) { e.printstacktrace(); } return rs; } // 添加操作 public void insert(student sd) { // todo auto-generated method stub getconnection(); try { string sql = "insert into student(id,name,age)" + "values('" + sd.getid() + "','" + sd.getname() + "','" + sd.getage() + "')"; int count = stmt.executeupdate(sql); } catch (sqlexception e) { e.printstacktrace(); } } // 删除操作 public void delete(student sd) { // todo auto-generated method stub getconnection(); try { string sql = "delete from student where id = '" + sd.getid() + "'"; int count = stmt.executeupdate(sql); } catch (sqlexception e) { e.printstacktrace(); } } // 更新操作 public void update(student sd) { // todo auto-generated method stub getconnection(); try { string sql = "update student set name='" + sd.getname() + "',age= '" + sd.getage() + "'where id = '" + sd.getid() + "'"; int count = stmt.executeupdate(sql); } catch (sqlexception e) { e.printstacktrace(); } } } // main 方法 public static void main(string[] args) throws badlocationexception, sqlexception { new test().setvisible(true); } }
关于管理系统的更多内容请点击《管理系统专题》进行学习
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。