I am learning Java and database recently. I remember the student management system I wrote before, which was downloaded from the Internet and was perfunctory. I had nothing to do, so I wrote one myself, but the functions did not implement many functions.
Development language: java; Development environment: Mysql, java; Development tools: eclipse
To develop this case, you must first have the Java development environment and Mysql on the computer, the Java development environment and the construction of Mysql, so I will not describe it anymore. If you need it, please contact me with the following contact information: [email protected]
The system is relatively simple this time: there is only one table in the database: stu; function: can add, delete, and modify students.
Development steps:
1. Create tables in the database:
create table stu(stuId String,stuName String,stuSex String,stuAge int,stuJG String,stuDept Sring);
2.java code mainly consists of four classes:
Test3 contains the main function; StuModel is used to refresh and render the database; StuAddDiag is used to add reader functions; StuUpDiag is used to modify student information. The specific code is as follows:
Test3.java:
import javax.swing.*;import java.util.*;import java.awt.*;import java.awt.event.*;import java.sql.Connection;import java.sql.Driver;import java.sql.DriverManager;import java.sql.PreparedStatement;import java.sql.ResultSet;import java.sql.Statement;public class Test3 extends JFrame implements ActionListener { //Define some controls JPanel jp1,jp2; JLabel jl1,jl2; JButton jb1,jb2,jb3,jb4; JTable jt; JScrollPane jsp; JTextField jtf; StuModel sm; //Define the variable Statement stat = null; PreparedStatement ps; Connection ct = null; ResultSet rs = null; public static void main(String[] args){ Test3 test3 = new Test3(); } //Constructor public Test3(){ jp1 = new JPanel(); jtf = new JTextField(10); jb1 = new JButton("Query"); jb1.addActionListener(this); jl1 = new JLabel("Please enter the name:"); jp1.add(jl1); jp1.add(jtf); jp1.add(jb1); jb2 = new JButton("Add"); jb2.addActionListener(this); jb3 = new JButton("Modify"); jb3.addActionListener(this); jb4 = new JButton("Delete"); jb4.addActionListener(this); jp2 = new JPanel(); jp2.add(jb2); jp2.add(jb3); jp2.add(jb4); //Create model object sm = new StuModel(); //Initialize jt = new JTable(sm); jsp = new JScrollPane(jt); //Put jsp into jframe this.add(jsp); this.add(jp1,"North"); this.add(jp2,"South"); this.setSize(600, 400); //this.setLocation(300, 200); this.setDefaultCloseOperation(EXIT_ON_CLOSE); this.setVisible(true); } public void actionPerformed(ActionEvent arg0) { //Determine which button is clicked if(arg0.getSource() == jb1){ System.out.println("The user wants to be queryed..."); //Because the data of the table is encapsulated into StuModel, it is easier to complete the query String name = this.jtf.getText().trim(); //Write a sql statement String sql = "select * from stu where stuName = '"+name+"' "; //Build a data model class and update sm = new StuModel(sql); //Update jtable jt.setModel(sm); } //1. Pop up the add interface else if(arg0.getSource() == jb2){ System.out.println("Add..."); StuAddDiag sa = new StuAddDiag(this,"Add student",true); //Get new data model again, sm = new StuModel(); jt.setModel(sm); }else if(arg0.getSource() == jb4){ //2. Delete the record//1. Get the student ID int rowNum = this.jt.getSelectedRow();//getSelectedRow will return to the row in the user point//If the user has no row selected, return -1 if(rowNum == -1){ //Prompt JOptionPane.showMessageDialog(this, "Please select a row"); return ; } //Get the academic ID String stuId = (String)sm.getValueAt(rowNum, 0); System.out.println("Id: "+stuId); //Connect the database and complete the deletion task try{ //1. Load the driver Class.forName("com.mysql.jdbc.Driver"); //2. Connect to the database String url = "jdbc:mysql://localhost:3306/spdb1"; String user = "root"; String passwd = "lfdy"; ct = DriverManager.getConnection(url, user, passwd); System.out.println("Connection successful"); ps = ct.prepareStatement("delete from stu where stuId = ?"); ps.setString(1,stuId); ps.executeUpdate(); }catch(Exception e){ e.printStackTrace(); } finally{ try{ if(rs!= null){ rs.close(); rs = null; } if(ps!= null){ ps.close(); ps = null; } if(ct != null){ ct.close(); ct = null; } } catch(Exception e){ e.printStackTrace(); } } sm = new StuModel(); //Update jtable jt.setModel(sm); }else if(arg0.getSource() == jb3){ System.out.println("11111"); //3. Users want to modify int rowNum = this.jt.getSelectedRow(); if(rowNum == -1){ // Prompt JOptionPane.showMessageDialog(this, "Please select a row"); return ; } // Show dialog System.out.println( "12435"); StuUpDiag su = new StuUpDiag(this, "Modify Academic", true, sm, rowNum); sm = new StuModel(); jt.setModel(sm); } }}StuModel.java:
/* * This is my stu table model* You can encapsulate all operations on student tables into this class*/package com.test2;import java.sql.Connection;import java.sql.DriverManager;import java.sql.ResultSet;import java.sql.Statement;import java.util.Vector;import javax.swing.table.*;public class StuModel extends AbstractTableModel{ //rowData stores row data, columnNames stores column names Vector rowData, columnNames; //Define the variable Statement stat that connects to the database = null; Connection ct = null; ResultSet rs = null; //Initialize public void init(String sql){ if(sql.equals("")){ sql = "select * from stu"; } //Intermediate//Set column name columnNames = new Vector(); columnNames.add("School number"); columnNames.add("Name"); columnNames.add("Gender"); columnNames.add("Age"); columnNames.add("Name"); columnNames.add("Name"); columnNames.add("Name"); columnNames.add("Sect"); //rowData stores multiple rows rowData = new Vector(); try{ //1. Load driver Class.forName("com.mysql.jdbc.Driver"); System.out.println("Loading successfully"); //2. Connect to the database//Define several constants String url = "jdbc:mysql://localhost:3306/spdb1"; String user = "root"; String passwd = "lfdy"; ct = DriverManager.getConnection(url,user,passwd); stat = ct.createStatement();//Create stat object rs = stat.executeQuery(sql);//Query result while(rs.next()){ Vector hang = new Vector(); hang.add(rs.getString(1)); hang.add(rs.getString(2)); hang.add(rs.getString(3)); hang.add(rs.getInt(4)); hang.add(rs.getString(5)); hang.add(rs.getString(6)); //Add to rowData.add(hang); } }catch(Exception e){ e.printStackTrace(); } finally{ try{ if(rs!=null){ rs.close(); rs = null; } if(stat != null){ stat.close(); stat = null; } if(ct != null){ ct.close(); ct = null; } } catch(Exception e){ e.printStackTrace(); } } } //Add student function public void addStu(String sql){ //Finish the addition task based on the sql statement entered by the user} //The second constructor obtains the data model through the passed sql statement public StuModel(String sql){ this.init(sql); } //Constructor, used to initialize my data model (table) public StuModel(){ this.init(""); } //Get how many rows are there int getRowCount() { // TODO Auto-generated method stub return this.rowData.size(); } //Get how many columns are there int getColumnCount() { // TODO Auto-generated method stub return this.columnNames.size(); } //Get data of a row and a column public Object getValueAt(int row, int column) { // TODO Auto-generated method stub return ((Vector)(this.rowData.get(row))).get(column); } //Get the attribute name public String getColumnName(int column) { // TODO Auto-generated method stub return (String)this.columnNames.get(column); }}StuAddDiag.java:
package com.test2;import javax.swing.JDialog;import javax.swing.*;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.sql.Statement;import java.sql.Connection;import java.sql.DriverManager;import java.sql.ResultSet;import java.sql.*;public class StuAddDiag extends JDialog implements ActionListener { //Define the swing component I need JLabel jl1,jl2,jl3,jl4,jl5,jl6; JTextField jf1,jf2,jf3,jf4,jf5,jf6; JPanel jp1,jp2,jp3; JButton jb1,jb2; //Owner ghostwriter parent window, title is the name of the window, modal specifies that it is a pattern window() or a non-mode window public StuAddDiag(Frame owner,String title,boolean modal){ //Calling parent class method super(owner, title,modal); jl1 = new JLabel("study number"); jl2 = new JLabel("name"); jl3 = new JLabel("gender"); jl4 = new JLabel("age"); jl5 = new JLabel("home"); jl6 = new JLabel("sect"); jf1 = new JTextField(10); jf2 = new JTextField(10); jf3 = new JTextField(10); jf4 = new JTextField(10); jf5 = new JTextField(10); jf6 = new JTextField(10); jb1 = new JButton("Add"); jb1.addActionListener(this); jb2 = new JButton("Cancel"); jp1 = new JPanel(); jp2 = new JPanel(); jp3 = new JPanel(); //Set the layout jp1.setLayout(new GridLayout(6,1)); jp2.setLayout(new GridLayout(6,1)); jp3.add(jb1); jp3.add(jb2); jp1.add(jl1); jp1.add(jl2); jp1.add(jl3); jp1.add(jl4); jp1.add(jl5); jp1.add(jl6); jp2.add(jf1); jp2.add(jf2); jp2.add(jf3); jp2.add(jf4); jp2.add(jf5); jp2.add(jf6); this.add(jp1, BorderLayout.WEST); this.add(jp2, BorderLayout.CENTER); this.add(jp3, BorderLayout.SOUTH); this.setSize(300,200); this.setVisible(true); } @Override public void actionPerformed(ActionEvent e) { // TODO Auto-generated method stub if(e.getSource() == jb1){ Connection ct = null; PreparedStatement pstmt = null; ResultSet rs = null; try{ //1. Load driver Class.forName("com.mysql.jdbc.Driver"); System.out.println("Loaded successfully"); //2. Connect to the database//Define several constants String url = "jdbc:mysql://localhost:3306/spdb1"; String user = "root"; String passwd = "lfdy"; ct = DriverManager.getConnection(url,user,passwd); //Compiled statement object String strsql = "insert into stu values(?,?,?,?,?,?)"; pstmt = ct.prepareStatement(strsql); //Assign the object pstmt.setString(1,jf1.getText()); pstmt.setString(2,jf2.getText()); pstmt.setString(3,jf3.getText()); pstmt.setString(4,jf4.getText()); pstmt.setString(5,jf5.getText()); pstmt.setString(6,jf6.getText()); pstmt.executeUpdate(); this.dispose();//Close the student dialog}catch(Exception arg1){ arg1.printStackTrace(); } finally{ try{ if(rs!=null){ rs.close(); rs = null; } if(pstmt != null){ pstmt.close(); pstmt = null; } if(ct != null){ ct.close(); ct = null; } }catch(Exception arg2){ arg2.printStackTrace(); } } } } } }StuUpDiag.java:
package com.test2;/* * Modify students*/import javax.swing.JDialog;import javax.swing.*;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.sql.Statement;import java.sql.Connection;import java.sql.DriverManager;import java.sql.ResultSet;import java.sql.sql.*;public class StuUpDiag extends JDialog implements ActionListener { //Define the swing component I need JLabel jl1,jl2,jl3,jl4,jl5,jl6; JTextField jf1,jf2,jf3,jf4,jf5,jf6; JPanel jp1,jp2,jp3; JButton jb1,jb2; //Owner pen-in-the-parent window, title is the name of the window, modal specifies that it is a mode window() or a non-mode window public StuUpDiag(Frame owner,String title,boolean modal,StuModel sm,int rowNum){ //Calling parent class method super(owner, title,modal); jl1 = new JLabel("Student number"); jl2 = new JLabel("Name"); jl3 = new JLabel("Gender"); jl4 = new JLabel("Age"); jl5 = new JLabel("Name"); jl6 = new JLabel("Section"); jf1 = new JTextField(10);jf1.setText((sm.getValueAt(rowNum, 0)).toString()); jf2 = new JTextField(10);jf2.setText((String)sm.getValueAt(rowNum, 1)); jf3 = new JTextField(10);jf3.setText(sm.getValueAt(rowNum, 2).toString()); jf4 = new JTextField(10);jf4.setText((sm.getValueAt(rowNum, 3)).toString()); jf5 = new JTextField(10);jf5.setText((String)sm.getValueAt(rowNum, 4)); jf6 = new JTextField(10);jf6.setText((String)sm.getValueAt(rowNum, 5)); jb1 = new JButton("Modify"); jb1.addActionListener(this); jb2 = new JButton("Cancel"); jp1 = new JPanel(); jp2 = new JPanel(); jp3 = new JPanel(); //Set the layout jp1.setLayout(new GridLayout(6,1)); jp2.setLayout(new GridLayout(6,1)); jp3.add(jb1); jp3.add(jb2); jp1.add(jl1); jp1.add(jl2); jp1.add(jl3); jp1.add(jl4); jp1.add(jl5); jp1.add(jl6); jp2.add(jf1); jp2.add(jf2); jp2.add(jf3); jp2.add(jf4); jp2.add(jf5); jp2.add(jf6); this.add(jp1, BorderLayout.WEST); this.add(jp2, BorderLayout.CENTER); this.add(jp3, BorderLayout.SOUTH); this.setSize(300,200); this.setVisible(true); } @Override public void actionPerformed(ActionEvent e) { // TODO Auto-generated method stub if(e.getSource() == jb1){ Connection ct = null; PreparedStatement pstmt = null; ResultSet rs = null; try{ //1. Load driver Class.forName("com.mysql.jdbc.Driver"); System.out.println("Loaded successfully"); //2. Connect to the database//Define several constants String url = "jdbc:mysql://localhost:3306/spdb1"; String user = "root"; String passwd = "lfdy"; ct = DriverManager.getConnection(url,user,passwd); //Compiled statement object String strsql = "insert into stu values(?,?,?,?,?,?)"; pstmt = ct.prepareStatement(strsql); //Assign the object pstmt.setString(1,jf1.getText()); pstmt.setString(2,jf2.getText()); pstmt.setString(3,jf3.getText()); pstmt.setString(4,jf4.getText()); pstmt.setString(5,jf5.getText()); pstmt.setString(6,jf6.getText()); pstmt.executeUpdate(); this.dispose();//Close the student dialog}catch(Exception arg1){ arg1.printStackTrace(); } finally{ try{ if(rs!=null){ rs.close(); rs = null; } if(pstmt != null){ pstmt.close(); pstmt = null; } if(ct != null){ ct.close(); ct = null; } }catch(Exception arg2){ arg2.printStackTrace(); } } } } } }Development and testing results:
1. System main interface:
2. Query by name:
3. Select a line and delete:
4. Select a line to modify:
5. Click the Add button to add:
This system will continue to be improved. If you have any questions or technical exchanges, please contact me: [email protected]
For more information about the management system, please click "Management System Special Topic" to learn
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.