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

📄 analyse.java~17~

📁 虚拟飞机订票系统
💻 JAVA~17~
字号:
package manager;

import java.sql.*;
import java.util.*;

//分析客户端提交的请求

public class analyse
{
  public ArrayList analyse(String message)
      throws SQLException
  {
    int i = 0;
    returnList=null;
    try {
      Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
      con = DriverManager.getConnection(
          "jdbc:odbc:Driver={MicroSoft Access Driver (*.mdb)};DBQ=order.mdb");
      sql = con.createStatement();
    }
    catch (Exception ex) {
      System.out.println("连接数据库失败。。。");
    }
    StringTokenizer tokens = new StringTokenizer(message, ",");
    ArrayList list = new ArrayList();
    while (tokens.hasMoreTokens())
      list.add(i++, tokens.nextToken());
    analyse = new String[list.size()];
    for (int j = 0; j < analyse.length; j++)
      analyse[j] = (String) list.get(j);
    if (analyse[0].equals("1")) {
      ManagerFrame.jTextArea1.append("客户动作:服务1\n");
      returnList= checkout(analyse);
    }
    if (analyse[0].equals("2")) {
      ManagerFrame.jTextArea1.append("客户动作:服务2\n");
      returnList= orderReceive(analyse);
    }
    if (analyse[0].equals("3")) {
      ManagerFrame.jTextArea1.append("客户动作:服务3\n");
      returnList= disOrder(analyse);
    }
    if (analyse[0].equals("4")) {
      ManagerFrame.jTextArea1.append("客户动作:服务4\n");
      returnList= disOrderSure(analyse);
    }
    if (analyse[0].equals("5")) {
      ManagerFrame.jTextArea1.append("客户动作:服务5\n");
      returnList= payOrder(analyse);
    }
    if (analyse[0].equals("6")) {
      ManagerFrame.jTextArea1.append("客户动作:服务6\n");
      returnList= payOrderSure(analyse);
    }
    return returnList;
  }

  private ArrayList checkout(String[] analyse)
  {
    int i = 0;
    ArrayList list = new ArrayList();

    String query =
        "SELECT fInfo,s_price,g_price,s_time,a_time FROM information " +
        "WHERE start='" + analyse[1] + "' AND destination='" + analyse[2] +
        "' AND belong="
        + analyse[3];
    try {
      ResultSet rs = sql.executeQuery(query);
      while (rs.next()) {
        String r = rs.getString("fInfo");
        r = r + "," + rs.getInt("s_price");
        r = r + "," + rs.getInt("g_price");
        r = r + "," + rs.getString("s_time");
        r = r + "," + rs.getString("a_time");
        list.add(i++, r);
      }
      con.close();
    }
    catch (SQLException e) {}
    return list;
  }

  private ArrayList orderReceive(String[] analyse)
  {
    int i = 0;
    ArrayList list = new ArrayList();

    try {
      String query;
      if (Integer.parseInt(analyse[4]) >= 3)
        query = "SELECT fId,fInfo,g_price,s_time,a_time FROM information WHERE"
            + " start='" + analyse[1] + "'and destination='" + analyse[2] + "'";
      else
        query = "SELECT fId,fInfo,s_price,s_time,a_time FROM information WHERE"
            + " start='" +
            analyse[1] + "'and destination='" + analyse[2] + "'";

      ResultSet rs = sql.executeQuery(query);
      while (rs.next()) {
        int fId = rs.getInt("fId");
        String fInfo = rs.getString("fInfo");
        String start_time = rs.getString("s_time");
        String arrive_time = rs.getString("a_time");
        int price = 0;
        if (Integer.parseInt(analyse[4]) >= 3)
          price = rs.getInt("g_price");
        else
          price = rs.getInt("s_price");

        //长生随机订单号
        Random random = new Random();
        int oId = random.nextInt(65535);

        //将订单写入数据库
        query =
            "INSERT INTO detail(oId,c_name,sex,c_IdCard,orderDate,dQty,fId,"
            + "fInfo,s_time,a_time,TotalCost) VALUES (" + oId + ",'"
            + analyse[5] + "'," + analyse[6] + ",'" + analyse[7] + "','"
            + analyse[3] + "'," + Integer.parseInt(analyse[4]) + "," +
            fId + ",'" + fInfo + "','" + start_time + "','" + arrive_time +
            "',"
            + price * Integer.parseInt(analyse[4]) + ")";

        int success = sql.executeUpdate(query);

        if (success == 1) {
          list.add(0, "success");
          list.add(1, oId);
          list.add(2, price);
          list.add(3, fInfo);
          list.add(4, start_time);
          list.add(5, arrive_time);
        }
        else
          list.add(0, "fail");
      }
      con.close();
    }
    catch (SQLException e) {}

    return list;
  }

  private ArrayList disOrder(String[] analyse)
  {
    ArrayList list = new ArrayList();

    try {
      String query =
          "SELECT orderDate,dQty,fInfo,s_time,a_time FROM detail WHERE"
          + " c_name='" + analyse[1] + "' and c_IdCard='" + analyse[2]
          + "' and oId=" + analyse[3];

      ResultSet rs = sql.executeQuery(query);
      while (rs.next()) {
        list.add(0, rs.getString("orderDate"));
        list.add(1, rs.getInt("dQty"));
        list.add(2, rs.getString("fInfo"));
        list.add(3, rs.getString("s_time").substring(10, 16));
        list.add(4, rs.getString("a_time").substring(10, 16));
      }
      con.close();
    }
    catch (SQLException e) {}

    if (list.isEmpty())
      list.add(0, "fail");
    return list;
  }

  private ArrayList disOrderSure(String[] analyse)
  {
    ArrayList list = new ArrayList();

    try {
      String query = "DELETE FROM detail WHERE c_name='" + analyse[1] +
          "' and "
          + "c_IdCard='" + analyse[2] + "' and oId=" + analyse[3];

      int sure = sql.executeUpdate(query);
      if (sure == 1)
        list.add(0, "success");
      else
        list.add(0, "fail");
      con.close();
    }
    catch (SQLException e) {}

    if (list.isEmpty())
      list.add(0, "fail");
    return list;
  }

  private ArrayList payOrder(String[] analyse)
  {
    ArrayList list = new ArrayList();

    try {
      String query =
          "SELECT orderDate,dQty,fInfo,totalCost,paid_up FROM detail "
          + "WHERE oId=" + analyse[1];
      ResultSet rs = sql.executeQuery(query);
      while (rs.next()) {
        list.add(0, rs.getString("orderDate"));
        list.add(1, rs.getString("fInfo"));
        list.add(2, rs.getInt("dQty"));
        list.add(3, rs.getInt("totalCost") - rs.getInt("paid_up"));
      }
      if (list.isEmpty())
        list.add(0, "fail");
      con.close();
    }
    catch (SQLException e) {}

    return list;
  }

  private ArrayList payOrderSure(String[] analyse)
  {
    ArrayList list = new ArrayList();
    try {
      String query = "UPDATE detail SET paid_up=" + analyse[2]
          + " WHERE oId=" + analyse[1];
      int success = sql.executeUpdate(query);
      if (success == 1)
        list.add(0, "success");
      else
        list.add(0, "fail");
      con.close();
    }
    catch (SQLException e) {}
    return list;
  }
  private void closeConnection()
  {
    try {
      if(sql!=null)
        sql.close();
      if(con!=null)
        con.close();
    }
    catch (SQLException ex) {
      ex.printStackTrace();
    }
  }
  String[] analyse;
  private Connection con;
  private Statement sql;
  private ArrayList returnList;
}

⌨️ 快捷键说明

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