The examples in this article share the specific code of the source code of the Java student information management system for your reference. The specific content is as follows
1.StudetManage class (main interface)
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(); } //======== Panel control 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; //Constructor public StudentManage() { /****************************** Initialize the panel control**********************/ //======== Query bar queryLab = new JLabel("Please enter a name:"); queryTxt = new JTextField(10); queryBtn = new JButton("Query"); allBtn = new JButton("All"); //......Add query bar to listen queryBtn.addActionListener(this); queryBtn.setActionCommand("query"); allBtn.addActionListener(this); allBtn.setActionCommand("all"); //=========== Add-and-deletion column addBtn = new JButton("add"); deleteBtn = new JButton("delete"); updateBtn = new JButton("modify"); //......Add add-and-deletion column listen addBtn.addActionListener(this); addBtn.setActionCommand("add"); deleteBtn.addActionListener(this); deleteBtn.setActionCommand("delete"); deleteBtn.addActionListener(this); deleteBtn.setActionCommand("delete"); updateBtn.addActionListener(this); updateBtn.setActionCommand("update"); //======== Create the overall layout of the window//...... Top-level query bar top = new JPanel(); top.add(queryLab); top.add(queryTxt); top.add(queryBtn); top.add(allBtn); //...... The bottom-level add-and-deletion column bottom = new JPanel(); bottom.add(addBtn); bottom.add(deleteBtn); bottom.add(updateBtn); //...... Middle-level display bar sm = new StuModel(); String sql = "select * from stu"; sm.queryStu(sql, null); resultTb = new JTable(sm); jsp = new JScrollPane(resultTb); //......Construct the overall layout this.add(top,BorderLayout.NORTH); this.add(jsp,BorderLayout.CENTER); this.add(bottom,BorderLayout.SOUTH); //=======Set the window property this.setSize(400, 300); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.setVisible(true); this.setResizable(false); } // Listen @Override public void actionPerformed(ActionEvent e) { // TODO Auto-generated method stub if(e.getActionCommand().equals("query")) { /***************************************/ //======= Get the name of the input student String name = queryTxt.getText().trim(); if(name.length() != 0) { //======== When the name input is valid, execute the query //......Define the parameter String sql = "select * from stu where stuName=?"; String []paras = {name}; //......Update the model jtableUpdate(sql, paras); } else { //========== When the name is empty, set the reminder JOptionPane.showMessageDialog(this, "Name input cannot be empty"); } } else if(e.getActionCommand().equals("add")) { /************************************/ new StuAddDialog(this, "Add student information", true); String sql = "select * from stu"; jtableUpdate(sql, null); } else if(e.getActionCommand().equals("all")) { /************************************ All shows *************************/ String sql = "select * from stu"; jtableUpdate(sql, null); } else if(e.getActionCommand().equals("delete")) { /************************************/ //======== Get the selection line number int rowNum = this.resultTb.getSelectedRow(); if(rowNum == -1) { JOptionPane.showMessageDialog(this, "Please select a row"); return ; } //======== Get the student ID number String stuId = (String)sm.getValueAt(rowNum, 0); //=========Delete student String sql = "delete from stu where stuId=?"; String []paras = {stuId}; StuModel tmp = new StuModel(); tmp.cudStu(sql, paras); //==========Update model sql = "select * from stu"; jtableUpdate(sql, null); } else if(e.getActionCommand().equals("update")) { /******************************Modify************************/ //========= Get the selection line number int rowNum = this.resultTb.getSelectedRow(); if(rowNum == -1) { JOptionPane.showMessageDialog(this, "Please select a row"); return ; } new StuUpdateDialog(this, "Modify student information", true, sm, rowNum); String sql = "select * from stu"; jtableUpdate(sql, null); } } //==========Update data in JTable public void jtableUpdate(String sql, String[] paras) { //...Create model sm = new StuModel(); sm.queryStu(sql, paras); //......Update the resultTb.setModel(sm); } }2. StuModel class (student database model)
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 = {}; } //======== Add and delete students public boolean cudStu(String sql, String []paras) { return new SqlHelper().cudExecute(sql, paras); } //========= Query the student public void queryStu(String sql, String []paras) { SqlHelper sqlHelper = null; //========= Initialize JTable information columnNames = new Vector(); rowDates = new Vector(); columnNames.add("Student number"); columnNames.add("Name"); columnNames.add("Gender"); columnNames.add("Age"); columnNames.add("home"); columnNames.add("part"); 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 class (add student information subinterface)
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{ //========= Panel control//......Left title bar private JLabel idLab,nameLab,sexLab,ageLab,jgLab,deptLab; //......Select fill in the information on the right to fill in the column private JTextField idTxt,nameTxt,sexTxt,ageTxt,jgTxt,deptTxt; //......Add and cancel button private JButton addBtn,cancelBtn; //......Layout control private JPanel left,center,bottom; //Constructor public StuAddDialog(Frame owner, String title, boolean modal) { //========Rewrite the parent class method super(owner, title, modal); //=========== The tag column on the left is idLab = new JLabel("Student number: "); nameLab = new JLabel("Name: "); sexLab = new JLabel("Gender: "); ageLab = new JLabel("Age: "); jgLab = new JLabel("Hometown: "); deptLab = new JLabel("Direction: "); //=========Fill in the information column on the right is idTxt = new JTextField(); nameTxt = new JTextField(); sexTxt = new JTextField(); ageTxt = new JTextField(); ageTxt = new JTextField(); ageTxt = new JTextField(); jgTxt = new JTextField(); deptTxt = new JTextField(); //========== Add and cancel buttons addBtn = new JButton("Add"); cancelBtn = new JButton("Cancel"); //......Add listen addBtn.addActionListener(this); addBtn.setActionCommand("add"); cancelBtn.addActionListener(this); cancelBtn.setActionCommand("cancel"); //======= Create layout//......Create left column = 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); //......Create the right column 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); //===================The underlying add and cancel button bottom = new JPanel(); bottom.add(addBtn); bottom.add(cancelBtn); //========= Overall layout this.add(left,BorderLayout.WEST); this.add(center,BorderLayout.CENTER); this.add(bottom,BorderLayout.SOUTH); //========== Set window attribute 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, "Add student information failed"); //======== Close the window this.dispose(); } else if(e.getActionCommand().equals("cancel")) { //======== Close the window this.dispose(); } } }4. StuUpdateDialog class (modify student information subinterface)
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.JPanel; import javax.swing.JTextField; import javax.swing.table.AbstractTableModel; public class StuUpdateDialog extends JDialog implements ActionListener{ //========= Panel controls//......Left title bar private JLabel idLab,nameLab,sexLab,ageLab,jgLab,deptLab; //......Right information select fill in the column private JTextField idTxt,nameTxt,sexTxt,ageTxt,jgTxt,deptTxt; //...Add and Cancel button private JButton addBtn,cancelBtn; //......Layout control private JPanel left,center,bottom; //Constructor public StuUpdateDialog(Frame owner, String title, boolean modal, StuModel sm, int rowNum) { //=========Rewrite the parent class method super(owner, title, modal); //=========== The left label bar idLab = new JLabel("Student number: "); nameLab = new JLabel("Name: "); sexLab = new JLabel("Gender: "); ageLab = new JLabel("Age: "); jgLab = new JLabel("Hometown: "); deptLab = new JLabel("Dimension: "); //========Fill in the information on the right side 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)); //======== Add and Cancel buttons addBtn = new JButton("Modify"); cancelBtn = new JButton("Cancel"); //......Add listen addBtn.addActionListener(this); addBtn.setActionCommand("update"); cancelBtn.addActionListener(this); cancelBtn.setActionCommand("cancel"); //======== Create layout//......Create 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); //......Create the right column 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); //============ The bottom layer add and cancel button bottom = new JPanel(); bottom.add(addBtn); bottom.add(cancelBtn); //======== Overall layout this.add(left,BorderLayout.WEST); this.add(center,BorderLayout.CENTER); this.add(bottom,BorderLayout.SOUTH); //========== Set window attribute 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, "Failed to modify student information"); //======== Close the window this.dispose(); } else if(e.getActionCommand().equals("cancel")) { //======== Close the window this.dispose(); } } } 5. SqlHelper class (lowest database class)
package com.sms3; import java.sql.*; public class SqlHelper { //========= Database 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 = "****"; //========== Query public ResultSet queryExecute(String sql, String []paras) { try { //========1. Load the driver Class.forName(driver); //========2. Connect ct = DriverManager.getConnection(url, user, passwd); //========3. Create PreparedStatement ps = ct.prepareStatement(sql); //========4. Assign a value to the question mark if(paras != null) { for(int i = 0; i < paras.length; i++) { ps.setString(i + 1, paras[i]); } } //=======5. Execute rs = ps.executeQuery(); } catch (Exception e) { // TODO: handle exception e.printStackTrace(); } finally { //this.close(); } //=========Return value return rs; } //======== Add or delete public boolean cudExecute(String sql, String []paras) { boolean b = true; try { //========1. Load the driver Class.forName(driver); //=======2. Connect ct = DriverManager.getConnection(url, user, passwd); //=======3. Create PreparedStatement ps = ct.prepareStatement(sql); //========4. Assign a value to the question mark for(int i = 0; i < paras.length; i++) { ps.setString(i + 1, paras[i]); } //========5. Execute if(ps.executeUpdate() != 1) b = false; } catch (Exception e) { // TODO: handle exception b = false; e.printStackTrace(); } finally { this.close(); } //==========Return value return b; } //========== Close resource 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(); } } }Main interface
Add student information interface
Modify the student information interface
The above is all the content of this article. I hope it will be helpful to everyone's learning and I hope everyone will support Wulin.com more.