Java写的学生信息管理系统

最初学习Java入门时候的东西:入门的哥们可以看看,就是与数据库链接,查询,删除等操纵数据库。

/**
 * @(#)studentsearch.java
 *
 *
 * @author
 * @version 1.00 2007
 */


/*
 * studentssearch.java
 *
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

/**
 *
 * @author Administrator
 */
import java.awt.*;
import java.awt.event.*;
import java.lang.*;
import java.lang.Object.*;
import java.sql.*;
import java.util.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.table.*;

public class studentssearch extends JFrame{
    JPanel contentPane;
 Label label1 = new Label();
 TextField snoField = new TextField();

 Label label2 = new Label();
    TextField snameField = new TextField();
    Label label3 = new Label();
    TextField smarkField = new TextField();
    Label label4 = new Label();
    TextField cnameField = new TextField();
    Label label5 = new Label();
    TextField sdeptField = new TextField();
  
    Button searchButton = new Button();
    Button deleteButton = new Button();
    Button refreshButton = new Button();
    Button addButton = new Button();
  
    Vector vector;
    String title []={"姓名","学号","课程","系别"," 成绩","老师"};
    Connection connection = null;
    ResultSet rSet =null;
    Statement statement =null;
      AbstractTableModel tm;
        public  void  StuAoddQuerySysFrame() {
    enableEvents(AWTEvent.WINDOW_EVENT_MASK);
    try {
      jbInit();
    }
    catch(Exception e) {
      e.printStackTrace();
    }
  }

