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

📄 comprehenquery.java~23~

📁 航班查询与订票系统 用Java与sqlserver2000来编写一个航班查询与订票系统
💻 JAVA~23~
字号:
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);
  private JLabel jlStart, jlFirstArrive, jlArrive, jlAirFirm, jlReplaceArrive;
  private JPanel jpFirstArriveBox, jpReplaceArrive, jpArrive1;
  private JButton jbQuery = new JButton("查询");

  private String start, firstArrive, arrive, airFirm;

  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);

    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);


    jrbSingle.addActionListener(new RadioListener());
    jrbDouble.addActionListener(new RadioListener());
    jrbMutiple.addActionListener(new RadioListener());

    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();
    }

  }

  class RadioListener
      implements ActionListener {
    public void actionPerformed(ActionEvent e) {
      if (jrbSingle.isSelected()) {
        jlFirstArrive.setText("            ");
        jpFirstArriveBox.removeAll();
        jpFirstArriveBox.add(jpReplaceArrive);
        jpFirstArriveBox.repaint();
        jlArrive.setText("    到达城市:");
      }
      else if (jrbDouble.isSelected()) {
        jlFirstArrive.setText("            ");
        jpFirstArriveBox.removeAll();
        jpFirstArriveBox.add(jpReplaceArrive);
        jpFirstArriveBox.repaint();

        jlArrive.setText("    到达城市:");
      }
      else if (jrbMutiple.isSelected()) {
        jlFirstArrive.setText("第一到达城市:");
        jpFirstArriveBox.removeAll();
        jpFirstArriveBox.add(jpArrive1);
        jpFirstArriveBox.repaint();
        jlArrive.setText("第二到达城市:");
      }
    }
  }

  public void actionPerformed(ActionEvent e) {
    if (jrbSingle.isSelected()) {
      start = (String) jcbStart.getSelectedItem();
      start = start.trim();

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

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

      executeSingleQuery();
    }
    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();
    }
    else if (jrbMutiple.isSelected()) {
      start = (String) jcbStart.getSelectedItem();
      start = start.trim();

      firstArrive = (String) jcbFirstArrive.getSelectedItem();
      firstArrive = firstArrive.trim();

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

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

      executeMutipleQuery();
    }
  }

  public void executeSingleQuery() {
    String sqlString = formSQLString(start, arrive);
    ResultSet rs = dbManager.getResult(sqlString);
    showResult("单程综合查询", rs);
  }

  public void executeDoubleQuery() {
    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);
  }

  public void executeMutipleQuery() {
    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) {
    String[] name = {
        "航班号", "公司", "起飞地", "目的地", "起飞时间", "到达时间", "儿童票价", "成人票价", "折扣", "退票率"};
    String[][] data = new String[0][0];
    DefaultTableModel defaultModel = new DefaultTableModel(data, name);

    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 + -