Java 基于MySQL数据库的简单学生管理系统

因为实验室要交作业然后就做了一个学生管理系统  贴个代码纪念一下~
做的太急界面什么的也比较差
还有一些小细节没有完善不过还是能实现主要的功能的~

Window是主界面
 

  1. package First;
  2.  
  3. import java.sql.*;
  4. import java.awt.*;
  5. import java.awt.event.*;
  6. import javax.swing.*;
  7.  
  8. public class Window {
  9.    public static void main(String[] args){
  10.            JFrame jframe = new JFrame("学生管理系统") ; //window
  11.            Dimension d = new Dimension(400,300);
  12.            Point p = new Point (250,350);
  13.           
  14.            jframe.setSize(d);
  15.            jframe.setLocation(p);
  16.            jframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  17.            jframe.setVisible(true);
  18.           
  19.            JButton button1 = new JButton("添加");
  20.            JButton button2 = new JButton("修改");
  21.            JButton button3 = new JButton("查询");
  22.            JButton button4 = new JButton("删除");
  23.            JButton button5 = new JButton("浏览");
  24.           
  25.            FlowLayout flow = new FlowLayout(FlowLayout.LEFT,10,10);
  26.            JPanel panel = new JPanel(flow);
  27.            panel.add(button1);
  28.            panel.add(button2);
  29.            panel.add(button3);
  30.            panel.add(button4);
  31.            panel.add(button5);
  32.           
  33.            jframe.add(panel);
  34.           
  35.            button1.addActionListener(new ActionListener(){
  36.                         public void actionPerformed(ActionEvent e){
  37.                                 Add add = new Add();
  38.  
  39.                         }                       
  40.                 });
  41.           
  42.            button2.addActionListener(new ActionListener(){
  43.                         public void actionPerformed(ActionEvent e){
  44.                                 Change change = new Change();                       
  45.                         }                       
  46.                 });
  47.           
  48.            button3.addActionListener(new ActionListener(){
  49.                         public void actionPerformed(ActionEvent e){
  50.                                 Ask ask = new Ask();                       
  51.                         }                       
  52.                 });
  53.           
  54.            button4.addActionListener(new ActionListener(){
  55.                         public void actionPerformed(ActionEvent e){
  56.                                 Delete delete = new Delete();                       
  57.                         }                       
  58.                 });
  59.           
  60.            button5.addActionListener(new ActionListener(){
  61.                         public void actionPerformed(ActionEvent e){
  62.                                 Look look = new Look();                       
  63.                         }                       
  64.                 });
  65.           
  66.    }
  67.  
  68. }

复制代码


