收藏 分销(赏)

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

上传人:天**** 文档编号:3244620 上传时间:2024-06-26 格式:DOC 页数:26 大小:43.04KB
下载 相关 举报
学生管理系统韩顺平java.doc_第1页
第1页 / 共26页
学生管理系统韩顺平java.doc_第2页
第2页 / 共26页
学生管理系统韩顺平java.doc_第3页
第3页 / 共26页
学生管理系统韩顺平java.doc_第4页
第4页 / 共26页
学生管理系统韩顺平java.doc_第5页
第5页 / 共26页
点击查看更多>>
资源描述

1、/* * 功能:简易学生管理系统 * 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.Pr

2、eparedStatement;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=nu

3、ll;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.setSelectionBackg

4、round(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,Border

5、Layout.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.setDefaultCl

6、oseOperation(JFrame.EXIT_ON_CLOSE);this.setVisible(true);public static void main(String args) / TODO Auto-generated method stubnew StuManageSystemVersion1();/更新数据模型public void updStuModel()sm=new StuModel();sm.queryStu(select * from stus, null);jt.setModel(sm);Overridepublic void actionPerformed(Act

7、ionEvent e) / TODO Auto-generated method stubif(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.setT

8、ext();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.get

9、SelectedRow()=-1)JOptionPane.showMessageDialog(this, 请先选择一行数据);return;elseint 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

10、.getSelectedRow()=-1)JOptionPane.showMessageDialog(this, 请先选择一行数据);return;elseint 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

11、 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 r

12、ow;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

13、.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 exceptionsh.close();/ 增.删.改操作public boolean updStu(String sql, String args) sh = new SqlHelper();return sh.updSQL(sql, args);Override/ 得到共有多少列

14、public int getColumnCount() / TODO Auto-generated method stubreturn this.cloumn.size();Override/ 得到共有多少行public int getRowCount() / TODO Auto-generated method stubreturn this.row.size();Override/ 得到某行某列旳值public Object getValueAt(int rowIndex, int columnIndex) / TODO Auto-generated method stubreturn t

15、his.row.get(rowIndex).get(columnIndex);Overridepublic String getColumnName(int index) / TODO Auto-generated method stubreturn 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.ActionLi

16、stener;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

17、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

18、= 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);l

19、eft.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

20、.setSize(230, 200);this.setLocation(1000, 300);this.setVisible(true);Overridepublic void actionPerformed(ActionEvent e) / TODO Auto-generated method stubif (e.getSource() = save) String values = id.getText().trim(), name.getText().trim(),age.getText().trim(), sex.getText().trim(),depart.getText().tr

21、im() ;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.A

22、ctionListener;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,

23、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(ne

24、w 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.setEdi

25、table(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(idValu

26、e, 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(thi

27、s);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);Overridepublic void actionPerformed(ActionEvent e) / TODO Auto-generated m

28、ethod stubif (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.dispos

29、e();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 s

30、tatic 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 blocke.prin

31、tStackTrace();try con = DriverManager.getConnection(DBURL, DBUSRER, DBPWD); catch (SQLException e) / TODO Auto-generated catch blocke.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.setS

32、tring(i + 1), argsi);ps.executeUpdate(); catch (SQLException e) / TODO Auto-generated catch blocke.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.lengt

33、h; i+) ps.setString(i + 1), argsi);rs = ps.executeQuery(); catch (SQLException e) / TODO Auto-generated catch blocke.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

展开阅读全文
部分上传会员的收益排行 01、路***(¥15400+),02、曲****(¥15300+),
03、wei****016(¥13200+),04、大***流(¥12600+),
05、Fis****915(¥4200+),06、h****i(¥4100+),
07、Q**(¥3400+),08、自******点(¥2400+),
09、h*****x(¥1400+),10、c****e(¥1100+),
11、be*****ha(¥800+),12、13********8(¥800+)。
相似文档                                   自信AI助手自信AI助手
搜索标签

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

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

关于我们      便捷服务       自信AI       AI导航        获赠5币

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

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

gongan.png浙公网安备33021202000488号   

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

关注我们 :gzh.png    weibo.png    LOFTER.png 

客服