⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 studentquery.java~45~

📁 这里是我在学校时所写的一些程序
💻 JAVA~45~
字号:
//package StudentQuery;
/**
 * <p>Title: Student Database</p>
 * <p>Description: Disign for Student Database</p>
 * <p>Copyright: Copyright (c) 2004 by Haozhigang</p>
 * <p>Company: Hunan Universery</p>
 * @author HaoZhigang
 * @version 1.0
 */

import java.sql.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;

public class StudentQuery extends JFrame{
  private Connection connection;
  private Statement statement;
  private ResultSet resultSet;
  private ResultSetMetaData rsMetaData;

  private JTable table;
  private JTextArea inputQuery;
  private JButton submitQuery,addRecord,
      delRecord,updateRecord,illustrate;
  private int state;

  public StudentQuery()
  {
    super("Enter Query.Click Submit to see Results.");

    state=0;
    String url="jdbc:odbc:Student";
    String username="anonymous";
    String password="guest";

    try{
      Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");

      connection=DriverManager.getConnection(
          url,username,password);

    }
    catch(ClassNotFoundException cnfex){
      System.err.println(
          "Failed to load JDBC/ODBC driver.");
      cnfex.printStackTrace();
      System.exit(1);

    }
    catch(SQLException sqlex){
      System.err.println("Unable to connect");
      sqlex.printStackTrace();
      System.exit(1);
    }

    inputQuery=
        new JTextArea("SELECT * FROM StudentBase",4,30);
    submitQuery=new JButton("Submit query");
    submitQuery.addActionListener(
        new ActionListener(){
      public void actionPerformed(ActionEvent e)
      {
        if (e.getSource() == submitQuery)
          getTable();
      }
    }
    );

    addRecord=new JButton("Add Record");
    addRecord.addActionListener(
        new ActionListener(){
      public void actionPerformed(ActionEvent e)
      {
        if (e.getSource() == addRecord)
          getTableAdd();
      }
    }
    );

    delRecord=new JButton("Delete Record");
    delRecord.addActionListener(
        new ActionListener(){
      public void actionPerformed(ActionEvent e)
      {
        if (e.getSource() == delRecord)
          getTableDel();
      }
    }
    );

    updateRecord=new JButton("Update Record");
    updateRecord.addActionListener(
        new ActionListener(){
      public void actionPerformed(ActionEvent e)
      {
        if (e.getSource() == updateRecord)
          getTableUpdate();
      }
    }
    );

    illustrate=new JButton("Illustrate");
    illustrate.addActionListener(
        new ActionListener(){
      public void actionPerformed(ActionEvent e)
      {
        if (e.getSource() == illustrate)
          illustrate();
      }
    }
    );


    JPanel topPanel=new JPanel();
    JPanel buttonPanel=new JPanel();
    buttonPanel.setLayout(new GridLayout(1,5));
    buttonPanel.add(submitQuery);
    buttonPanel.add(addRecord);
    buttonPanel.add(delRecord);
    buttonPanel.add(updateRecord);
    buttonPanel.add(illustrate);

    topPanel.setLayout(new BorderLayout());
    topPanel.add(new JScrollPane(inputQuery),
                 BorderLayout.CENTER);
    topPanel.add(buttonPanel,BorderLayout.SOUTH);

    table=new JTable(4,4);

    Container c=getContentPane();
    c.setLayout(new BorderLayout());
    c.add(topPanel,BorderLayout.NORTH);
    c.add(table,BorderLayout.CENTER);

    getTable();

    setSize(500,300);
    show();
  }

  private void illustrate()
  {
    state++;
    switch(state){
      case 1:
        inputQuery.setText("SELECT * \nFROM StudentBase\n"+
                           "WHERE Name='Yangchun'");
        break;
      case 2:
        inputQuery.setText("INSERT INTO StudentBase\n"+
                           "VALUES ('20010810405','Haozhigang','21','male','computer')");
        break;
      case 3:
        inputQuery.setText("DELETE FROM StudentBase\n"+
                     "WHERE StudentID='20010810405'");
        break;
      case 4:
        inputQuery.setText("UPDATE StudentBase\n"+
                     "SET Age='22'\n"+
                     "WHERE Name='HaoZhigang'");
        break;
    }
    if(state==4)
      state=0;
  }

  private void getTable()
  {
    try {
      String query = inputQuery.getText();

      statement = connection.createStatement();
      resultSet = statement.executeQuery(query);
      displayResultSet(resultSet);
    }
    catch (SQLException sqlex) {
      sqlex.printStackTrace();
    }
  }