Add是添加

 

  1. package First;
  2.  
  3. import java.sql.*;
  4. import java.awt.*;
  5. import java.awt.event.*;
  6. import javax.swing.*;
  7.  
  8. import com.mysql.jdbc.Driver;
  9.  
  10. import First.Window;
  11.  
  12. public class Add extends JFrame {
  13.         private static final long serialVersionUID = -1928970409928880648L;
  14.        
  15.         JLabel jlnumber = new JLabel("学号:");
  16.         JLabel jlname = new JLabel("姓名:");
  17.         JLabel jlsex = new JLabel("性别:");
  18.         JLabel jlbirthday = new JLabel("出生日期:");
  19.         JLabel jldepartment = new JLabel("学院:");
  20.        
  21.         JTextField jtnumber = new JTextField("",20);
  22.         JTextField jtname = new JTextField("",20);
  23.         JTextField jtsex = new JTextField("",20);
  24.         JTextField jtbirthday = new JTextField("",20);
  25.         JTextField jtdepartment = new JTextField("",20);
  26.        
  27.         JButton buttonadd = new JButton("添加");
  28.         JButton buttonreturn = new JButton("返回");
  29.        
  30.        
  31.         public Add() {
  32.                 JPanel jpnumber = new JPanel();
  33.                 JPanel jpname = new JPanel();
  34.                 JPanel jpsex = new JPanel();
  35.                 JPanel jpbirthday = new JPanel();
  36.                 JPanel jpdepartment = new JPanel();
  37.                 JPanel jpforbutton = new JPanel(new GridLayout(1,1));
  38.                
  39.                 jpnumber.add(jlnumber);
  40.                 jpnumber.add(jtnumber);
  41.                
  42.                 jpname.add(jlname);
  43.                 jpname.add(jtname);
  44.                
  45.                 jpsex.add(jlsex);
  46.                 jpsex.add(jtsex);
  47.                
  48.                 jpbirthday.add(jlbirthday);
  49.                 jpbirthday.add(jtbirthday);
  50.                
  51.                 jpdepartment.add(jldepartment);
  52.                 jpdepartment.add(jtdepartment);
  53.                
  54.                 jpforbutton.add(buttonadd);
  55.                 jpforbutton.add(buttonreturn);
  56.                
  57.                 buttonadd.addActionListener(new ActionListener(){
  58.                         public void actionPerformed(ActionEvent e){
  59.                                
  60.                                 //Add
  61.                                 Connection conn = null;
  62.                                 Statement stat = null;
  63.                                 PreparedStatement ps=null;
  64.                                 String sql = "INSERT INTO student(number,name,sex,birthday,department) "
  65.                                                 + "values(?,?,?,?,?)";
  66.                                 try{
  67.                                         Class.forName("Driver");
  68.                                         System.out.println("JBDC 加载成功!");
  69.                                 }catch(Exception a){
  70.                                         System.out.println("JBDC 狗带!");
  71.                                         a.printStackTrace();
  72.                                 }
  73.                                 try{
  74.                                         conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/javaStu","root","123");
  75.                                         ps=conn.prepareStatement(sql);
  76.                                        
  77.                                         ps.setString(1,jtnumber.getText());
  78.                                         ps.setString(2,jtname.getText());
  79.                                         ps.setString(3,jtsex.getText());
  80.                                         ps.setString(4,jtbirthday.getText());
  81.                                         ps.setString(5,jtdepartment.getText());
  82.  
  83.                                         ps.executeUpdate();
  84.                                        
  85.                                         //System.out.println("MySQL 连接成功!");
  86.                                         //stat = conn.createStatement();
  87.                                         //stat.executeUpdate(sql);
  88.                                         //System.out.println("插入数据成功!");
  89.                                        
  90.                                 }catch (SQLException b){
  91.                                         b.printStackTrace();
  92.                                 }finally{
  93.                                         try{
  94.                                                 conn.close();
  95.                                                 System.out.println("MySQL 关闭成功");
  96.                                         }catch (SQLException c){
  97.                                                 System.out.println("MySQL 关闭失败 ");
  98.                                                 c.printStackTrace();
  99.                                         }
  100.                                        
  101.                                 }
  102.                                        
  103.                                        
  104.                 }}       
  105.                
  106.                                 );
  107.                
  108.                 buttonreturn.addActionListener(new ActionListener(){
  109.                         public void actionPerformed(ActionEvent e){
  110.                                 Window window = new Window();                       
  111.                         }                       
  112.                 });
  113.                
  114.                
  115.                 this.setTitle("添加学生信息");
  116.                 this.setLayout(new GridLayout(9,1));
  117.                 this.add(jpnumber);
  118.                 this.add(jpname);
  119.                 this.add(jpsex);
  120.                 this.add(jpbirthday);
  121.                 this.add(jpdepartment);
  122.                 this.add(jpforbutton);
  123.                 this.setLocation(400,300);
  124.                 this.setSize(350,300);
  125.                 this.setVisible(true);
  126.                
  127.         }
  128.        
  129.        
  130. }
  131.  

复制代码






