查看: 1832|回复: 0

[Java代码] java+SQL server2008学生信息管理系统源码

发表于 2018-3-3 12:06:54

本文实例为大家分享了java学生信息管理系统源码的具体代码,供大家参考,具体内容如下

1、StudetManage类(主界面)

  1. package com.sms3;
  2. import java.awt.*;
  3. import javax.swing.*;
  4. import java.awt.event.*;
  5. public class StudentManage extends JFrame implements ActionListener
  6. {
  7. /**
  8. * @param args
  9. */
  10. public static void main(String[] args)
  11. {
  12. // TODO Auto-generated method stub
  13. new StudentManage();
  14. }
  15. //========面板控件
  16. private JLabel queryLab = null;
  17. private JTextField queryTxt = null;
  18. private JButton queryBtn = null;
  19. private JButton allBtn = null;
  20. private JTable resultTb = null;
  21. private JScrollPane jsp = null;
  22. private JButton addBtn = null;
  23. private JButton deleteBtn = null;
  24. private JButton updateBtn = null;
  25. private JPanel top = null;
  26. private JPanel bottom = null;
  27. //========
  28. private StuModel sm = null;
  29. //构造函数
  30. public StudentManage()
  31. {
  32. /***************************初始化面板控件***********************/
  33. //========查询栏
  34. queryLab = new JLabel("请输入姓名:");
  35. queryTxt = new JTextField(10);
  36. queryBtn = new JButton("查询");
  37. allBtn = new JButton("全部");
  38. //......添加查询栏监听
  39. queryBtn.addActionListener(this);
  40. queryBtn.setActionCommand("query");
  41. allBtn.addActionListener(this);
  42. allBtn.setActionCommand("all");
  43. //========增删改栏
  44. addBtn = new JButton("添加");
  45. deleteBtn = new JButton("删除");
  46. updateBtn = new JButton("修改");
  47. //......添加增删改栏监听
  48. addBtn.addActionListener(this);
  49. addBtn.setActionCommand("add");
  50. deleteBtn.addActionListener(this);
  51. deleteBtn.setActionCommand("delete");
  52. updateBtn.addActionListener(this);
  53. updateBtn.setActionCommand("update");
  54. //========创建窗口整体布局
  55. //......顶层查询栏
  56. top = new JPanel();
  57. top.add(queryLab);
  58. top.add(queryTxt);
  59. top.add(queryBtn);
  60. top.add(allBtn);
  61. //......底层增删改栏
  62. bottom = new JPanel();
  63. bottom.add(addBtn);
  64. bottom.add(deleteBtn);
  65. bottom.add(updateBtn);
  66. //......中间层显示栏
  67. sm = new StuModel();
  68. String sql = "select * from stu";
  69. sm.queryStu(sql, null);
  70. resultTb = new JTable(sm);
  71. jsp = new JScrollPane(resultTb);
  72. //......构建整体布局
  73. this.add(top,BorderLayout.NORTH);
  74. this.add(jsp,BorderLayout.CENTER);
  75. this.add(bottom,BorderLayout.SOUTH);
  76. //========设置窗口属性
  77. this.setSize(400, 300);
  78. this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  79. this.setVisible(true);
  80. this.setResizable(false);
  81. }
  82. //监听
  83. @Override
  84. public void actionPerformed(ActionEvent e)
  85. {
  86. // TODO Auto-generated method stub
  87. if(e.getActionCommand().equals("query")) {
  88. /*********************查询***********************/
  89. //========获取输入学生的姓名
  90. String name = queryTxt.getText().trim();
  91. if(name.length() != 0) {
  92. //========姓名输入有效时,执行查询
  93. //......定义参数
  94. String sql = "select * from stu where stuName=?";
  95. String []paras = {name};
  96. //......更新模型
  97. jtableUpdate(sql, paras);
  98. } else {
  99. //========姓名为空时,设置提醒
  100. JOptionPane.showMessageDialog(this, "姓名输入不能为空");
  101. }
  102. } else if(e.getActionCommand().equals("add")) {
  103. /*********************添加***********************/
  104. new StuAddDialog(this, "添加学生信息", true);
  105. String sql = "select * from stu";
  106. jtableUpdate(sql, null);
  107. } else if(e.getActionCommand().equals("all")) {
  108. /*********************全部显示***********************/
  109. String sql = "select * from stu";
  110. jtableUpdate(sql, null);
  111. } else if(e.getActionCommand().equals("delete")) {
  112. /*********************删除***********************/
  113. //========获取选择行号
  114. int rowNum = this.resultTb.getSelectedRow();
  115. if(rowNum == -1) {
  116. JOptionPane.showMessageDialog(this, "请选择一行");
  117. return ;
  118. }
  119. //========获取学生ID号
  120. String stuId = (String)sm.getValueAt(rowNum, 0);
  121. //========删除学生
  122. String sql = "delete from stu where stuId=?";
  123. String []paras = {stuId};
  124. StuModel tmp = new StuModel();
  125. tmp.cudStu(sql, paras);
  126. //========更新模型
  127. sql = "select * from stu";
  128. jtableUpdate(sql, null);
  129. } else if(e.getActionCommand().equals("update")) {
  130. /*********************修改***********************/
  131. //========获取选择行号
  132. int rowNum = this.resultTb.getSelectedRow();
  133. if(rowNum == -1) {
  134. JOptionPane.showMessageDialog(this, "请选择一行");
  135. return ;
  136. }
  137. new StuUpdateDialog(this, "修改学生信息", true, sm, rowNum);
  138. String sql = "select * from stu";
  139. jtableUpdate(sql, null);
  140. }
  141. }
  142. //========更新JTable内数据
  143. public void jtableUpdate(String sql, String[] paras)
  144. {
  145. //......创建模型
  146. sm = new StuModel();
  147. sm.queryStu(sql, paras);
  148. //......更新显示
  149. resultTb.setModel(sm);
  150. }
  151. }