  private void jbInit() throws Exception  {
    contentPane = (JPanel) this.getContentPane();
    label1.setText("学号");
    contentPane.setLayout(xYLayout1);
    this.setSize(new Dimension(571, 521));
    this.setTitle("学生成绩管理查询系统");
    label2.setText("姓名");
    label3.setText("成绩");
    label4.setText("课程");
    label5.setText("系名");
    addButton.setLabel("添加");
    deleteButton.setLabel("删除");
    refreshButton.setLabel("更新");
    addButton.setLabel("查询");
    searchButton.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(ActionEvent e) {
       searchButton_actionPerformed(e);
      }
    });
    deleteButton.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(ActionEvent e) {
        deleteButton_actionPerformed(e);
      }
    });
    refreshButton.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(ActionEvent e) {
        refreshButton_actionPerformed(e);
      }
    });
    addButton.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(ActionEvent e) {
        addButton_actionPerformed(e);
      }
    });
     contentPane.add(snoField);
      contentPane.add(label1);
       contentPane.add(snameField);
        contentPane.add(Label2);
         contentPane.add(smarkField);
          contentPane.add(Label3);
           contentPane.add(cnameField);
            contentPane.add(Label4);
             contentPane.add(searchButton);
              contentPane.add(deleteButton);
               contentPane.add(refreshButton);
               contentPane.add(addButton);
               
             createtable();
  }
  void createtable(){
   JTable table;
   JScrollPane scroll;
   vector =new Vector();
   tm = new AbstractTableModel() {  
     public int getColumnCount() {               
        return title.length;
   }
    public int getRowCount() {               
        return vector.size();
      }
      public Object getValueAt(int row, int column) {        
        if(!vector.isEmpty()) {
          return ((Vector)vector.elementAt(row)).elementAt(column);
        }
        else {
          return null;
        }
      }
      public void setValueAt(Object value, int row, int column) {

      }
      public String getColumnName(int column) {               
        return title[column];
      }
      public Class getColumnClass(int c) {                   
        return getValueAt(0,c).getClass();
      }
      public boolean isCellEditable(int row, int column) {   
        return false;
      }
    };
    table = new JTable(tm);                                  
    table.setToolTipText("Display add Result");             
    table.setAutoResizeMode(table.AUTO_RESIZE_OFF);         
    table.setCellSelectionEnabled(false);                    
    table.setShowHorizontalLines(true);                       
    table.setShowVerticalLines(true);
    scroll = new JScrollPane(table);                          
    scroll.setBounds(6,20,540,250);
    contentPane.add(scroll);
  }

  protected void processWindowEvent(WindowEvent e) {
    super.processWindowEvent(e);
    if (e.getID() == WindowEvent.WINDOW_CLOSING) {
      System.exit(0);
    }
  }

  void addButton_actionPerformed(ActionEvent e) {
    try {
      Class.forName("com.mysql.jdbc.Driver"); 
      String url = "jdbc:mysql://localhost:3306/studentmark";            
      connection = DriverManager.getConnection(url,root,"1230"); 
     
      statement = connection.createStatement();
      String sql1 = "insert into student(sno,sname,ssex,sdept) values (" + Integer.parseInt(snoField.getText()) +", '" + snameField.getText()+"'," ",'" "',"+"'"+sdepartField.getText()+ "')";
      String sql2 = "insert into courese (smark,cname,tname) values (" + "'" + smarkField.getText()+"','" + cnamelField.getText()+"'," ")";
      statement.executeUpdate(sql2);            
      statement.executeUpdate(sql1);            
   
      snoField.setText("");
      snameField.setText("");
    
      sdepartField.setText("");
  
    }
    catch(SQLException ex){                     
      System.out.println("\nERROR:----- SQLException -----\n");
      while (ex != null) {
        System.out.println("Message:   " + ex.getMessage());
        System.out.println("SQLState:  " + ex.getSQLState());
        System.out.println("ErrorCode: " + ex.getErrorCode());
        ex = ex.getNextException();
      }
    }
    catch(Exception ex ) {
      ex.printStackTrace();
    }
    finally {
      try {
        if(statement != null) {
          statement.close();                               
        }
        if(connection != null) {
          connection.close();                              
        }
      }
      catch (SQLException ex)  {
        System.out.println("\nERROR:----- SQLException -----\n");
        System.out.println("Message:   " + ex.getMessage( ));
        System.out.println("SQLState:  " + ex.getSQLState());
        System.out.println("ErrorCode: " + ex.getErrorCode());
      }
    }
  }
 
 
  void delete_actionPerformed(ActionEvent e){
   try{
    Class.forName("com.mysql.jdbc.Driver"); 
      String url = "jdbc:mysql://localhost:3306/studentmark";            
      connection = DriverManager.getConnection(url,root,"1230"); 
     
      statement = connection.createStatement(
       ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
       String sql="select * from student where sno="Integer.parseInt(snoField.gettext());
       rSet = statement.executeQuery(sql);            
      if (rSet.next()==false) {                      
        JOptionPane msg = new JOptionPane();
        JOptionPane.showMessageDialog(StuAddQuerySysFrame.this, "数据库中没有您删除的学号", "数据库中没有您删除的学号!", 1);
      }
      else {
        String sql1="delete from student where sno="+Integer.parseInt(snoField.getText());
        statement.executeUpdate(sql1);            
        String sql2="delete from course where cname="+Integer.parseInt(cnameField.getText());
        statement.executeUpdate(sql2);            
      
        snoField.setText("");
        snameField.setText("");
        sdeptField.setText("");
       
      }
    }
    catch(SQLException ex){                       
      System.out.println("\nERROR:----- SQLException -----\n");
      while (ex != null) {
        System.out.println("Message:   " + ex.getMessage());
        System.out.println("SQLState:  " + ex.getSQLState());
        System.out.println("ErrorCode: " + ex.getErrorCode());
        ex = ex.getNextException();
      }
    }
    catch(Exception ex ) {
      ex.printStackTrace();
    }
    finally {
      try {
        if(statement != null) {
          statement.close();                              
        }
        if(connection != null) {
          connection.close();                             
        }
      }
      catch (SQLException ex)  {
        System.out.println("\nERROR:----- SQLException -----\n");
        System.out.println("Message:   " + ex.getMessage( ));
        System.out.println("SQLState:  " + ex.getSQLState());
        System.out.println("ErrorCode: " + ex.getErrorCode());
      }
    }
  }
       
 void refreshButton_actionPerformed(ActionEvent e) {
    try {
     Class.forName("com.mysql.jdbc.Driver"); 
      String url = "jdbc:mysql://localhost:3306/studentmark";            
      connection = DriverManager.getConnection(url,root,"1230"); 
     
      statement = connection.createStatement();
     
      String sql1="update student set sname='"+snameField.getText()+"', ssex='"+sexField.getText()+"',sdept='"+sdeptField.getText()+"' where sno="+Integer.parseInt(snoField.getText());
      statement.executeUpdate(sql1);           
      String sql2="update course set cname='"+cnameField.getText()+"' where cname="+Integer.parseInt(cnoField.getText());
      statement.executeUpdate(sql2);            
   String sql3="update smark set smark='"+Integer.parseInt(smarkField.getText())+"' where sno="+Integer.parseInt(snoField.getText());
      statement.executeUpdate(sql3);
      snoField.setText("");
      snameField.setText("");
      sdeptField.setText("");
      smarkField.setText("");
      cnameField.setText("");
    
     
    }
    catch(SQLException ex){                       
      System.out.println("\nERROR:----- SQLException -----\n");
      while (ex != null) {
        System.out.println("Message:   " + ex.getMessage());
        System.out.println("SQLState:  " + ex.getSQLState());
        System.out.println("ErrorCode: " + ex.getErrorCode());
        ex = ex.getNextException();
      }
    }
    catch(Exception ex ) {
      ex.printStackTrace();
    }
    finally {
      try {
        if(statement != null) {
          statement.close();                             
        }
        if(connection != null) {
          connection.close();                             
        }
      }
      catch (SQLException ex)  {
        System.out.println("\nERROR:----- SQLException -----\n");
        System.out.println("Message:   " + ex.getMessage( ));
        System.out.println("SQLState:  " + ex.getSQLState());
        System.out.println("ErrorCode: " + ex.getErrorCode());
      }
    }
  }
 
  void queryButton_actionPerformed(ActionEvent e) { 
    try {
      Class.forName("com.mysql.jdbc.Driver"); 
      String url = "jdbc:mysql://localhost:3306/studentmark";            
      connection = DriverManager.getConnection(url,root,"1230"); 
     
      statement = connection.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
     
      String sql = "select * from student where sname="+snameField.getText() ;
      rSet = statement.executeQuery(sql);            
      if (rSet.next()==false) {                     
        JOptionPane msg = new JOptionPane();
        JOptionPane.showMessageDialog(StuAddQuerySysFrame.this, "数据库中没有您查询的studnet", "数据库中没有您查询的学生", 1);
      }
      else {
      sql = "select student.sno,sname ,sdept,smark,cname from student,course,smark,teacher where sname="+snameField.getText()
         +"and"+smark= '"+Integer.parseInt(smarkField.getText())"' ;
        ResultSet rs = statement.executeQuery(sql);  
        stunumField.setText("") ;          
        vector.removeAllElements();        
        tm.fireTableStructureChanged();   
        while(rs.next())
        {
          Vector rec_vector = new Vector();
         
          rec_vector.addElement(String.valueOf(rs.getInt(1)));
          rec_vector.addElement(rs.getString(2));
          rec_vector.addElement(String.valueOf(rs.getInt(4)));
          rec_vector.addElement(rs.getString(3));
          rec_vector.addElement(rs.getString(5));
       
          vector.addElement(rec_vector);  
        }
        tm.fireTableStructureChanged();      
      }
    }
    catch(SQLException ex){                
      System.out.println("\nERROR:----- SQLException -----\n");
      while (ex != null) {
        System.out.println("Message:   " + ex.getMessage());
        System.out.println("SQLState:  " + ex.getSQLState());
        System.out.println("ErrorCode: " + ex.getErrorCode());
        ex = ex.getNextException();
      }
    }
    catch(Exception ex ) {
      ex.printStackTrace();
    }
    finally {
      try {
        if(statement != null) {
          statement.close();                              
        }
        if(connection != null) {
          connection.close();                              
        }
      }
      catch (SQLException ex)  {
        System.out.println("\nERROR:----- SQLException -----\n");
        System.out.println("Message:   " + ex.getMessage( ));
        System.out.println("SQLState:  " + ex.getSQLState());
        System.out.println("ErrorCode: " + ex.getErrorCode());
      }
    }
  }

    
   }
 

posted on 2010-05-31 09:15  sunliming  阅读(4997)  评论(1编辑  收藏  举报