首页 学生管理系统-韩顺平java

学生管理系统-韩顺平java

举报
开通vip

学生管理系统-韩顺平java学生管理系统-韩顺平java /** * 功能:简易学生管理系统 * 1.能过姓名查询; * 2.增.删.改学生信息 * */ package com.sutmanage.version2; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.MouseEvent; import java.awt.event.MouseLi...

学生管理系统-韩顺平java
学生管理系统-韩顺平java /** * 功能:简易学生管理系统 * 1.能过姓名查询; * 2.增.删.改学生信息 * */ package com.sutmanage.version2; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.MouseEvent; import java.awt.event.MouseListener; import java.util.*; import javax.swing.*; import com.mysql.jdbc.PreparedStatement; import java.sql.*; public class StuManageSystemVersion1 extends JFrame implements ActionListener{ //定义组件,界面由三部分组成,上部是姓名查询部分,为一个JPanel,中间是一个JScorllPane,底部也是一个JPanel,放了三个按钮 JPanel top,bottom; JScrollPane jsp; JTable jt; JButton search,add,delete,update; JLabel name; JTextField jtf; StuModel sm=null; public StuManageSystemVersion1(){ //创建组件 //Top组件 top=new JPanel(); name=new JLabel("请输入姓名"); jtf=new JTextField(22); search=new JButton("查询"); search.addActionListener(this); top.add(name); top.add(jtf); top.add(search); sm=new StuModel(); sm.queryStu("select * from stus", null); jt=new JTable(sm); jt.setSelectionBackground(Color.RED); jsp=new JScrollPane(jt); //底部组件 bottom=new JPanel(); add=new JButton("增加"); add.addActionListener(this); delete=new JButton("删除"); delete.addActionListener(this); update=new JButton("修改"); update.addActionListener(this); bottom.add(add); bottom.add(delete); bottom.add(update); //添加组件 this.add(top,BorderLayout.NORTH); this.add(jsp,BorderLayout.CENTER); this.add(bottom,BorderLayout.SOUTH); //设置窗体属性 this.setSize(400,300); this.setTitle("学生管理系统"); int w=Toolkit.getDefaultToolkit().getScreenSize().width; int h=Toolkit.getDefaultToolkit().getScreenSize().height; this.setLocation(w/2-200, h/2-150); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.setVisible(true); } public static void main(String[] args) { // TODO Auto-generated method stub new StuManageSystemVersion1(); } //更新数据模型 public void updStuModel(){ sm=new StuModel(); sm.queryStu("select * from stus", null); jt.setModel(sm); } @Override public void actionPerformed(ActionEvent e) { // TODO Auto-generated method stub if(e.getSource()==search){ String s=jtf.getText().trim(); String name[]={s}; String sql=""; if(s.length()!=0){ sql="select * from Stus where StuName=?"; }else if(s.length()==0){ sql="select * from Stus"; name=null; } sm=new StuModel(); sm.queryStu(sql, name); jt.setModel(sm); jtf.setText(""); } else if(e.getSource()==add){ Boolean flag=true; while(flag){ AddNewStu ans=new AddNewStu(this, "添加新学生", true); //更新数据模型 this.updStuModel(); //确认是否添加新的学生 int i=JOptionPane.showConfirmDialog(this, "是否继续添加学生?","",JOptionPane.YES_NO_OPTION); if(i==JOptionPane.NO_OPTION){ flag=false; } } } else if(e.getSource()==delete){ if(jt.getSelectedRow()==-1){ JOptionPane.showMessageDialog(this, "请先选择一行数据"); return; }else{ int selectID=jt.getSelectedRow(); String stuID[]={jt.getValueAt(selectID, 0).toString()}; String sql="delete from stus where stuID=?"; sm=new StuModel(); sm.updStu(sql, stuID); //更新数据模型 this.updStuModel(); } } else if(e.getSource()==update){ if(jt.getSelectedRow()==-1){ JOptionPane.showMessageDialog(this, "请先选择一行数据"); return; }else{ int id=jt.getSelectedRow(); sm=new StuModel(); sm.queryStu("select * from stus", null); UpdateStu us=new UpdateStu(this, "修改学生信息", true,sm,id); //更新数据模型 this.updStuModel(); } } } } /** * 这是我的一个Stu 关于同志近三年现实表现材料材料类招标技术评分表图表与交易pdf视力表打印pdf用图表说话 pdf 的模型 * */ package com.sutmanage.version2; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import java.util.Vector; import javax.swing.table.AbstractTableModel; import com.mysql.fabric.xmlrpc.base.Array; public class StuModel extends AbstractTableModel { ResultSet rs; Vector cloumn; Vector row; SqlHelper sh; // 查询数据 public void queryStu(String sql, String[] args) { // 中间的显示组件 cloumn = new Vector(); cloumn.add("学生ID"); cloumn.add("姓名"); cloumn.add("年龄"); cloumn.add("性别"); cloumn.add("系别"); row = new Vector(); sh = new SqlHelper(); rs = sh.query(sql, args); try { while (rs.next()) { Vector hang = new Vector(); hang.add(rs.getInt(1)); hang.add(rs.getString(2)); hang.add(rs.getInt(3)); hang.add(rs.getString(4)); hang.add(rs.getString(5)); row.add(hang); } } catch (Exception e) { // TODO: handle exception } sh.close(); } // 增.删.改操作 public boolean updStu(String sql, String[] args) { sh = new SqlHelper(); return sh.updSQL(sql, args); } @Override // 得到共有多少列 public int getColumnCount() { // TODO Auto-generated method stub return this.cloumn.size(); } @Override // 得到共有多少行 public int getRowCount() { // TODO Auto-generated method stub return this.row.size(); } @Override // 得到某行某列的值 public Object getValueAt(int rowIndex, int columnIndex) { // TODO Auto-generated method stub return this.row.get(rowIndex).get(columnIndex); } @Override public String getColumnName(int index) { // TODO Auto-generated method stub return this.cloumn.get(index); } } package com.sutmanage.version2; import java.awt.BorderLayout; import java.awt.GridLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.*; public class AddNewStu extends JDialog implements ActionListener { // 定义swing组件 JPanel top, bottom, left; JLabel stuID, stuName, stuAge, stuSex, stuDepart; JTextField id, name, age, sex, depart; JButton save, cancel; StuModel sm = null; // 构造函数 public AddNewStu(JFrame owner, String title, boolean modal) { super(owner, title, modal); // 创建组件 top = new JPanel(new GridLayout(5, 1)); left = new JPanel(new GridLayout(5, 1)); stuID = new JLabel("学生ID", JLabel.CENTER); stuName = new JLabel("姓名", JLabel.CENTER); stuAge = new JLabel("年龄", JLabel.CENTER); stuSex = new JLabel("性别", JLabel.CENTER); stuDepart = new JLabel("系别", JLabel.CENTER); id = new JTextField(15); name = new JTextField(15); age = new JTextField(15); sex = new JTextField(15); depart = new JTextField(15); top.add(stuID); top.add(stuName); top.add(stuAge); top.add(stuSex); top.add(stuDepart); left.add(id); left.add(name); left.add(age); left.add(sex); left.add(depart); bottom = new JPanel(); save = new JButton("保存"); save.addActionListener(this); cancel = new JButton("取消"); cancel.addActionListener(this); bottom.add(save); bottom.add(cancel); this.add(top, BorderLayout.CENTER); this.add(left, BorderLayout.EAST); this.add(bottom, BorderLayout.SOUTH); // 设置窗体属性 this.setSize(230, 200); this.setLocation(1000, 300); this.setVisible(true); } @Override public void actionPerformed(ActionEvent e) { // TODO Auto-generated method stub if (e.getSource() == save) { String values[] = { id.getText().trim(), name.getText().trim(), age.getText().trim(), sex.getText().trim(), depart.getText().trim() }; String sql = "insert into stus values(?,?,?,?,?)"; sm = new StuModel(); sm.updStu(sql, values); this.dispose(); } else if(e.getSource()==cancel){ this.dispose(); } } } package com.sutmanage.version2; import java.awt.BorderLayout; 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.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JTextField; public class UpdateStu extends JDialog implements ActionListener { // 定义swing组件 JPanel top, bottom, left; JLabel stuID, stuName, stuAge, stuSex, stuDepart; JTextField id, name, age, sex, depart; JButton save, cancel; StuModel sm = null; String values[]; // 构造函数 public UpdateStu(JFrame owner, String title, boolean modal, StuModel sm, int idValue) { super(owner, title, modal); // 创建组件 top = new JPanel(new GridLayout(5, 1)); left = new JPanel(new GridLayout(5, 1)); stuID = new JLabel("学生ID", JLabel.CENTER); stuName = new JLabel("姓名", JLabel.CENTER); stuAge = new JLabel("年龄", JLabel.CENTER); stuSex = new JLabel("性别", JLabel.CENTER); stuDepart = new JLabel("系别", JLabel.CENTER); id = new JTextField(15); id.setText(sm.getValueAt(idValue, 0).toString()); id.setEditable(false); name = new JTextField(15); name.setText(sm.getValueAt(idValue, 1).toString()); age = new JTextField(15); age.setText(sm.getValueAt(idValue, 2).toString()); sex = new JTextField(15); sex.setText(sm.getValueAt(idValue, 3).toString()); depart = new JTextField(15); depart.setText(sm.getValueAt(idValue, 4).toString()); top.add(stuID); top.add(stuName); top.add(stuAge); top.add(stuSex); top.add(stuDepart); left.add(id); left.add(name); left.add(age); left.add(sex); left.add(depart); bottom = new JPanel(); save = new JButton("保存"); save.addActionListener(this); cancel = new JButton("取消"); cancel.addActionListener(this); bottom.add(save); bottom.add(cancel); this.add(top, BorderLayout.CENTER); this.add(left, BorderLayout.EAST); this.add(bottom, BorderLayout.SOUTH); // 设置窗体属性 this.setSize(230, 200); this.setLocation(1000, 300); this.setVisible(true); } @Override public void actionPerformed(ActionEvent e) { // TODO Auto-generated method stub if (e.getSource() == save) { String values[] = { name.getText().trim(), age.getText().trim(), sex.getText().trim(), depart.getText().trim(), id.getText().trim() }; String sql = "update stus set stuName=?,stuAge=?,stuSex=?,stuDepart=? where stuID=?"; sm = new StuModel(); sm.updStu(sql, values); this.dispose(); } else if(e.getSource()==cancel){ this.dispose(); } } } /** * 功能:数据库操作模型 */ package com.sutmanage.version2; import java.sql.*; import java.util.Vector; public class SqlHelper { public static final String DRIVER = "com.mysql.jdbc.Driver"; public static final String DBURL = "jdbc:mysql://127.0.0.1:3306/StuManage"; public static final String DBUSRER = "root"; public static final String DBPWD = "123456"; // 连接数据库的组件 Connection con; java.sql.PreparedStatement ps; ResultSet rs; // 构造函数,在调用SqlHelper类时,就自动建立连接 public SqlHelper() { // 建立数据库连接 try { Class.forName(DRIVER); } catch (ClassNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } try { con = DriverManager.getConnection(DBURL, DBUSRER, DBPWD); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } // 增.删.改 public boolean updSQL(String sql, String[] args) { boolean b = true; try { ps = con.prepareStatement(sql); for (int i = 0; i < args.length; i++) { ps.setString((i + 1), args[i]); } ps.executeUpdate(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } this.close(); return b; } // 查询 public ResultSet query(String sql, String[] args) { try { ps = con.prepareStatement(sql); if (args == null) { rs = ps.executeQuery(); } else { for (int i = 0; i < args.length; i++) { ps.setString((i + 1), args[i]); } rs = ps.executeQuery(); } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } return rs; } // 关闭资源 public void close() { try { if (rs != null) rs.close(); if (ps != null) ps.close(); if (con != null) con.close(); } catch (SQLException e) { e.printStackTrace(); // TODO: handle exception } } }
本文档为【学生管理系统-韩顺平java】,请使用软件OFFICE或WPS软件打开。作品中的文字与图均可以修改和编辑, 图片更改请在作品中右键图片并更换,文字修改请直接点击文字进行修改,也可以新增和删除文档中的内容。
该文档来自用户分享,如有侵权行为请发邮件ishare@vip.sina.com联系网站客服,我们会及时删除。
[版权声明] 本站所有资料为用户分享产生,若发现您的权利被侵害,请联系客服邮件isharekefu@iask.cn,我们尽快处理。
本作品所展示的图片、画像、字体、音乐的版权可能需版权方额外授权,请谨慎使用。
网站提供的党政主题相关内容(国旗、国徽、党徽..)目的在于配合国家政策宣传,仅限个人学习分享使用,禁止用于任何广告和商用目的。
下载需要: 免费 已有0 人下载
最新资料
资料动态
专题动态
is_348501
暂无简介~
格式:doc
大小:45KB
软件:Word
页数:0
分类:生活休闲
上传时间:2017-10-07
浏览量:14