复制代码

2、StuModel类(学生数据库模型)

  1. package com.sms3;
  2. import java.sql.ResultSet;
  3. import java.util.Vector;
  4. import javax.swing.table.AbstractTableModel;
  5. public class StuModel extends AbstractTableModel{
  6. private Vector columnNames;
  7. private Vector rowDates;
  8. //
  9. public StuModel()
  10. {
  11. String sql = "select * from stu";
  12. String []paras = {};
  13. }
  14. //========增删改学生
  15. public boolean cudStu(String sql, String []paras)
  16. {
  17. return new SqlHelper().cudExecute(sql, paras);
  18. }
  19. //========查询学生
  20. public void queryStu(String sql, String []paras)
  21. {
  22. SqlHelper sqlHelper = null;
  23. //========初始化JTable信息
  24. columnNames = new Vector();
  25. rowDates = new Vector();
  26. columnNames.add("学号"); columnNames.add("名字");
  27. columnNames.add("性别"); columnNames.add("年龄");
  28. columnNames.add("籍贯"); columnNames.add("系别");
  29. try {
  30. sqlHelper = new SqlHelper();
  31. ResultSet rs = sqlHelper.queryExecute(sql, paras);
  32. while(rs.next()) {
  33. Vector row = new Vector();
  34. row.add(rs.getString(1));
  35. row.add(rs.getString(2));
  36. row.add(rs.getString(3));
  37. row.add(rs.getString(4));
  38. row.add(rs.getString(5));
  39. row.add(rs.getString(6));
  40. rowDates.add(row);
  41. }
  42. } catch (Exception e) {
  43. // TODO: handle exception
  44. } finally {
  45. sqlHelper.close();
  46. }
  47. }
  48. @Override
  49. public int getColumnCount() {
  50. // TODO Auto-generated method stub
  51. return this.columnNames.size();
  52. }
  53. @Override
  54. public int getRowCount() {
  55. // TODO Auto-generated method stub
  56. return this.rowDates.size();
  57. }
  58. @Override
  59. public Object getValueAt(int row, int col) {
  60. // TODO Auto-generated method stub
  61. if(!rowDates.isEmpty())
  62. return ((Vector)this.rowDates.get(row)).get(col);
  63. else
  64. return null;
  65. }
  66. @Override
  67. public String getColumnName(int column) {
  68. // TODO Auto-generated method stub
  69. return (String)this.columnNames.get(column);
  70. }
  71. }
