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

📄 comprehenquery.java~21~

📁 这个代码是一本关于JAVA书籍的源代码 这本书叫做JAVA+SQLSERVER项目开发实践,希望对大家有帮助
💻 JAVA~21~
字号:
package flight.query;

import flight.assist.*;

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


public class ComprehenQuery
    extends JPanel
    implements ActionListener {
  DataBaseManager dbManager = DataBaseManager.getInstance();

  private JComboBox jcbStart = new JComboBox(),
      jcbFirstArrive = new JComboBox(),
      jcbArrive = new JComboBox(), jcbAirFirm = new JComboBox();

  //*********************************************************************

   private JRadioButton jrbSingle = new JRadioButton("单程", true),
       jrbDouble = new JRadioButton("往返", false),
       jrbMutiple = new JRadioButton("联程", false);

  //The program should change the content in the label dynamically,
  //so we should put the JLabels as the menber varibles
  //so that we can change their contents in every method of this class
  private JLabel jlStart, jlFirstArrive, jlArrive, jlAirFirm, jlReplaceArrive;

  //The program should remove and add components into the framework dynamically
  //according to the query mode you have selected!
  //So we should put the JPanels as the menber varibles
  //so that we can remove and add components in the panels in every method of this class
  private JPanel jpFirstArriveBox, jpReplaceArrive, jpArrive1;

  private JButton jbQuery = new JButton("查询");

  //Used to the items choosed from each combobox
  private String start, firstArrive, arrive, airFirm;

  //Setup GUI in the Constructor method
  public ComprehenQuery() {
    jcbAirFirm.addItem("所有");
    jcbAirFirm.setSelectedItem("所有");

    JPanel jp1 = new JPanel();
    jp1.add(jrbSingle);
    JPanel jp2 = new JPanel();
    jp2.add(jrbDouble);
    JPanel jp3 = new JPanel();
    jp3.add(jrbMutiple);

    JPanel jpRadioButton = new JPanel();
    jpRadioButton.setLayout(new GridLayout(5, 1));
    jpRadioButton.add(new JLabel("       "));
    jpRadioButton.add(jp1);
    jpRadioButton.add(jp2);
    jpRadioButton.add(jp3);
    jpRadioButton.add(new JLabel("       "));

    ButtonGroup bg = new ButtonGroup();
    bg.add(jrbSingle);
    bg.add(jrbDouble);
    bg.add(jrbMutiple);

    //*********************************************************************

    JPanel jpStart = new JPanel(new FlowLayout(FlowLayout.RIGHT));
    jpStart.add(jlStart = new JLabel("        出发城市:"));

    JPanel jpFirstArrive = new JPanel(new FlowLayout(FlowLayout.RIGHT));
    jpFirstArrive.add(jlFirstArrive = new JLabel("                "));

    JPanel jpArrive = new JPanel(new FlowLayout(FlowLayout.RIGHT));
    jpArrive.add(jlArrive = new JLabel("        到达城市:"));

    JPanel jpAirFirm = new JPanel(new FlowLayout(FlowLayout.RIGHT));
    jpAirFirm.add(jlAirFirm = new JLabel("       航空公司:"));

    JPanel jpLabels = new JPanel();
    jpLabels.setLayout(new GridLayout(7, 1));
    jpLabels.add(jpStart);
    jpLabels.add(jpFirstArrive);
    jpLabels.add(jpArrive);

    jpLabels.add(new JLabel("            "));
    jpLabels.add(jpAirFirm);
    jpLabels.add(new JLabel("            "));

    //*********************************************************************

    JPanel jpStartBox = new JPanel(new FlowLayout(FlowLayout.LEFT));
    jpStartBox.add(jcbStart);

    //***********************
     //The combobox "jcbFirstArrive" should be removed from and added into the
     //jpFirstArriveBox dynamically,
     //so create a panel contains an empty label that used to replace
     //the panel that contains the combobox "jcbFirstArrive" when we don't need it
    jpReplaceArrive = new JPanel(new FlowLayout(FlowLayout.LEFT));
    jpReplaceArrive.add(jlReplaceArrive = new JLabel("            "));

    jpArrive1 = new JPanel(new FlowLayout(FlowLayout.LEFT));
    jpArrive1.add(jcbFirstArrive);

    jpFirstArriveBox = new JPanel(new FlowLayout(FlowLayout.LEFT, 0, 0));
    jpFirstArriveBox.add(jpReplaceArrive);
    //***********************

    JPanel jpArriveBox = new JPanel(new FlowLayout(FlowLayout.LEFT));
    jpArriveBox.add(jcbArrive);

    //***********************

    JPanel jpAirFirmBox = new JPanel(new FlowLayout(FlowLayout.LEFT));
    jpAirFirmBox.add(jcbAirFirm);

    JPanel jpButton = new JPanel(new FlowLayout(FlowLayout.LEFT));
    jpButton.add(jbQuery);

    JPanel jpComboBox = new JPanel();
    jpComboBox.setLayout(new GridLayout(7, 1));
    jpComboBox.add(jpStartBox);
    jpComboBox.add(jpFirstArriveBox);
    jpComboBox.add(jpArriveBox);
    jpComboBox.add(new JLabel("            "));
    jpComboBox.add(jpAirFirmBox);
    jpComboBox.add(jpButton);

    //*********************************************************************

    JPanel jpQuery = new JPanel();
    jpQuery.setLayout(new BorderLayout());
    jpQuery.add(jpLabels, BorderLayout.WEST);
    jpQuery.add(jpComboBox, BorderLayout.CENTER);

    JPanel jpDown = new JPanel();
    jpDown.setLayout(new BorderLayout());
    jpDown.add(jpRadioButton, BorderLayout.WEST);
    jpDown.add(jpQuery, BorderLayout.CENTER);

    JLabel jlTitle = new JLabel("综合查询");
    jlTitle.setHorizontalAlignment(JLabel.CENTER);
    jlTitle.setFont(new Font("Tims", Font.BOLD, 23));
    this.setLayout(new BorderLayout());
    this.add(jlTitle, BorderLayout.NORTH);
    this.add(jpDown, BorderLayout.CENTER);
    ;

    //Add listener to the radio buttons
    //RadioListener is an inner class which define below
    jrbSingle.addActionListener(new RadioListener());
    jrbDouble.addActionListener(new RadioListener());
    jrbMutiple.addActionListener(new RadioListener());

    //Add listener to the query button
    jbQuery.addActionListener(this);
    initComboxes();
  }

  private void initComboxes() {
    String sql = "Select DISTINCT start from flight";
    ResultSet rs = dbManager.getResult(sql);
    rs = dbManager.getResult(sql);
    try {
      while (rs.next()) {
        String startCity = rs.getString(1);
        this.jcbStart.addItem(startCity);
      }
    }
    catch (Exception e) {
      e.printStackTrace();
    }

    sql = "Select DISTINCT destination from flight";
    rs = dbManager.getResult(sql);
    try {
      while (rs.next()) {
        String destinationCity = rs.getString(1);
        this.jcbArrive.addItem(destinationCity);
        this.jcbFirstArrive.addItem(destinationCity);
      }
    }
    catch (Exception e) {
      e.printStackTrace();
    }
    sql = "Select DISTINCT airfirm from flight";
    rs = dbManager.getResult(sql);
    try {
      while (rs.next()) {
        String firm = rs.getString(1);
        this.jcbAirFirm.addItem(firm);
      }
    }
    catch (Exception e) {
      e.printStackTrace();
    }

  }

  //An inner class for the JRadioButton listener
  class RadioListener
      implements ActionListener {
    public void actionPerformed(ActionEvent e) {
      //Remove and add components into the framework dynamically
      //according to the query mode you have selected!

      //If you choose the one way query mode
      if (jrbSingle.isSelected()) {
        jlFirstArrive.setText("            ");
        //Remove the present components in the jpFirstArriveBox panel
        jpFirstArriveBox.removeAll();
        //Add the designate component into the panel in the designate query mode
        jpFirstArriveBox.add(jpReplaceArrive);
        //Use the method repaint() so that the component you just add into the panel
        //can be showed immediately
        jpFirstArriveBox.repaint();

        jlArrive.setText("    到达城市:");
      }
      //If you choose the out and home query mode
      else if (jrbDouble.isSelected()) {
        jlFirstArrive.setText("            ");
        jpFirstArriveBox.removeAll();
        jpFirstArriveBox.add(jpReplaceArrive);
        jpFirstArriveBox.repaint();

        jlArrive.setText("    到达城市:");
      }
      //If you choose the mutiple way query mode
      else if (jrbMutiple.isSelected()) {
        jlFirstArrive.setText("第一到达城市:");
        jpFirstArriveBox.removeAll();
        jpFirstArriveBox.add(jpArrive1);
        jpFirstArriveBox.repaint();

        jlArrive.setText("第二到达城市:");
      }
    }
  }

  //The monitor method for the button "jbQuery"
  public void actionPerformed(ActionEvent e) {
    //According to the query mode you choose,the operation is different

    //If you choose the one way query mode
    if (jrbSingle.isSelected()) {
      //Get the start place
      start = (String) jcbStart.getSelectedItem();
      //Trim the space at the side of the string
      start = start.trim();

      //Get the destination
      arrive = (String) jcbArrive.getSelectedItem();
      arrive = arrive.trim();

      //Get which air firm you want to take
      airFirm = (String) jcbAirFirm.getSelectedItem();
      airFirm = airFirm.trim();

      //Do the query work
      executeSingleQuery();
    }
    //If you choose the out and home way query mode
    else if (jrbDouble.isSelected()) {
      start = (String) jcbStart.getSelectedItem();
      start = start.trim();

      arrive = (String) jcbArrive.getSelectedItem();
      arrive = arrive.trim();

      airFirm = (String) jcbAirFirm.getSelectedItem();
      airFirm = airFirm.trim();

      executeDoubleQuery();
    }
    //If you choose the mutiple way query mode
    else if (jrbMutiple.isSelected()) {
      start = (String) jcbStart.getSelectedItem();
      start = start.trim();
      //Get the midway destination
      firstArrive = (String) jcbFirstArrive.getSelectedItem();
      firstArrive = firstArrive.trim();
      //Get the final destination
      arrive = (String) jcbArrive.getSelectedItem();
      arrive = arrive.trim();

      airFirm = (String) jcbAirFirm.getSelectedItem();
      airFirm = airFirm.trim();

      executeMutipleQuery();
    }
  }

  //The query method for the one way query mode
  public void executeSingleQuery() {
    String sqlString = formSQLString(start, arrive);
    ResultSet rs = dbManager.getResult(sqlString);
    showResult("单程综合查询", rs);
  }

  //The query method for the out and home way query mode
  public void executeDoubleQuery() {
    //The out and home way has to query the database two times to find the
    //flight infomation of from start city to destination and from destination to start city
    String sqlString1 = formSQLString(start, arrive);
    ResultSet rs1 = dbManager.getResult(sqlString1);
    DefaultTableModel model1=constructTableModel(rs1);
    String sqlString2 = formSQLString(arrive, start);
    ResultSet rs2 = dbManager.getResult(sqlString2);
    DefaultTableModel model2=constructTableModel(rs2);
    showResult("往返综合查询", model1, model2);
  }




  //The query method for the mutiple way query mode
  public void executeMutipleQuery() {
    //The out and home way has to query the database two times to find the
    //flight infomation of from start city to midway destination
    //and from midway destination to final destination
    String sqlString1 = formSQLString(start, firstArrive);
    ResultSet rs1 = dbManager.getResult(sqlString1);
    DefaultTableModel model1=constructTableModel(rs1);
    String sqlString2 = formSQLString(firstArrive, arrive);
    ResultSet rs2 = dbManager.getResult(sqlString2);
    DefaultTableModel model2=constructTableModel(rs2);
    showResult("联程综合查询", model1, model2);
  }

  public String formSQLString(String begin, String end) {
    String sqlString = "SELECT DISTINCT * FROM " + "flight " +
        "WHERE start=" + "\'" + begin + "\'" + " AND " +
        "destination=" + "\'" + end + "\'";
    if (!airFirm.equals("所有"))
      sqlString += " AND " + "airFirm=" + "\'" + airFirm + "\'";
    return sqlString;
  }

  public void showResult(String title, ResultSet rs) {
    ResultTable table = new ResultTable(title, rs);
    Dimension d = Toolkit.getDefaultToolkit().getScreenSize(); //屏幕大小
    table.setLocation( (d.width - table.getSize().width) / 2,
                      (d.height - table.getSize().height) / 2);
    table.show();
  }

  public void showResult(String title, DefaultTableModel model1, DefaultTableModel model2) {
    ResultTable table = new ResultTable(title, model1, model2);
    Dimension d = Toolkit.getDefaultToolkit().getScreenSize(); //屏幕大小
    table.setLocation( (d.width - table.getSize().width) / 2,
                      (d.height - table.getSize().height) / 2);
    table.show();
  }

  private DefaultTableModel constructTableModel(ResultSet rs1) {
    DefaultTableModel defaultModel=new DefaultTableModel();
    try{
      while (rs1.next()) {
        Vector vector = new Vector();
        vector.addElement(rs1.getString(1));
        vector.addElement(rs1.getString(2));
        vector.addElement(rs1.getString(3));
        vector.addElement(rs1.getString(4));
        vector.addElement(rs1.getString(5));
        vector.addElement(rs1.getString(6));
        vector.addElement(rs1.getString(7));
        vector.addElement(rs1.getString(8));
        vector.addElement(rs1.getString(9));
        vector.addElement(rs1.getString(10));
        defaultModel.addRow(vector);
      }
    }catch(Exception e){
      e.printStackTrace();
    }
    return defaultModel;
  }

} ///:~

⌨️ 快捷键说明

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