Ask是查询
 

  1. package First;
  2.  
  3. import java.sql.*;
  4. import java.awt.*;
  5. import java.awt.event.*;
  6. import javax.swing.*;
  7.  
  8. import First.Window;
  9.  
  10. public class Ask extends JFrame {
  11.         private static final long serialVersionUID = -1928970409928880648L;
  12.        
  13.         JLabel jlnumber = new JLabel("学号:");
  14.         JLabel jlname = new JLabel("姓名:");
  15.         JLabel jlsex = new JLabel("性别:");
  16.         JLabel jlbirthday = new JLabel("出生日期:");
  17.         JLabel jldepartment = new JLabel("学院:");
  18.        
  19.         JTextField jtnumber = new JTextField("",20);
  20.         JLabel jname = new JLabel();
  21.         JLabel jsex = new JLabel();
  22.         JLabel jbirthday = new JLabel();
  23.         JLabel jdepartment = new JLabel();
  24.        
  25.         JButton buttonask = new JButton("查询");
  26.         JButton buttonreturn = new JButton("返回");
  27.        
  28.        
  29.         public Ask() {
  30.                 JPanel jpnumber = new JPanel();
  31.                 JPanel jpname = new JPanel();
  32.                 JPanel jpsex = new JPanel();
  33.                 JPanel jpbirthday = new JPanel();
  34.                 JPanel jpdepartment = new JPanel();
  35.                 JPanel jpforbutton = new JPanel(new GridLayout(1,1));
  36.                
  37.                 jpnumber.add(jlnumber);
  38.                 jpnumber.add(jtnumber);
  39.                
  40.                 jpname.add(jlname);
  41.                 jpname.add(jname);
  42.                
  43.                 jpsex.add(jlsex);
  44.                 jpsex.add(jsex);
  45.                
  46.                 jpbirthday.add(jlbirthday);
  47.                 jpbirthday.add(jbirthday);
  48.                
  49.                 jpdepartment.add(jldepartment);
  50.                 jpdepartment.add(jdepartment);
  51.                
  52.                 jpforbutton.add(buttonask);
  53.                 jpforbutton.add(buttonreturn);
  54.                
  55.                 buttonask.addActionListener(new ActionListener(){
  56.                         public void actionPerformed(ActionEvent e){
  57.                                 Connection conn = null;
  58.                                 ResultSet res = null;
  59.                                 Statement stat = null;
  60.                                
  61.                                 String sql = "SELECT number,name,sex,birthday,department FROM student;";
  62.                                 try{
  63.                                         Class.forName("com.mysql.jdbc.Driver");
  64.                                        
  65.                                 }catch(Exception d){
  66.                                         System.out.println("jdbc fall");
  67.                                         d.printStackTrace();
  68.                                 }
  69.                                 try{
  70.                                         conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/javaStu","root","123");
  71.                                         stat=conn.createStatement();
  72.                                         res=stat.executeQuery(sql);
  73.                                         while (res.next())
  74.                                         {
  75.                                                 if (res.getString(1).equals(jtnumber.getText()))
  76.                                                 {
  77.                                                         jname.setText(res.getString(2));
  78.                                                         jsex.setText(res.getString(3));
  79.                                                         jbirthday.setText(res.getString(4));
  80.                                                         jdepartment.setText(res.getString(5));
  81.  
  82.                                                         break;
  83.                                                 }
  84.                                         }
  85.                                 }catch (SQLException e1) {
  86.                                         // TODO Auto-generated catch block
  87.                                         e1.printStackTrace();
  88.                                
  89.                                
  90.                         }
  91.                                 finally{
  92.                                         try{
  93.                                                 conn.close();
  94.                                         }catch(SQLException ar){
  95.                                                 ar.printStackTrace();
  96.                                         }
  97.                        
  98.                                 }}}
  99.                        
  100.                                 );
  101.                
  102.                 buttonreturn.addActionListener(new ActionListener(){
  103.                         public void actionPerformed(ActionEvent e){
  104.                                 Window window = new Window();                       
  105.                         }                       
  106.                 });
  107.                
  108.                
  109.                 this.setTitle("查询学生信息");
  110.                 this.setLayout(new GridLayout(9,1));
  111.                 this.add(jpnumber);
  112.                 this.add(jpname);
  113.                 this.add(jpsex);
  114.                 this.add(jpbirthday);
  115.                 this.add(jpdepartment);
  116.                 this.add(jpforbutton);
  117.                 this.setLocation(400,300);
  118.                 this.setSize(350,300);
  119.                 this.setVisible(true);
  120.                
  121.                
  122.         }
  123.        
  124.        
  125. }
  126.  

复制代码






