Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use java to realize Student Information Management system

2025-01-14 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly explains "how to use java to achieve student information management system", the content of the article is simple and clear, easy to learn and understand, now please follow the editor's ideas slowly in depth, together to study and learn "how to use java to achieve student information management system" bar!

This article example for you to share the java student information management system source code, 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 panel control * * / = query bar queryLab = new JLabel ("Please enter name:"); queryTxt = new JTextField (10); queryBtn = new JButton ("query") AllBtn = new JButton (all); /. Add query bar listener queryBtn.addActionListener (this); queryBtn.setActionCommand ("query"); allBtn.addActionListener (this); allBtn.setActionCommand ("all"); / / = add, delete, change column addBtn = new JButton ("add"); deleteBtn = new JButton ("delete"); updateBtn = new JButton ("modify"); / /. Add add and delete bar listener addBtn.addActionListener (this); addBtn.setActionCommand ("add"); deleteBtn.addActionListener (this); deleteBtn.setActionCommand ("delete"); updateBtn.addActionListener (this); updateBtn.setActionCommand ("update"); / / = create the overall window layout / /. Top-level query bar top = new JPanel (); top.add (queryLab); top.add (queryTxt); top.add (queryBtn); top.add (allBtn); / /. The bottom column bottom = new JPanel (); bottom.add (addBtn); bottom.add (deleteBtn); bottom.add (updateBtn); / /. Middle layer display bar sm = new StuModel (); String sql = "select * from stu"; sm.queryStu (sql, null); resultTb = new JTable (sm); jsp = new JScrollPane (resultTb); / /. Build the overall layout this.add (top,BorderLayout.NORTH); this.add (jsp,BorderLayout.CENTER); this.add (bottom,BorderLayout.SOUTH); / / = set window properties 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")) {/ * query * * / = get input students Name String name = queryTxt.getText (). Trim () If (name.length ()! = 0) {/ / = query / / is executed when the name input is valid. Define the parameter String sql = "select * from stu where stuName=?"; String [] paras = {name}; / /. Update model jtableUpdate (sql, paras);} else {/ / = set reminder JOptionPane.showMessageDialog (this, "name input cannot be empty") when the name is empty }} else if (e.getActionCommand (). Equals ("add")) {/ * add * * / new StuAddDialog (this, "add student information", true); String sql = "select * from stu"; jtableUpdate (sql, null) } else if (e.getActionCommand (). Equals ("all")) {/ * all display * * / String sql = "select * from stu"; jtableUpdate (sql, null) } else if (e.getActionCommand () .equals ("delete")) {/ * delete * * / = get the selected line number int rowNum = this.resultTb.getSelectedRow () If (rowNum = =-1) {JOptionPane.showMessageDialog (this, "Please select a row"); return;} / / = get 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 selected 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 the model sm = new StuModel (); sm.queryStu (sql, paras); / /. Update display 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, delete and modify student public boolean cudStu (String sql, String [] paras) {return new SqlHelper () .cudExecute (sql, paras) } / / = query 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 ("first name"); columnNames.add ("gender"); columnNames.add ("age"); columnNames.add ("place of origin"); columnNames.add ("department") 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 controls / /. The left title bar private JLabel idLab,nameLab,sexLab,ageLab,jgLab,deptLab; / /. Select the fill column private JTextField idTxt,nameTxt,sexTxt,ageTxt,jgTxt,deptTxt; / / for information on the right. Add and cancel buttons private JButton addBtn,cancelBtn; / /. Layout control private JPanel left,center,bottom; / / constructor public StuAddDialog (Frame owner, String title, boolean modal) {/ / = override parent method super (owner, title, modal); / / = left tag bar idLab = new JLabel ("student number:"); nameLab = new JLabel ("name:"); sexLab = new JLabel ("gender:"); ageLab = new JLabel ("age:") JgLab = new JLabel ("place of origin:"); deptLab = new JLabel ("department:"); / / = right information fill bar idTxt = new JTextField (); nameTxt = new JTextField (); sexTxt = new JTextField (); ageTxt = new JTextField (); jgTxt = new JTextField (); deptTxt = new JTextField (); / / = add and cancel buttons addBtn = new JButton ("add") CancelBtn = new JButton ("cancel"); /. Add listening addBtn.addActionListener (this); addBtn.setActionCommand ("add"); cancelBtn.addActionListener (this); cancelBtn.setActionCommand ("cancel"); / / = create layout / /. Create the left column 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); / / = bottom add and cancel buttons 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 properties 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")) {/ * * add student information * / 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, "failed to add student information") / / = close window this.dispose ();} else if (e.getActionCommand (). Equals ("cancel")) {/ / = close 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.JTextField; import javax.swing.table.AbstractTableModel; public class StuUpdateDialog extends JDialog implements ActionListener {/ / = panel controls / /. The left title bar private JLabel idLab,nameLab,sexLab,ageLab,jgLab,deptLab; / /. Select the fill column private JTextField idTxt,nameTxt,sexTxt,ageTxt,jgTxt,deptTxt; / / for information on the right. Add and cancel buttons private JButton addBtn,cancelBtn; / /. Layout control private JPanel left,center,bottom; / / constructor public StuUpdateDialog (Frame owner, String title, boolean modal, StuModel sm, int rowNum) {/ / = override parent method super (owner, title, modal); / / = left tag bar idLab = new JLabel ("student number:"); nameLab = new JLabel ("name:"); sexLab = new JLabel ("gender:") AgeLab = new JLabel ("age:"); jgLab = new JLabel ("place of origin:"); deptLab = new JLabel ("department:"); / / = right information column 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 listening addBtn.addActionListener (this); addBtn.setActionCommand ("update"); cancelBtn.addActionListener (this); cancelBtn.setActionCommand ("cancel"); / / = create layout / /. Create the left column 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); / / = bottom add and cancel buttons 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 properties 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")) {/ * * modify student information * / 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 window this.dispose () } else if (e.getActionCommand () .equals ("cancel")) {/ / = close the window this.dispose ();}

5. SqlHelper class (the 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 driver Class.forName (driver); / / = 2, connect ct = DriverManager.getConnection (url, user, passwd); / / = 3, create PreparedStatement ps = ct.prepareStatement (sql) / / = 4. Assign if (paras! = null) {for (int I = 0; I) to the question mark

< 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(); } } } 主界面

Add student information interface

Modify the student information interface

Thank you for your reading, the above is the content of "how to use java to achieve student information management system". After the study of this article, I believe you have a deeper understanding of how to use java to achieve student information management system, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report