收藏 分销(赏)

学生管理系统韩顺平java.doc

上传人:天**** 文档编号:3244620 上传时间:2024-06-26 格式:DOC 页数:26 大小:43.04KB 下载积分:10 金币
下载 相关 举报
学生管理系统韩顺平java.doc_第1页
第1页 / 共26页
学生管理系统韩顺平java.doc_第2页
第2页 / 共26页


点击查看更多>>
资源描述
/** * 功能:简易学生管理系统 * 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表旳模型 * */ 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<String> cloumn; Vector<Vector> row; SqlHelper sh; // 查询数据 public void queryStu(String sql, String[] args) { // 中间旳显示组件 cloumn = new Vector<String>(); cloumn.add("学生ID"); cloumn.add("姓名"); cloumn.add("年龄"); cloumn.add("性别"); cloumn.add("系别"); row = new Vector<Vector>(); sh = new SqlHelper(); rs = sh.query(sql, args); try { while (rs.next()) { Vector<Object> hang = new Vector<Object>(); 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 } } }
展开阅读全文

开通  VIP会员、SVIP会员  优惠大
下载10份以上建议开通VIP会员
下载20份以上建议开通SVIP会员


开通VIP      成为共赢上传

当前位置:首页 > 通信科技 > 开发语言

移动网页_全站_页脚广告1

关于我们      便捷服务       自信AI       AI导航        抽奖活动

©2010-2025 宁波自信网络信息技术有限公司  版权所有

客服电话:4009-655-100  投诉/维权电话:18658249818

gongan.png浙公网安备33021202000488号   

icp.png浙ICP备2021020529号-1  |  浙B2-20240490  

关注我们 :微信公众号    抖音    微博    LOFTER 

客服