您好,登錄后才能下訂單哦!
這篇文章主要講解了“怎么用java實現學生信息管理系統”,文中的講解內容簡單清晰,易于學習與理解,下面請大家跟著小編的思路慢慢深入,一起來研究和學習“怎么用java實現學生信息管理系統”吧!
本文實例為大家分享了java學生信息管理系統源碼的具體代碼,供大家參考,具體內容如下
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(); } } }
主界面
添加學生信息界面
修改學生信息界面
感謝各位的閱讀,以上就是“怎么用java實現學生信息管理系統”的內容了,經過本文的學習后,相信大家對怎么用java實現學生信息管理系統這一問題有了更深刻的體會,具體使用情況還需要大家實踐驗證。這里是億速云,小編將為大家推送更多相關知識點的文章,歡迎關注!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。