复制代码

3、StuAddDialog类(添加学生信息子界面)

  1. package com.sms3;
  2. import java.awt.BorderLayout;
  3. import java.awt.Dialog;
  4. import java.awt.Frame;
  5. import java.awt.GridLayout;
  6. import java.awt.event.ActionEvent;
  7. import java.awt.event.ActionListener;
  8. import javax.swing.*;
  9. public class StuAddDialog extends JDialog implements ActionListener{
  10. //=========面板控件
  11. //......左侧标题栏
  12. private JLabel idLab,nameLab,sexLab,ageLab,jgLab,deptLab;
  13. //......右侧信息选择填写栏
  14. private JTextField idTxt,nameTxt,sexTxt,ageTxt,jgTxt,deptTxt;
  15. //......添加和取消按钮
  16. private JButton addBtn,cancelBtn;
  17. //......布局控件
  18. private JPanel left,center,bottom;
  19. //构造函数
  20. public StuAddDialog(Frame owner, String title, boolean modal)
  21. {
  22. //========重写父类方法
  23. super(owner, title, modal);
  24. //========左侧标签栏
  25. idLab = new JLabel("学号: ");
  26. nameLab = new JLabel("姓名: ");
  27. sexLab = new JLabel("性别: ");
  28. ageLab = new JLabel("年龄: ");
  29. jgLab = new JLabel("籍贯: ");
  30. deptLab = new JLabel("系别: ");
  31. //========右侧信息填写栏
  32. idTxt = new JTextField();
  33. nameTxt = new JTextField();
  34. sexTxt = new JTextField();
  35. ageTxt = new JTextField();
  36. jgTxt = new JTextField();
  37. deptTxt = new JTextField();
  38. //========添加和取消按钮
  39. addBtn = new JButton("添加");
  40. cancelBtn = new JButton("取消");
  41. //......添加监听
  42. addBtn.addActionListener(this);
  43. addBtn.setActionCommand("add");
  44. cancelBtn.addActionListener(this);
  45. cancelBtn.setActionCommand("cancel");
  46. //========创建布局
  47. //......创建左边栏
  48. left = new JPanel();
  49. left.setLayout(new GridLayout(6, 1));
  50. left.add(idLab); left.add(nameLab);
  51. left.add(sexLab); left.add(ageLab);
  52. left.add(jgLab); left.add(deptLab);
  53. //......创建右边栏
  54. center = new JPanel();
  55. center.setLayout(new GridLayout(6, 1));
  56. center.add(idTxt); center.add(nameTxt);
  57. center.add(sexTxt); center.add(ageTxt);
  58. center.add(jgTxt); center.add(deptTxt);
  59. //========底层添加和取消按钮
  60. bottom = new JPanel();
  61. bottom.add(addBtn);
  62. bottom.add(cancelBtn);
  63. //========整体布局
  64. this.add(left,BorderLayout.WEST);
  65. this.add(center,BorderLayout.CENTER);
  66. this.add(bottom,BorderLayout.SOUTH);
  67. //========设置窗口属性
  68. this.setSize(300, 250);
  69. this.setResizable(false);
  70. this.setVisible(true);
  71. }
  72. @Override
  73. public void actionPerformed(ActionEvent e)
  74. {
  75. // TODO Auto-generated method stub
  76. if(e.getActionCommand().equals("add")) {
  77. /***********************添加学生信息**************************/
  78. StuModel tmp = new StuModel();
  79. String sql = "insert into stu values(?,?,?,?,?,?)";
  80. String []paras = {idTxt.getText(),nameTxt.getText(),sexTxt.getText(),
  81. ageTxt.getText(),jgTxt.getText(),deptTxt.getText()};
  82. if(!tmp.cudStu(sql, paras))
  83. JOptionPane.showMessageDialog(this, "添加学生信息失败");
  84. //========关闭窗口
  85. this.dispose();
  86. } else if(e.getActionCommand().equals("cancel")) {
  87. //========关闭窗口
  88. this.dispose();
  89. }
  90. }
  91. }
复制代码

4、StuUpdateDialog类(修改学生信息子界面)

  1. package com.sms3;
  2. import java.awt.BorderLayout;
  3. import java.awt.Frame;
  4. import java.awt.GridLayout;
  5. import java.awt.event.ActionEvent;
  6. import java.awt.event.ActionListener;
  7. import javax.swing.JButton;
  8. import javax.swing.JDialog;
  9. import javax.swing.JLabel;
  10. import javax.swing.JOptionPane;
  11. import javax.swing.JPanel;
  12. import javax.swing.JTextField;
  13. import javax.swing.table.AbstractTableModel;
  14. public class StuUpdateDialog extends JDialog implements ActionListener{
  15. //=========面板控件
  16. //......左侧标题栏
  17. private JLabel idLab,nameLab,sexLab,ageLab,jgLab,deptLab;
  18. //......右侧信息选择填写栏
  19. private JTextField idTxt,nameTxt,sexTxt,ageTxt,jgTxt,deptTxt;
  20. //......添加和取消按钮
  21. private JButton addBtn,cancelBtn;
  22. //......布局控件
  23. private JPanel left,center,bottom;
  24. //构造函数
  25. public StuUpdateDialog(Frame owner, String title, boolean modal, StuModel sm, int rowNum)
  26. {
  27. //========重写父类方法
  28. super(owner, title, modal);
  29. //========左侧标签栏
  30. idLab = new JLabel("学号: ");
  31. nameLab = new JLabel("姓名: ");
  32. sexLab = new JLabel("性别: ");
  33. ageLab = new JLabel("年龄: ");
  34. jgLab = new JLabel("籍贯: ");
  35. deptLab = new JLabel("系别: ");
  36. //========右侧信息填写栏
  37. idTxt = new JTextField();
  38. idTxt.setText((String)sm.getValueAt(rowNum, 0));
  39. idTxt.setEditable(false);
  40. nameTxt = new JTextField();
  41. nameTxt.setText((String)sm.getValueAt(rowNum, 1));
  42. sexTxt = new JTextField();
  43. sexTxt.setText((String)sm.getValueAt(rowNum, 2));
  44. ageTxt = new JTextField();
  45. ageTxt.setText((String)sm.getValueAt(rowNum, 3));
  46. jgTxt = new JTextField();
  47. jgTxt.setText((String)sm.getValueAt(rowNum, 4));
  48. deptTxt = new JTextField();
  49. deptTxt.setText((String)sm.getValueAt(rowNum, 5));
  50. //========添加和取消按钮
  51. addBtn = new JButton("修改");
  52. cancelBtn = new JButton("取消");
  53. //......添加监听
  54. addBtn.addActionListener(this);
  55. addBtn.setActionCommand("update");
  56. cancelBtn.addActionListener(this);
  57. cancelBtn.setActionCommand("cancel");
  58. //========创建布局
  59. //......创建左边栏
  60. left = new JPanel();
  61. left.setLayout(new GridLayout(6, 1));
  62. left.add(idLab); left.add(nameLab);
  63. left.add(sexLab); left.add(ageLab);
  64. left.add(jgLab); left.add(deptLab);
  65. //......创建右边栏
  66. center = new JPanel();
  67. center.setLayout(new GridLayout(6, 1));
  68. center.add(idTxt); center.add(nameTxt);
  69. center.add(sexTxt); center.add(ageTxt);
  70. center.add(jgTxt); center.add(deptTxt);
  71. //========底层添加和取消按钮
  72. bottom = new JPanel();
  73. bottom.add(addBtn);
  74. bottom.add(cancelBtn);
  75. //========整体布局
  76. this.add(left,BorderLayout.WEST);
  77. this.add(center,BorderLayout.CENTER);
  78. this.add(bottom,BorderLayout.SOUTH);
  79. //========设置窗口属性
  80. this.setSize(300, 250);
  81. this.setResizable(false);
  82. this.setVisible(true);
  83. }
  84. @Override
  85. public void actionPerformed(ActionEvent e) {
  86. // TODO Auto-generated method stub
  87. if(e.getActionCommand().equals("update")) {
  88. /***********************修改学生信息**************************/
  89. StuModel tmp = new StuModel();
  90. String sql = "update stu set stuName=?,stuSex=?,stuAge=?,stuJg=?,stuDept=? where stuId=?";
  91. String []paras = {nameTxt.getText(),sexTxt.getText(),ageTxt.getText(),
  92. jgTxt.getText(),deptTxt.getText(),idTxt.getText()};
  93. if(!tmp.cudStu(sql, paras))
  94. JOptionPane.showMessageDialog(this, "修改学生信息失败");
  95. //========关闭窗口
  96. this.dispose();
  97. } else if(e.getActionCommand().equals("cancel")) {
  98. //========关闭窗口
  99. this.dispose();
  100. }
  101. }
  102. }