Change是修改
 

  1. package First;
  2.  
  3. import java.sql.*;
  4. import java.awt.*;
  5. import java.awt.event.*;
  6. import javax.swing.*;
  7.  
  8. import First.Window;
  9.  
  10. public class Change extends JFrame {
  11.         private static final long serialVersionUID = -1928970409928880648L;
  12.        
  13.         JLabel jlnumber = new JLabel("学号:");
  14.         JLabel jlname = new JLabel("姓名:");
  15.         JLabel jlsex = new JLabel("性别:");
  16.         JLabel jlbirthday = new JLabel("出生日期:");
  17.         JLabel jldepartment = new JLabel("学院:");
  18.        
  19.         JTextField jtnumber = new JTextField("",20);
  20.         JTextField jtname = new JTextField("",20);
  21.         JTextField jtsex = new JTextField("",20);
  22.         JTextField jtbirthday = new JTextField("",20);
  23.         JTextField jtdepartment = new JTextField("",20);
  24.        
  25.         JButton buttonchange = new JButton("修改");
  26.         JButton buttonreturn = new JButton("返回");
  27.        
  28.        
  29.         public Change() {
  30.                 JPanel jpnumber = new JPanel();
  31.                 JPanel jpname = new JPanel();
  32.                 JPanel jpsex = new JPanel();
  33.                 JPanel jpbirthday = new JPanel();
  34.                 JPanel jpdepartment = new JPanel();
  35.                 JPanel jpforbutton = new JPanel(new GridLayout(1,1));
  36.                
  37.                 jpnumber.add(jlnumber);
  38.                 jpnumber.add(jtnumber);
  39.                
  40.                 jpname.add(jlname);
  41.                 jpname.add(jtname);
  42.                
  43.                 jpsex.add(jlsex);
  44.                 jpsex.add(jtsex);
  45.                
  46.                 jpbirthday.add(jlbirthday);
  47.                 jpbirthday.add(jtbirthday);
  48.                
  49.                 jpdepartment.add(jldepartment);
  50.                 jpdepartment.add(jtdepartment);
  51.                
  52.                 jpforbutton.add(buttonchange);
  53.                 jpforbutton.add(buttonreturn);
  54.                
  55.                 buttonchange.addActionListener(new ActionListener(){
  56.                         public void actionPerformed(ActionEvent e){
  57.                                 String number = jtnumber.getText();
  58.                                 String name = jtname.getText();
  59.                                 String sex = jtsex.getText();
  60.                                 String birthday = jtbirthday.getText();
  61.                                 String department = jtdepartment.getText();
  62.                                
  63.                                 Connection conn = null;
  64.                                 ResultSet res = null;
  65.                                 Statement stat = null;
  66.                                
  67.                                 String sql = "SELECT number,name,sex,birthday,department FROM student;";
  68.                                 try{
  69.                                         Class.forName("com.mysql.jdbc.Driver");
  70.                                        
  71.                                 }catch(Exception d){
  72.                                         System.out.println("jdbc fall");
  73.                                         d.printStackTrace();
  74.                                 }
  75.                                 try{
  76.                                         conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/javaStu","root","123");
  77.                                         stat=conn.createStatement();
  78.                                         res=stat.executeQuery(sql);
  79.                                         while (res.next())
  80.                                         {
  81.                                                 //change
  82.                                                 if (res.getString(1).equals(jtnumber.getText()))
  83.                                                 {
  84.                         try{
  85.                                                         Class.forName("com.mysql.jdbc.Driver");
  86.                                                 }catch(Exception d){
  87.                                                         System.out.println("jdbc fall");
  88.                                                         d.printStackTrace();
  89.                                                 }
  90.                                                        
  91.                                                         String sql2="UPDATE student SET name=""+name+""  WHERE number=""+jtnumber.getText()+""";
  92.                                                         String sql3="UPDATE student SET sex=""+sex+""  WHERE number=""+jtnumber.getText()+""";
  93.                                                         String sql4="UPDATE student SET birthday=""+birthday+""  WHERE number=""+jtnumber.getText()+""";
  94.                                                         String sql5="UPDATE student SET department=""+department+""  WHERE number=""+jtnumber.getText()+""";
  95.                                                         try {
  96.                                                                 conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/javaStu","root","123");
  97.                                                                 stat=conn.createStatement();
  98.                                                                 stat.executeUpdate(sql2);
  99.                                                                 stat.executeUpdate(sql3);
  100.                                                                 stat.executeUpdate(sql4);
  101.                                                                 stat.executeUpdate(sql5);
  102.                                                         } catch (SQLException g) {
  103.                                                                 // TODO Auto-generated catch block
  104.                                                                 g.printStackTrace();
  105.                                                         }try{
  106.                                                                 stat.close();
  107.                                                                 conn.close();
  108.                                                         }catch(SQLException ar){
  109.                                                                 ar.printStackTrace();
  110.                                                 }
  111.  
  112.                                                         break;
  113.                                                 }
  114.                                                
  115.                                         //change end
  116.                                         }
  117.                                 }catch (SQLException e1) {
  118.                                         // TODO Auto-generated catch block
  119.                                         e1.printStackTrace();
  120.                                
  121.                                
  122.                         }
  123.                                 finally{
  124.                                         try{
  125.                                                 conn.close();
  126.                                         }catch(SQLException ar){
  127.                                                 ar.printStackTrace();
  128.                                         }
  129.                        
  130.                                 }
  131.                                
  132.                         }
  133.                        
  134.                        
  135.                 });
  136.                
  137.                
  138.                 buttonreturn.addActionListener(new ActionListener(){
  139.                         public void actionPerformed(ActionEvent e){
  140.                                 Window window = new Window();                       
  141.                         }                       
  142.                 });
  143.                
  144.                 this.setTitle("修改学生信息");
  145.                 this.setLayout(new GridLayout(9,1));
  146.                 this.add(jpnumber);
  147.                 this.add(jpname);
  148.                 this.add(jpsex);
  149.                 this.add(jpbirthday);
  150.                 this.add(jpdepartment);
  151.                 this.add(jpforbutton);
  152.                 this.setLocation(400,300);
  153.                 this.setSize(350,300);
  154.                 this.setVisible(true);
  155.                
  156.                
  157.         }
  158.        
  159.        
  160. }
  161.  