  private void getTableAdd()
  {
    try {
      String query = inputQuery.getText();
      String temp="SELECT * FROM StudentBase";

      statement = connection.createStatement();
      int result = statement.executeUpdate(query);
      if(result==1){
        JOptionPane.showMessageDialog(this,
                                      "Add Record Succeed",
                                      "Add Record Result",
                                      JOptionPane.PLAIN_MESSAGE);
      resultSet = statement.executeQuery(temp);
      displayResultSet(resultSet);
      }
      else
        JOptionPane.showMessageDialog(this,
                                      "Add Record Failed",
                                      "Add Record Result",
                                      JOptionPane.ERROR_MESSAGE);
      statement.close();
    }
    catch (SQLException sqlex) {
      sqlex.printStackTrace();
    }
  }

  private void getTableDel()
  {
    try {
      String query = inputQuery.getText();
      String temp="SELECT * FROM StudentBase";

      statement = connection.createStatement();
      int result = statement.executeUpdate(query);
      if(result==1){
        JOptionPane.showMessageDialog(this,
                                      "Delete Record Success",
                                      "Del Record Success",
                                      JOptionPane.PLAIN_MESSAGE);
      resultSet = statement.executeQuery(temp);
      displayResultSet(resultSet);
      }
      else
        JOptionPane.showMessageDialog(this,
                                      "Delete Record Failed",
                                      "Del Record Failed",
                                      JOptionPane.ERROR_MESSAGE);
      statement.close();
    }
    catch (SQLException sqlex) {
      sqlex.printStackTrace();
    }
  }

  private void getTableUpdate()
  {
    try {
      String query = inputQuery.getText();
      String temp="SELECT * FROM StudentBase";

      statement = connection.createStatement();
      int result = statement.executeUpdate(query);
      if(result==1){
        JOptionPane.showMessageDialog(this,
                                      "Update Record Success",
                                      "Update Record Result",
                                      JOptionPane.PLAIN_MESSAGE);
      resultSet = statement.executeQuery(temp);
      displayResultSet(resultSet);
      }
      else
        JOptionPane.showMessageDialog(this,
                                      "Update Record Failed",
                                      "Update Record Result",
                                      JOptionPane.ERROR_MESSAGE);
      statement.close();
    }
    catch (SQLException sqlex) {
      sqlex.printStackTrace();
    }
  }


  private void displayResultSet(ResultSet rs)
      throws SQLException
  {
    boolean moreRecords = rs.next();

    if (!moreRecords) {
      JOptionPane.showMessageDialog(this,
                                    "ResultSet contained no records");
      setTitle("No records to display");
      return;
    }

    Vector columnHeads = new Vector();
    Vector rows = new Vector();

    try {
      ResultSetMetaData rsmd = rs.getMetaData();

      for (int i = 1; i <= rsmd.getColumnCount(); ++i)
        columnHeads.addElement(rsmd.getColumnName(i));

      do {
        rows.addElement(getNextRow(rs, rsmd));
      }
      while (rs.next());

      table = new JTable(rows, columnHeads);
      JScrollPane scroller = new JScrollPane(table);
      Container c = getContentPane();
      c.remove(1);
      c.add(scroller, BorderLayout.CENTER);
      c.validate();
    }
    catch (SQLException sqlex) {
      sqlex.printStackTrace();
    }
  }

  private Vector getNextRow(ResultSet rs,
                            ResultSetMetaData rsmd)
      throws SQLException
  {
    Vector currentRow=new Vector();

    for(int i=1;i<=rsmd.getColumnCount();++i)
      switch(rsmd.getColumnType(i)){
        case Types.VARCHAR:
        case Types.LONGVARCHAR:
          currentRow.addElement(rs.getString(i) );
          break;
        case Types.INTEGER:
          currentRow.addElement(
              new Long(rs.getLong(i)));
          break;
        default:
          System.out.println("Type was:"+
                             rsmd.getColumnTypeName(i));

      }
    return currentRow;
  }

  public void shutDown()
  {
    try{
      connection.close();
    }
    catch(SQLException sqlex){
      System.err.println("Unable to disconnect");
      sqlex.printStackTrace();
    }
  }

  public static void main(String args[])
  {
    final StudentQuery app=
        new StudentQuery();

    app.addWindowListener(
        new WindowAdapter(){
           public void windowClosing(WindowEvent e)
           {
             app.shutDown();
             System.exit(0);
           }
        }
    );
  }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -