java学生信息管理系统源码 java+SQL server2008学生信息管理系统源码
蔡金平 人气:0想了解java+SQL server2008学生信息管理系统源码的相关内容吗,蔡金平在本文为您仔细讲解java学生信息管理系统源码的相关知识和一些Code实例,欢迎阅读和指正,我们先划重点:java,SQL,server2008,学生信息管理系统,下面大家一起来学习吧。
1、StudetManage类(主界面)
package com.sms3; import java.awt.*; import javax.swing.*; import java.awt.event.*; public class StudentManage extends JFrame implements ActionListener { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub new StudentManage(); } //========面板控件 private JLabel queryLab = null; private JTextField queryTxt = null; private JButton queryBtn = null; private JButton allBtn = null; private JTable resultTb = null; private JScrollPane jsp = null; private JButton addBtn = null; private JButton deleteBtn = null; private JButton updateBtn = null; private JPanel top = null; private JPanel bottom = null; //======== private StuModel sm = null; //构造函数 public StudentManage() { /***************************初始化面板控件***********************/ //========查询栏 queryLab = new JLabel("请输入姓名:"); queryTxt = new JTextField(10); queryBtn = new JButton("查询"); allBtn = new JButton("全部"); //......添加查询栏监听 queryBtn.addActionListener(this); queryBtn.setActionCommand("query"); allBtn.addActionListener(this); allBtn.setActionCommand("all"); //========增删改栏 addBtn = new JButton("添加"); deleteBtn = new JButton("删除"); updateBtn = new JButton("修改"); //......添加增删改栏监听 addBtn.addActionListener(this); addBtn.setActionCommand("add"); deleteBtn.addActionListener(this); deleteBtn.setActionCommand("delete"); updateBtn.addActionListener(this); updateBtn.setActionCommand("update"); //========创建窗口整体布局 //......顶层查询栏 top = new JPanel(); top.add(queryLab); top.add(queryTxt); top.add(queryBtn); top.add(allBtn); //......底层增删改栏 bottom = new JPanel(); bottom.add(addBtn); bottom.add(deleteBtn); bottom.add(updateBtn); //......中间层显示栏 sm = new StuModel(); String sql = "select * from stu"; sm.queryStu(sql, null); resultTb = new JTable(sm); jsp = new JScrollPane(resultTb); //......构建整体布局 this.add(top,BorderLayout.NORTH); this.add(jsp,BorderLayout.CENTER); this.add(bottom,BorderLayout.SOUTH); //========设置窗口属性 this.setSize(400, 300); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.setVisible(true); this.setResizable(false); } //监听 @Override public void actionPerformed(ActionEvent e) { // TODO Auto-generated method stub if(e.getActionCommand().equals("query")) { /*********************查询***********************/ //========获取输入学生的姓名 String name = queryTxt.getText().trim(); if(name.length() != 0) { //========姓名输入有效时,执行查询 //......定义参数 String sql = "select * from stu where stuName=?"; String []paras = {name}; //......更新模型 jtableUpdate(sql, paras); } else { //========姓名为空时,设置提醒 JOptionPane.showMessageDialog(this, "姓名输入不能为空"); } } else if(e.getActionCommand().equals("add")) { /*********************添加***********************/ new StuAddDialog(this, "添加学生信息", true); String sql = "select * from stu"; jtableUpdate(sql, null); } else if(e.getActionCommand().equals("all")) { /*********************全部显示***********************/ String sql = "select * from stu"; jtableUpdate(sql, null); } else if(e.getActionCommand().equals("delete")) { /*********************删除***********************/ //========获取选择行号 int rowNum = this.resultTb.getSelectedRow(); if(rowNum == -1) { JOptionPane.showMessageDialog(this, "请选择一行"); return ; } //========获取学生ID号 String stuId = (String)sm.getValueAt(rowNum, 0); //========删除学生 String sql = "delete from stu where stuId=?"; String []paras = {stuId}; StuModel tmp = new StuModel(); tmp.cudStu(sql, paras); //========更新模型 sql = "select * from stu"; jtableUpdate(sql, null); } else if(e.getActionCommand().equals("update")) { /*********************修改***********************/ //========获取选择行号 int rowNum = this.resultTb.getSelectedRow(); if(rowNum == -1) { JOptionPane.showMessageDialog(this, "请选择一行"); return ; } new StuUpdateDialog(this, "修改学生信息", true, sm, rowNum); String sql = "select * from stu"; jtableUpdate(sql, null); } } //========更新JTable内数据 public void jtableUpdate(String sql, String[] paras) { //......创建模型 sm = new StuModel(); sm.queryStu(sql, paras); //......更新显示 resultTb.setModel(sm); } }
2、StuModel类(学生数据库模型)
package com.sms3; import java.sql.ResultSet; import java.util.Vector; import javax.swing.table.AbstractTableModel; public class StuModel extends AbstractTableModel{ private Vector columnNames; private Vector rowDates; // public StuModel() { String sql = "select * from stu"; String []paras = {}; } //========增删改学生 public boolean cudStu(String sql, String []paras) { return new SqlHelper().cudExecute(sql, paras); } //========查询学生 public void queryStu(String sql, String []paras) { SqlHelper sqlHelper = null; //========初始化JTable信息 columnNames = new Vector(); rowDates = new Vector(); columnNames.add("学号"); columnNames.add("名字"); columnNames.add("性别"); columnNames.add("年龄"); columnNames.add("籍贯"); columnNames.add("系别"); try { sqlHelper = new SqlHelper(); ResultSet rs = sqlHelper.queryExecute(sql, paras); while(rs.next()) { Vector row = new Vector(); row.add(rs.getString(1)); row.add(rs.getString(2)); row.add(rs.getString(3)); row.add(rs.getString(4)); row.add(rs.getString(5)); row.add(rs.getString(6)); rowDates.add(row); } } catch (Exception e) { // TODO: handle exception } finally { sqlHelper.close(); } } @Override public int getColumnCount() { // TODO Auto-generated method stub return this.columnNames.size(); } @Override public int getRowCount() { // TODO Auto-generated method stub return this.rowDates.size(); } @Override public Object getValueAt(int row, int col) { // TODO Auto-generated method stub if(!rowDates.isEmpty()) return ((Vector)this.rowDates.get(row)).get(col); else return null; } @Override public String getColumnName(int column) { // TODO Auto-generated method stub return (String)this.columnNames.get(column); } }
3、StuAddDialog类(添加学生信息子界面)
package com.sms3; import java.awt.BorderLayout; import java.awt.Dialog; import java.awt.Frame; import java.awt.GridLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.*; public class StuAddDialog extends JDialog implements ActionListener{ //=========面板控件 //......左侧标题栏 private JLabel idLab,nameLab,sexLab,ageLab,jgLab,deptLab; //......右侧信息选择填写栏 private JTextField idTxt,nameTxt,sexTxt,ageTxt,jgTxt,deptTxt; //......添加和取消按钮 private JButton addBtn,cancelBtn; //......布局控件 private JPanel left,center,bottom; //构造函数 public StuAddDialog(Frame owner, String title, boolean modal) { //========重写父类方法 super(owner, title, modal); //========左侧标签栏 idLab = new JLabel("学号: "); nameLab = new JLabel("姓名: "); sexLab = new JLabel("性别: "); ageLab = new JLabel("年龄: "); jgLab = new JLabel("籍贯: "); deptLab = new JLabel("系别: "); //========右侧信息填写栏 idTxt = new JTextField(); nameTxt = new JTextField(); sexTxt = new JTextField(); ageTxt = new JTextField(); jgTxt = new JTextField(); deptTxt = new JTextField(); //========添加和取消按钮 addBtn = new JButton("添加"); cancelBtn = new JButton("取消"); //......添加监听 addBtn.addActionListener(this); addBtn.setActionCommand("add"); cancelBtn.addActionListener(this); cancelBtn.setActionCommand("cancel"); //========创建布局 //......创建左边栏 left = new JPanel(); left.setLayout(new GridLayout(6, 1)); left.add(idLab); left.add(nameLab); left.add(sexLab); left.add(ageLab); left.add(jgLab); left.add(deptLab); //......创建右边栏 center = new JPanel(); center.setLayout(new GridLayout(6, 1)); center.add(idTxt); center.add(nameTxt); center.add(sexTxt); center.add(ageTxt); center.add(jgTxt); center.add(deptTxt); //========底层添加和取消按钮 bottom = new JPanel(); bottom.add(addBtn); bottom.add(cancelBtn); //========整体布局 this.add(left,BorderLayout.WEST); this.add(center,BorderLayout.CENTER); this.add(bottom,BorderLayout.SOUTH); //========设置窗口属性 this.setSize(300, 250); this.setResizable(false); this.setVisible(true); } @Override public void actionPerformed(ActionEvent e) { // TODO Auto-generated method stub if(e.getActionCommand().equals("add")) { /***********************添加学生信息**************************/ StuModel tmp = new StuModel(); String sql = "insert into stu values(?,?,?,?,?,?)"; String []paras = {idTxt.getText(),nameTxt.getText(),sexTxt.getText(), ageTxt.getText(),jgTxt.getText(),deptTxt.getText()}; if(!tmp.cudStu(sql, paras)) JOptionPane.showMessageDialog(this, "添加学生信息失败"); //========关闭窗口 this.dispose(); } else if(e.getActionCommand().equals("cancel")) { //========关闭窗口 this.dispose(); } } }
4、StuUpdateDialog类(修改学生信息子界面)
package com.sms3; import java.awt.BorderLayout; import java.awt.Frame; 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.JLabel; import javax.swing.JOptionPane; import javax.swing.JPanel; import javax.swing.JTextField; import javax.swing.table.AbstractTableModel; public class StuUpdateDialog extends JDialog implements ActionListener{ //=========面板控件 //......左侧标题栏 private JLabel idLab,nameLab,sexLab,ageLab,jgLab,deptLab; //......右侧信息选择填写栏 private JTextField idTxt,nameTxt,sexTxt,ageTxt,jgTxt,deptTxt; //......添加和取消按钮 private JButton addBtn,cancelBtn; //......布局控件 private JPanel left,center,bottom; //构造函数 public StuUpdateDialog(Frame owner, String title, boolean modal, StuModel sm, int rowNum) { //========重写父类方法 super(owner, title, modal); //========左侧标签栏 idLab = new JLabel("学号: "); nameLab = new JLabel("姓名: "); sexLab = new JLabel("性别: "); ageLab = new JLabel("年龄: "); jgLab = new JLabel("籍贯: "); deptLab = new JLabel("系别: "); //========右侧信息填写栏 idTxt = new JTextField(); idTxt.setText((String)sm.getValueAt(rowNum, 0)); idTxt.setEditable(false); nameTxt = new JTextField(); nameTxt.setText((String)sm.getValueAt(rowNum, 1)); sexTxt = new JTextField(); sexTxt.setText((String)sm.getValueAt(rowNum, 2)); ageTxt = new JTextField(); ageTxt.setText((String)sm.getValueAt(rowNum, 3)); jgTxt = new JTextField(); jgTxt.setText((String)sm.getValueAt(rowNum, 4)); deptTxt = new JTextField(); deptTxt.setText((String)sm.getValueAt(rowNum, 5)); //========添加和取消按钮 addBtn = new JButton("修改"); cancelBtn = new JButton("取消"); //......添加监听 addBtn.addActionListener(this); addBtn.setActionCommand("update"); cancelBtn.addActionListener(this); cancelBtn.setActionCommand("cancel"); //========创建布局 //......创建左边栏 left = new JPanel(); left.setLayout(new GridLayout(6, 1)); left.add(idLab); left.add(nameLab); left.add(sexLab); left.add(ageLab); left.add(jgLab); left.add(deptLab); //......创建右边栏 center = new JPanel(); center.setLayout(new GridLayout(6, 1)); center.add(idTxt); center.add(nameTxt); center.add(sexTxt); center.add(ageTxt); center.add(jgTxt); center.add(deptTxt); //========底层添加和取消按钮 bottom = new JPanel(); bottom.add(addBtn); bottom.add(cancelBtn); //========整体布局 this.add(left,BorderLayout.WEST); this.add(center,BorderLayout.CENTER); this.add(bottom,BorderLayout.SOUTH); //========设置窗口属性 this.setSize(300, 250); this.setResizable(false); this.setVisible(true); } @Override public void actionPerformed(ActionEvent e) { // TODO Auto-generated method stub if(e.getActionCommand().equals("update")) { /***********************修改学生信息**************************/ StuModel tmp = new StuModel(); String sql = "update stu set stuName=?,stuSex=?,stuAge=?,stuJg=?,stuDept=? where stuId=?"; String []paras = {nameTxt.getText(),sexTxt.getText(),ageTxt.getText(), jgTxt.getText(),deptTxt.getText(),idTxt.getText()}; if(!tmp.cudStu(sql, paras)) JOptionPane.showMessageDialog(this, "修改学生信息失败"); //========关闭窗口 this.dispose(); } else if(e.getActionCommand().equals("cancel")) { //========关闭窗口 this.dispose(); } } }
5、SqlHelper类(最底层数据库类)
package com.sms3; import java.sql.*; public class SqlHelper { //========数据库 private Connection ct = null; private PreparedStatement ps = null; private ResultSet rs = null; private String driver = "com.microsoft.sqlserver.jdbc.SQLServerDriver"; private String url = "jdbc:sqlserver://127.0.0.1:1433;database=studentMan"; private String user = "sa"; private String passwd = "****"; //========查询 public ResultSet queryExecute(String sql, String []paras) { try { //========1、加载驱动 Class.forName(driver); //========2、连接 ct = DriverManager.getConnection(url, user, passwd); //========3、创建PreparedStatement ps = ct.prepareStatement(sql); //========4、给问号赋值 if(paras != null) { for(int i = 0; i < paras.length; i++) { ps.setString(i + 1, paras[i]); } } //========5、执行 rs = ps.executeQuery(); } catch (Exception e) { // TODO: handle exception e.printStackTrace(); } finally { //this.close(); } //========返回值 return rs; } //========增删改 public boolean cudExecute(String sql, String []paras) { boolean b = true; try { //========1、加载驱动 Class.forName(driver); //========2、连接 ct = DriverManager.getConnection(url, user, passwd); //========3、创建PreparedStatement ps = ct.prepareStatement(sql); //========4、给问号赋值 for(int i = 0; i < paras.length; i++) { ps.setString(i + 1, paras[i]); } //========5、执行 if(ps.executeUpdate() != 1) b = false; } catch (Exception e) { // TODO: handle exception b = false; e.printStackTrace(); } finally { this.close(); } //========返回值 return b; } //========关闭资源 public void close() { try { if(rs!=null) rs.close(); if(ps!=null) ps.close(); if(ct!=null) ct.close(); } catch (Exception e2) { // TODO: handle exception e2.printStackTrace(); } } }
主界面
添加学生信息界面
修改学生信息界面
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
加载全部内容