复制代码






Delete是删除
 

  1. package First;
  2.  
  3. import java.sql.*;
  4. import java.awt.*;
  5. import java.awt.event.*;
  6.    import javax.swing.*;
  7.  
  8. import First.Window;
  9.  
  10.  
  11.  
  12.    public class Delete extends JFrame {
  13.                 private static final long serialVersionUID = -1928970409928880648L;
  14.                                   
  15.                 JLabel jlnumber = new JLabel("学号:");
  16.                
  17.                 JTextField jtnumber = new JTextField("",20);
  18.                
  19.                 JButton buttondelete = new JButton("删除");
  20.                 JButton buttonreturn = new JButton("返回");
  21.                
  22.                
  23.                 public Delete() {
  24.                         JPanel jpnumber = new JPanel();
  25.                         JPanel jpforbutton = new JPanel(new GridLayout(1,1));
  26.                        
  27.                         jpnumber.add(jlnumber);
  28.                         jpnumber.add(jtnumber);
  29.                        
  30.                         jpforbutton.add(buttondelete);
  31.                         jpforbutton.add(buttonreturn);
  32.                        
  33.                         buttondelete.addActionListener(new ActionListener(){
  34.                                 public void actionPerformed(ActionEvent e){
  35.                                         String number = jtnumber.getText();
  36.                                        
  37.                                         Connection conn = null;
  38.                                         ResultSet res = null;
  39.                                         Statement stat = null;
  40.                                         String sql = "DELETE FROM student WHERE number=""+number+""";
  41.                                        
  42.                                         try{
  43.                                                 Class.forName("com.mysql.jdbc.Driver");
  44.                                         }catch(Exception a){
  45.                                                 a.printStackTrace();
  46.                                         }
  47.                                         try{
  48.                                                 conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/javaStu","root","123");
  49.                                                 stat = conn.createStatement();
  50.                                                 stat.executeUpdate(sql);
  51.                                         }catch(SQLException h){
  52.                                                 h.printStackTrace();
  53.                                                
  54.                                         }finally{
  55.                                                 try{
  56.                                                         conn.close();
  57.                                                         System.out.println("close success!");
  58.                                                 }catch(SQLException j){
  59.                                                         System.out.println("close go die!");
  60.                                                     j.printStackTrace();
  61.                                                 }
  62.                                                
  63.                                         }
  64.                                        
  65.                                 }
  66.                                
  67.                                
  68.                         });
  69.                        
  70.                         buttonreturn.addActionListener(new ActionListener(){
  71.                                 public void actionPerformed(ActionEvent e){
  72.                                         Window window = new Window();                       
  73.                                 }                       
  74.                         });
  75.                        
  76.                        
  77.                         this.setTitle("删除学生信息");
  78.                         this.setLayout(new GridLayout(9,1));
  79.                         this.add(jpnumber);
  80.                         this.add(jpforbutton);
  81.                         this.setLocation(400,300);
  82.                         this.setSize(350,300);
  83.                         this.setVisible(true);
  84.                        
  85.                        
  86.                 }
  87.                
  88.             
  89.             
  90.         }
  91.        
  92.        
  93.  

