Java+mysql Student Management System Source _java

Source: Internet
Author: User
Tags gettext one table stub aliyun

Recently learning Java and database, think of the previous written student management system, are downloaded from the Internet, perfunctory. There is nothing to do, but also write their own one, but not many functions to achieve.

Development language: Java; development environment: Mysql, Java; Development tools: Eclipse
Development of this case, first of all, on the computer has a Java development environment and MySQL, Java development environment and the building of MySQL, no longer described, if necessary, please contact me at the bottom of the contact:dingyelf@aliyun.com

This system is simpler: there is only one table in the database: Stu; function: Can add, delete, modify to the student.
Development steps:
1. Building tables in the database:

CREATE TABLE Stu (
stuid string,
stuname string,
stusex string,
stuage int,
STUJG string,
Studept sring
);

The 2.java code consists primarily of four classes:
The Test3 contains the main function, Stumodel is used to refresh and render the database, Stuadddiag is used to add the reader function; Stuupdiag is to modify the 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;
 The variable Statement stat = null that defines the connection database;
 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 first 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 the JSP into the 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 ("user
  Want to be queried ... ");
  Because the data on the table is encapsulated into Stumodel, it is simpler to complete the query String name = This.jtf.getText (). Trim ();
  Write an 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);
  //One, pop-up add interface else if (arg0.getsource () = = jb2) {System.out.println ("add ...");
   
  Stuadddiag sa = new Stuadddiag (this, add student, true);
  Re-acquire new data model, SM = Stumodel (); Jt.setmodel (SM); }else if (arg0.getsource () = = JB4) {//II, delete record//1. Get the student id int rownum = this.jt.getSelectedRow ();//getselectedrow will return to the user
  The line in the point//if the user does not select a row, return-1 if (rownum = = 1) {//Prompt Joptionpane.showmessagedialog (this, "Please select a row");
  return;
  //Get Academic id String stuid = (string) sm.getvalueat (rownum, 0);
   
  System.out.println ("Id:" +stuid); 
  Connect the database, complete the delete task try{//1. Load Drive Class.forName ("Com.mysql.jdbc.Driver");
  2. Connection database String URL = "JDBC:MYSQL://LOCALHOST:3306/SPDB1";
  String user = "root";
    
  String passwd = "Lfdy";
  ct = drivermanager.getconnection (url, user, passwd);
  System.out.println ("connected successfully");
  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");
  Third, the user wants to modify int rownum = This.jt.getSelectedRow ();
  if (rownum = = 1) {//Hint Joptionpane.showmessagedialog (this, "Please select a row");
  return;
 }//Show dialog Box System.out.println ("12435");
 Stuupdiag su = new Stuupdiag (This, "Modify academic", true, SM, rownum);
 SM = new Stumodel ();
  Jt.setmodel (SM);
 } 
 }
}

Stumodel.java:

* * This is a model of my STU table * can be the operation of the student table all encapsulated to 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 store row data, columnnames store column name Vector rowdata,columnnames;
 The variable Statement stat = null that defines the connection database;
 Connection ct = null;
 
 ResultSet rs = null;
  Initializes public void init (String sql) {if (Sql.equals (")) {sql = ' select * from Stu ';
  }//middle//Set column name ColumnNames = new Vector ();
  Columnnames.add ("School Number");
  Columnnames.add ("name");
  Columnnames.add ("gender");
  Columnnames.add ("Age");
  Columnnames.add ("Native Place");
    
  Columnnames.add ("Door faction");
    
  RowData store Multiple lines rowdata = new Vector ();
  try{//1. Loading drive Class.forName ("Com.mysql.jdbc.Driver");
  SYSTEM.OUT.PRINTLN ("loaded successfully");
  2. Connection Database//define several constant String urls = "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 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 ();
 }}//increase student function public void addstu (String sql) {//Add task based on SQL statement entered by user}//second constructor, get data model by passing SQL statement
 Public Stumodel (String sql) {this.init (SQL);
 }//constructor, for initializing my Data Model (table) public Stumodel () {This.init (""); //Get total number of rows public int GetRowCount () {//TODO auto-generated method stub RetuRN This.rowData.size ();
 ///Get total number of columns public int getcolumncount () {//TODO auto-generated a stub return this.columnNames.size (); //Get a column of a row of data public Object getvalueat (int row, int column) {//TODO auto-generated Method stub return (Vector) (t
 His.rowData.get (Row))). Get (column); ///Get Property name public String getcolumnname (int column) {//TODO auto-generated Method stub return (String) this.column
 Names.get (column);
 }
}

