1、学生管理系统韩顺平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.
2、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; JTextFiel
3、d 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=
4、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(dele
5、te); 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().he
6、ight; 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 s
7、tus, 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 Stu
8、s; 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.Y
9、ES_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=?;
10、 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
11、(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
12、.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(系
13、别); 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 excep
14、tion 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 m
15、ethod 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(in
16、dex); 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
17、, 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);
18、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 JTextFi
19、eld(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 JButto
20、n(取消); 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 actionPe
21、rformed(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, va
22、lues); 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.
23、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, cance
24、l; 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
25、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
26、(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
27、(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); th
28、is.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.getTex
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1