复制代码






Look是浏览
 

  1. package First;
  2.  
  3. import java.sql.*;
  4. import java.awt.*;
  5. import java.awt.event.*;
  6. import javax.swing.*;
  7. import java.util.*;
  8.  
  9. import First.Window;
  10.  
  11. public class Look extends JFrame {
  12.         private static final long serialVersionUID = -1928970409928880648L;
  13.        
  14.         Connection conn = null;
  15.         PreparedStatement ps = null;
  16.         ResultSet res = null;
  17.        
  18.        
  19.         //JButton buttonlook = new JButton("浏览");
  20.         //JButton buttonreturn = new JButton("返回");
  21.        
  22.         JTable jtable;
  23.         JScrollPane jscrollpane = new JScrollPane();
  24.        
  25.         Vector columnNames = null;
  26.         Vector rowData = null;
  27.        
  28.         public Look() {
  29.                 JPanel jpforbutton = new JPanel(new GridLayout(1,1));
  30.  
  31.                 columnNames = new Vector();
  32.                 columnNames.add("学号");
  33.                 columnNames.add("姓名");
  34.                 columnNames.add("性别");
  35.                 columnNames.add("出生日期");
  36.                 columnNames.add("学院");
  37.                 rowData = new Vector();
  38.                
  39.                 //jpforbutton.add(buttonlook);
  40.                 //jpforbutton.add(buttonreturn);
  41.                
  42.                
  43.                 try {
  44.                         Class.forName("com.mysql.jdbc.Driver");
  45.                         conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/javaStu","root","123");
  46.                         ps = conn.prepareStatement("SELECT * FROM student");
  47.                         res = ps.executeQuery();
  48.                         while (res.next())
  49.                         {
  50.                                 Vector hang = new Vector();
  51.                                 hang.add(res.getString(1));
  52.                                 hang.add(res.getString(2));
  53.                                 hang.add(res.getString(3));
  54.                                 hang.add(res.getString(4));
  55.                                 hang.add(res.getString(5));
  56.                                 rowData.add(hang);
  57.                                
  58.                         }
  59.                         System.out.println("load  ok!");
  60.                 }catch (Exception q){
  61.                         q.printStackTrace();
  62.                         System.out.println("go die");
  63.                 }finally{
  64.                         try{
  65.                                 res.close();
  66.                                 ps.close();
  67.                                 conn.close();
  68.                                 System.out.println("close ok");
  69.                         }catch (SQLException o){
  70.                                 o.printStackTrace();
  71.                                 System.out.println("go die 2");
  72.                         }
  73.                 }
  74.                
  75.                
  76.                
  77.                
  78.                 jtable = new JTable(rowData,columnNames);
  79.                 jscrollpane = new JScrollPane(jtable);
  80.                
  81.                 this.add(jscrollpane);
  82.                 this.setTitle("浏览学生信息");
  83.                 this.setLayout(new GridLayout(2,5));
  84.                 this.add(jpforbutton);
  85.                 this.setLocation(300,300);
  86.                 this.setSize(500,300);
  87.                 this.setVisible(true);
  88.                 this.setResizable(false);
  89.                
  90.         }
  91.        
  92.        
  93. }

复制代码





一些运行的界面~






猜你喜欢

转载自my.oschina.net/u/3797416/blog/1647796