复制代码

5、SqlHelper类(最底层数据库类)

  1. package com.sms3;
  2. import java.sql.*;
  3. public class SqlHelper {
  4. //========数据库
  5. private Connection ct = null;
  6. private PreparedStatement ps = null;
  7. private ResultSet rs = null;
  8. private String driver = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
  9. private String url = "jdbc:sqlserver://127.0.0.1:1433;database=studentMan";
  10. private String user = "sa";
  11. private String passwd = "****";
  12. //========查询
  13. public ResultSet queryExecute(String sql, String []paras)
  14. {
  15. try {
  16. //========1、加载驱动
  17. Class.forName(driver);
  18. //========2、连接
  19. ct = DriverManager.getConnection(url, user, passwd);
  20. //========3、创建PreparedStatement
  21. ps = ct.prepareStatement(sql);
  22. //========4、给问号赋值
  23. if(paras != null) {
  24. for(int i = 0; i < paras.length; i++) {
  25. ps.setString(i + 1, paras[i]);
  26. }
  27. }
  28. //========5、执行
  29. rs = ps.executeQuery();
  30. } catch (Exception e) {
  31. // TODO: handle exception
  32. e.printStackTrace();
  33. } finally {
  34. //this.close();
  35. }
  36. //========返回值
  37. return rs;
  38. }
  39. //========增删改
  40. public boolean cudExecute(String sql, String []paras)
  41. {
  42. boolean b = true;
  43. try {
  44. //========1、加载驱动
  45. Class.forName(driver);
  46. //========2、连接
  47. ct = DriverManager.getConnection(url, user, passwd);
  48. //========3、创建PreparedStatement
  49. ps = ct.prepareStatement(sql);
  50. //========4、给问号赋值
  51. for(int i = 0; i < paras.length; i++) {
  52. ps.setString(i + 1, paras[i]);
  53. }
  54. //========5、执行
  55. if(ps.executeUpdate() != 1) b = false;
  56. } catch (Exception e) {
  57. // TODO: handle exception
  58. b = false;
  59. e.printStackTrace();
  60. } finally {
  61. this.close();
  62. }
  63. //========返回值
  64. return b;
  65. }
  66. //========关闭资源
  67. public void close()
  68. {
  69. try {
  70. if(rs!=null) rs.close();
  71. if(ps!=null) ps.close();
  72. if(ct!=null) ct.close();
  73. } catch (Exception e2) {
  74. // TODO: handle exception
  75. e2.printStackTrace();
  76. }
  77. }
  78. }
复制代码

主界面

添加学生信息界面

修改学生信息界面

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持程序员之家。



回复

使用道具 举报