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

📄 commonquery.java~17~

📁 航班查询与订票系统 用Java与sqlserver2000来编写一个航班查询与订票系统
💻 JAVA~17~
字号:
package flight.query;

import flight.assist.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;
import java.util.*;
import java.sql.*;

public class CommonQuery
    extends JPanel
    implements ActionListener, ItemListener {
  DataBaseManager dbManager = DataBaseManager.getInstance();

  private JTextField flightField = new JTextField(8);

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

  //********************************************************************
  private JButton jbAirFirmQuery = new JButton("查询");

 //********************************************************************
  private JComboBox jcbFlight = new JComboBox();
  private JComboBox jcbAirFirm = new JComboBox();
  private JComboBox jcbDestination = new JComboBox();
  private JComboBox jcbStart = new JComboBox();
  private JComboBox jcbArrive = new JComboBox();

  private JButton jbPlaceQuery1 = new JButton("查询"),
      jbPlaceQuery2 = new JButton("查询");

  //Used to store the flightNumber you have choosed
  private String flightNumber;

  //Used to store the item you choosed inth combobox
  private String airfirm;

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

  public CommonQuery() {
    JPanel jpFlight = new JPanel();
    jpFlight.setBorder(new TitledBorder("按航班号查询"));
    jpFlight.add(new JLabel("请输入航班号或从列表选择:"));
    jpFlight.add(flightField);
    jcbFlight.setSelectedItem(null);
    jpFlight.add(jcbFlight);
    jpFlight.add(jbFlightQuery);

    JPanel jpAirFirm = new JPanel();
    jpAirFirm.setBorder(new TitledBorder("按航空公司查询"));
    jpAirFirm.add(new JLabel("请选择你想要查询的航空公司名称:"));
    jpAirFirm.add(jcbAirFirm);
    jpAirFirm.add(jbAirFirmQuery);

    JPanel jp1 = new JPanel();
    jp1.add(new JLabel("请选择你想要到达的目的地:"));
    jp1.add(jcbDestination);
    jp1.add(new JLabel("    "));
    jp1.add(jbPlaceQuery1);

    JPanel jpPlace1 = new JPanel();
    jpPlace1.setLayout(new BorderLayout());
    jpPlace1.add(new JLabel("查询方法一:"), BorderLayout.NORTH);
    jpPlace1.add(jp1, BorderLayout.CENTER);

    JPanel jp2 = new JPanel();
    jp2.add(new JLabel("出发城市:"));
    jp2.add(jcbStart);
    jp2.add(new JLabel("抵达城市:"));
    jp2.add(jcbArrive);
    jp2.add(jbPlaceQuery2);

    JPanel jpPlace2 = new JPanel();
    jpPlace2.setLayout(new BorderLayout());
    jpPlace2.add(new JLabel("查询方法二:"), BorderLayout.NORTH);
    jpPlace2.add(new JLabel("请选择起始城市和抵达城市进行查询:"), BorderLayout.CENTER);
    jpPlace2.add(jp2, BorderLayout.SOUTH);

    JPanel jpDestin = new JPanel();
    jpDestin.setBorder(new TitledBorder("按目的地查询"));
    jpDestin.setLayout(new BorderLayout());
    jpDestin.add(jpPlace1, BorderLayout.NORTH);
    jpDestin.add(jpPlace2, BorderLayout.CENTER);

    this.setLayout(new BorderLayout());
    this.add(jpFlight, BorderLayout.NORTH);
    this.add(jpAirFirm, BorderLayout.CENTER);
    this.add(jpDestin, BorderLayout.SOUTH);

    jcbFlight.addItemListener(this);

    jbFlightQuery.addActionListener(this);
    jbAirFirmQuery.addActionListener(this);
    jbPlaceQuery1.addActionListener(this);
    jbPlaceQuery2.addActionListener(this);
    initComboxes();

  }

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

    sql="Select DISTINCT start from flight";
    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.jcbDestination.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();
    }

  }


  public void itemStateChanged(ItemEvent e) {
    if (e.getSource() == jcbFlight) {
      flightField.setText( (String) jcbFlight.getSelectedItem());
    }
  }

  public void actionPerformed(ActionEvent e) {
    if (e.getSource() == jbFlightQuery) {
      try{
        this.flightNumber = flightField.getText().trim();

        //Haven't enter anything
        if (flightNumber.length() == 0) {
          JOptionPane.showMessageDialog(null, "请输入航班号或者从列表中选择",
                                        "错误信息", JOptionPane.ERROR_MESSAGE);
          return;
        }
        //Do the query work
        executeFlightQuery();
      }catch(Exception ee){
        ee.printStackTrace();
        System.out.println("###########"+ee);
      }
    }

    else if (e.getSource() == jbAirFirmQuery) {
      this.airfirm = (String) jcbAirFirm.getSelectedItem();

      //Do the query work
      executeAirFirmQuery();
    }

    else if (e.getSource() == jbPlaceQuery1) {
      //Get the destination you want to query
      destination = (String) jcbDestination.getSelectedItem();
      //Trim the space at the side of the string
      destination = destination.trim();

      //Do the query work,and diliver the flag=1,means that
      //it is for the first kind of query
      executeDestinQuery(1);
    }

    else if (e.getSource() == jbPlaceQuery2) {
      //Get the start place
      start = (String) jcbStart.getSelectedItem();
      start = start.trim();
      //Get the destionation
      arrive = (String) jcbArrive.getSelectedItem();
      arrive = arrive.trim();

      //Do the query work,and diliver the flag=2,means that
      //it is for the second kind of query
      executeDestinQuery(2);
    }
  }

  public void executeFlightQuery() {
    //Form the sqlString
    String sqlString = "SELECT DISTINCT * FROM " +
        "flight " +
        "WHERE flight=" + "\'" + flightNumber + "\'";

    ResultSet rs = dbManager.getResult(sqlString);

    if (rs != null) {
      //Form result string
      showResult("按航班查询",rs);
    }
    else
      JOptionPane.showMessageDialog(null, "没有连接上数据库!",
                                    "错误信息", JOptionPane.ERROR_MESSAGE);
  }

  public void executeAirFirmQuery() {
    //Form the sqlString
    String sqlString = "SELECT DISTINCT * FROM " +
        "flight " +
        "WHERE airfirm=" + "\'" + airfirm + "\'";

    ResultSet rs = dbManager.getResult(sqlString);

    if (rs != null) {
      showResult("按航空公司查询",rs);
    }
    else
      JOptionPane.showMessageDialog(null, "没有连接上数据库!",
                                    "错误信息", JOptionPane.ERROR_MESSAGE);
  }

  public void executeDestinQuery(int flag) {
    //Form the SQL string
    String sqlString = "SELECT DISTINCT * FROM " + "flight ";

    //The SQL string is different for the two kinds of query
    if (flag == 1)
      sqlString += "WHERE destination=" + "\'" + destination + "\'";
    else
      sqlString += "WHERE start=" + "\'" + start + "\'" + " AND " +
          "destination=" + "\'" + arrive + "\'";

     ResultSet rs = dbManager.getResult(sqlString);

    if (rs != null) {
      showResult("按目的地查询",rs);
    }

    else
      JOptionPane.showMessageDialog(null, "没有连接上数据库!",
                                    "错误信息", JOptionPane.ERROR_MESSAGE);
  }



  //Show the result in a table
  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();

  }
}

⌨️ 快捷键说明

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