Stuadddiag.java:

Package com.test2;
Import Javax.swing.JDialog;
Import javax.swing.*;
Import java.awt.*;
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 components I need JLabel jl1,jl2,jl3,jl4,jl5,jl6;
 JTextField Jf1,jf2,jf3,jf4,jf5,jf6;
 JPanel jp1,jp2,jp3;
 JButton jb1,jb2; Owner Ghost of the parent window, title is the name of the window, modal specified is a modal window () or modeless window public stuadddiag (Frame owner,string title, Boolean modal) {//Call the parent class method s
  
  Uper (Owner,title,modal);
  JL1 = new JLabel ("School Number");
  JL2 = new JLabel ("First name"); 
  Jl3 = new JLabel ("gender");
  Jl4 = new JLabel ("Age");
  JL5 = new JLabel ("Native Place");
  
  JL6 = new JLabel ("Gate faction");
  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 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. Loading drive Class.forName ("Com.mysql.jdbc.Driver");
    SYSTEM.OUT.PRINTLN ("loaded successfully"); 2. Connection Database//define several constants String URLs = "jdbc:mysql://localhost:3306/spdB1 ";
    String user = "root";
    String passwd = "Lfdy";
  
    ct = drivermanager.getconnection (URL,USER,PASSWD);
    With the compiled statement object String strSQL = "INSERT into Stu values (?,?,?,?,?,?)";
    
    pstmt = Ct.preparestatement (strSQL);
    Assign a value to an 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 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;
* * * Revise student */import Javax.swing.JDialog;
Import javax.swing.*;
Import java.awt.*;
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 Stuupdiag extends JDialog implements ActionListener {//define the Swing components I need JLabel jl1,jl2,jl3,jl4,jl5,jl6;
 JTextField Jf1,jf2,jf3,jf4,jf5,jf6;
 JPanel jp1,jp2,jp3;
 JButton jb1,jb2; Owner Ghost of the parent window, title is the name of the window, modal specified is a modal window () or modeless window public stuupdiag (Frame owner,string title, Boolean Modal,stumodel SM,
  
  int rownum) {//Call parent class method Super (Owner,title,modal);
  
  JL1 = new JLabel ("School Number");
  
  JL2 = new JLabel ("First name"); 
  Jl3 = new JLabel ("gender");
  Jl4 = new JLabel ("Age");
  
  
  
  JL5 = new JLabel ("Native Place");
  
  
  JL6 = new JLabel ("Gate faction");
  JF1 = new JTextField (n); Jf1.settext (Sm.getvalueat (rownum, 0)). toString ()); JF2 = new JTextField (ten); Jf2.settext ((String) sm.getvalueat (rownum, 1));
  JF3 = new JTextField (n); Jf3.settext (Sm.getvalueat (rownum, 2). toString ()); 
  Jf4 = new JTextField (n); Jf4.settext (Sm.getvalueat (rownum, 3)). ToString ());
  Jf5 = new JTextField (a); Jf5.settext ((String) sm.getvalueat (rownum, 4);
  
  Jf6 = new JTextField (a); 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 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. Loading drive Class.forName ("Com.mysql.jdbc.Driver");
    SYSTEM.OUT.PRINTLN ("loaded successfully");
    2. Connection Database//define several constant String urls = "JDBC:MYSQL://LOCALHOST:3306/SPDB1";
    String user = "root";
    String passwd = "Lfdy";
  
    ct = drivermanager.getconnection (URL,USER,PASSWD);
    With the compiled statement object String strSQL = "INSERT into Stu values (?,?,?,?,?,?)";
    
    pstmt = Ct.preparestatement (strSQL);
    Assign a value to an 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 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 test results:

1. System Main Interface:

2. Search by First Name:

3. Select one line and delete:

4. Select one line to modify:

5. Click the Add button to add:

Follow-up this system will continue to improve, have questions and technical exchange, can contact me: dingyelf@aliyun.com

The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.