小组开发地铁项目

环境:Eclipse

数据库:六条线的站点名分别录入六个表,huancheng那个表是换乘需要的条件。

 

 

 

 

servlet:

package com.sub.servlet;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.sub.entity.subway;
import com.sub.dao.Dao;
/**
 * Servlet implementation class Servlet
 */
@WebServlet("/Servlet")
public class Servlet extends HttpServlet {
 private static final long serialVersionUID = 1L;
 Dao dao=new Dao();
 protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  request.setCharacterEncoding("utf-8");
  String method = request.getParameter("method");
  
  if ("chaxun".equals(method)) {
   chaxun(request, response);
  }
 }
 private void chaxun(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
 String start=request.getParameter("start");
 String end=request.getParameter("end");
 int a[]=new int[2];
 int b[]=new int[2];
 int k[]=new int[4];
 a=dao.getLineshuzu(start);
 b=dao.getLineshuzu(end);
 if((dao.site(start)||dao.site2(start)||dao.site3(start)||dao.site4(start)||dao.site5(start)||dao.site6(start))&&((dao.site(end)||dao.site2(end)||dao.site3(end)||dao.site4(end)||dao.site5(end)||dao.site6(end))))
 {
 if(((dao.getSum(start)>1&&dao.getSum(end)==1)||(dao.getSum(start)==1&&dao.getSum(end)>1)||(dao.getSum(start)>1&&dao.getSum(end)>1))&&((a[0]!=b[0])&&(a[0]!=b[1])&&(a[1]!=b[0])&&(a[1]!=b[1])))
 {
  String route="";
  int []temp=new int[4];
  int temp1=0;
  temp[0]=0;temp[1]=1;temp[2]=2;temp[3]=3;
  k[0]=a[0];k[1]=a[1];k[2]=b[0];k[3]=b[1];
  System.out.println(k[0]+","+k[1]+","+k[2]+","+k[3]);
  String a1=dao.getdouble(k[0], k[2]);
  String b1=dao.getdouble(k[0], k[3]);
  String c1=dao.getdouble(k[1], k[2]);
  String d1=dao.getdouble(k[1], k[3]);
  System.out.println(a1+","+b1+","+c1+","+d1);
  if(k[0]!=0&&k[2]!=0)
  {if(temp[temp1]==0)
  {
   System.out.println("");
   System.out.println("----------------线路Ⅰ:----------------");
   route=route+"-----线路Ⅰ:";
  }
  if(temp[temp1]==1)
  {
   System.out.println("");
   System.out.println("----------------线路Ⅱ:----------------");
   route=route+"-----线路Ⅱ:";
  }
  if(temp[temp1]==2)
  {
   System.out.println("");
   System.out.println("----------------线路Ⅲ:----------------");
   route=route+"-----线路Ⅲ:";
  }
  if(temp[temp1]==3)
  {
   System.out.println("");
   System.out.println("----------------线路Ⅳ:----------------");
   route=route+"-----线路Ⅳ:";
  }
  route=route+dao.getdoublesite(k[0], k[2], start, end);
  temp1++;
  }//0-2
  if(k[0]!=0&&k[3]!=0)
  {if(temp[temp1]==0)
  {
   System.out.println("");
   System.out.println("----------------线路Ⅰ:----------------");
   route=route+"-----线路Ⅰ:";
  }
  if(temp[temp1]==1)
  {
   System.out.println("");
   System.out.println("----------------线路Ⅱ:----------------");
   route=route+"-----线路Ⅱ:";
  }
  if(temp[temp1]==2)
  {
   System.out.println("");
   System.out.println("----------------线路Ⅲ:----------------");
   route=route+"-----线路Ⅲ:";
  }
  if(temp[temp1]==3)
  {
   System.out.println("");
   System.out.println("----------------线路Ⅳ:----------------");
   route=route+"-----线路Ⅳ:";
  }
  route=route+dao.getdoublesite(k[0], k[3], start, end);
  temp1++;
  }//0-3
  if(k[1]!=0&&k[2]!=0)
  {if(temp[temp1]==0)
  {
   System.out.println("");
   System.out.println("----------------线路Ⅰ:----------------");
   route=route+"-----线路Ⅰ:";
  }
  if(temp[temp1]==1)
  {
   System.out.println("");
   System.out.println("----------------线路Ⅱ:----------------");
   route=route+"-----线路Ⅱ:";
  }
  if(temp[temp1]==2)
  {
   System.out.println("");
   System.out.println("----------------线路Ⅲ:----------------");
   route=route+"-----线路Ⅲ:";
  }
  if(temp[temp1]==3)
  {
   System.out.println("");
   System.out.println("----------------线路Ⅳ:----------------");
   route=route+"-----线路Ⅳ:";
  }
  route=route+dao.getdoublesite(k[1], k[2], start, end);
  temp1++;
  }//1-2
  if(k[1]!=0&&k[3]!=0)
  {if(temp[temp1]==0)
  {  
   System.out.println("");
   System.out.println("----------------线路Ⅰ:----------------");
   route=route+"-----线路Ⅰ:";
  }
  if(temp[temp1]==1)
  {
   System.out.println("");
   System.out.println("----------------线路Ⅱ:----------------");
   route=route+"-----线路Ⅱ:";
  }
  if(temp[temp1]==2)
  {
   System.out.println("");
   System.out.println("----------------线路Ⅲ:----------------");
   route=route+"-----线路Ⅲ:";
  }
  if(temp[temp1]==3)
  {
   System.out.println("");
   System.out.println("----------------线路Ⅳ:----------------");
   route=route+"-----线路Ⅳ:";
  }
  route=route+dao.getdoublesite(k[1], k[3], start, end);
  temp1++;
  }//1-3
  request.setAttribute("data",route );
  request.getRequestDispatcher("Query1.jsp").forward(request, response);
 }
 if((dao.getSum(start)>1||dao.getSum(end)>1)&&((a[0]==b[0])||(a[0]==b[1])||(a[1]==b[0])||(a[1]==b[1])))
 {
  String route="";
  System.out.println(a[0]+","+a[1]+","+b[0]+","+b[1]);
  if(a[0]==b[0])
  {
   route=dao.getjkl(a[0], start, end);
  }
  if(a[0]==b[1])
  {
   route=dao.getjkl(a[0], start, end);
  }
  if(a[1]==b[0])
  {
   route=dao.getjkl(a[1], start, end);
  }
  if(a[1]==b[1])
  {
   route=dao.getjkl(a[1], start, end);
  }
  request.setAttribute("data",route );
  request.getRequestDispatcher("Query1.jsp").forward(request, response);
 }
 if(dao.site(start)&&dao.site(end)&&(dao.getLine(start)==1)&&(dao.getLine(end)==1)&&dao.getSum(start)==1&&dao.getSum(end)==1)
 {
  int num1=dao.getNum(start);
  int num2=dao.getNum(end);
  if(num1<=num2)
  {
  String line1=dao.getSiter1(num1, num2);
  System.out.println("路线为:"+line1);
  String route="路线为:"+line1;
  request.setAttribute("data",route );
  request.getRequestDispatcher("Query1.jsp").forward(request, response);
  }
  if(num1>num2)
  {
   String line2=dao.getSitek1(num1, num2);
   System.out.println("路线为:"+line2);
   String route="路线为:"+line2;
   request.setAttribute("data",route );
   request.getRequestDispatcher("Query1.jsp").forward(request, response);
  }
 }//两站都在一号线
 if(dao.site2(start)&&dao.site2(end)&&(dao.getLine(start)==2)&&(dao.getLine(end)==2)&&dao.getSum(start)==1&&dao.getSum(end)==1)
 {
  int num1=dao.getNum2(start);
  int num2=dao.getNum2(end);
  if(num1<=num2)
  {
  String line1=dao.getSiter2(num1, num2);
  System.out.println("路线为:"+line1);
  String route="路线为:"+line1;
  request.setAttribute("data",route);
  request.getRequestDispatcher("Query1.jsp").forward(request, response);
  }
  if(num1>num2)
  {
   String line2=dao.getSitek2(num1, num2);
   System.out.println("路线为:"+line2);
   String route="路线为:"+line2;
   request.setAttribute("data",route );
   request.getRequestDispatcher("Query1.jsp").forward(request, response);
  }
 }//两站都在二号线
 if(dao.site3(start)&&dao.site3(end)&&(dao.getLine(start)==3)&&(dao.getLine(end)==3)&&dao.getSum(start)==1&&dao.getSum(end)==1)
 {
  int num1=dao.getNum3(start);
  int num2=dao.getNum3(end);
  if(num1<=num2)
  {
  String line1=dao.getSiter3(num1, num2);
  System.out.println("路线为:"+line1);
  String route="路线为:"+line1;
  request.setAttribute("data",route );
  request.getRequestDispatcher("Query1.jsp").forward(request, response);
  }
  if(num1>num2)
  {
   String line2=dao.getSitek3(num1, num2);
   System.out.println("路线为:"+line2);
   String route="路线为:"+line2;
   request.setAttribute("data",route );
   request.getRequestDispatcher("Query1.jsp").forward(request, response);
  }
 }//两站都在三号线
 if(dao.site4(start)&&dao.site4(end)&&(dao.getLine(start)==4)&&(dao.getLine(end)==4)&&dao.getSum(start)==1&&dao.getSum(end)==1)
 {
  int num1=dao.getNum4(start);
  int num2=dao.getNum4(end);
  if(num1<=num2)
  {
  String line1=dao.getSiter4(num1, num2);
  System.out.println("路线为:"+line1);
  String route="路线为:"+line1;
  request.setAttribute("data",route );
  request.getRequestDispatcher("Query1.jsp").forward(request, response);
  }
  if(num1>num2)
  {
   String line2=dao.getSitek4(num1, num2);
   System.out.println("路线为:"+line2);
   String route="路线为:"+line2;
   request.setAttribute("data",route );
   request.getRequestDispatcher("Query1.jsp").forward(request, response);
  }
 }//两站都在四号线
 if(dao.site5(start)&&dao.site5(end)&&(dao.getLine(start)==5)&&(dao.getLine(end)==5)&&dao.getSum(start)==1&&dao.getSum(end)==1)
 {
  int num1=dao.getNum5(start);
  int num2=dao.getNum5(end);
  if(num1<=num2)
  {
  String line1=dao.getSiter5(num1, num2);
  System.out.println("路线为:"+line1);
  String route="路线为:"+line1;
  request.setAttribute("data",route );
  request.getRequestDispatcher("Query1.jsp").forward(request, response);
  }
  if(num1>num2)
  {
   String line2=dao.getSitek5(num1, num2);
   System.out.println("路线为:"+line2);
   String route="路线为:"+line2;
   request.setAttribute("data",route );
   request.getRequestDispatcher("Query1.jsp").forward(request, response);
  }
 }//两站都在五号线
 if(dao.site6(start)&&dao.site6(end)&&(dao.getLine(start)==6)&&(dao.getLine(end)==6)&&dao.getSum(start)==1&&dao.getSum(end)==1)
 {
  int num1=dao.getNum6(start);
  int num2=dao.getNum6(end);
  if(num1<=num2)
  {
  String line1=dao.getSiter6(num1, num2);
  System.out.println("路线为:"+line1);
  String route="路线为:"+line1;
  request.setAttribute("data",route );
  request.getRequestDispatcher("Query1.jsp").forward(request, response);
  }
  if(num1>num2)
  {
   String line2=dao.getSitek6(num1, num2);
   System.out.println("路线为:"+line2);
   String route="路线为:"+line2;
   request.setAttribute("data",route );
   request.getRequestDispatcher("Query1.jsp").forward(request, response);
  }
 }//两站都在六号线
 if(a[1]==0&&b[1]==0&&a[0]!=b[0]&&dao.getSum(start)==1&&dao.getSum(end)==1)//所有换乘(不包括任意换乘点)
 {
  String route="";
        int []d=new int[4];
  String []c=new String[2];
  System.out.println(a[0]+","+b[0]);
  c=dao.getgline(a, b);
  d=dao.getviewnum(a, b, c, start, end);
     route=dao.getview(a, b, d[0], d[2],d[1],d[3]);
     if(c[1]!="")
     {
      String route1="";
      System.out.println("");
      System.out.print("第二条路线:");
      c=dao.getgline(a, b);
   d=dao.getviewnum1(a, b, c, start, end);
   route=route+"**********";
      route1=dao.getview(a, b, d[0], d[2],d[1],d[3]);
      route=route+"第二条路线:"+route1;
     }
     request.setAttribute("data",route );
  request.getRequestDispatcher("Query1.jsp").forward(request, response);
 }
 }
 else
 {
   request.setAttribute("data","输入站点有误!" );
   request.getRequestDispatcher("Query1.jsp").forward(request, response);
 }
 }
 }
dao:
都是函数,好几千行。。。。
工具类:
package com.sub.DBUtil;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class DBUtil {
 public static String db_url = "jdbc:mysql://localhost:3306/railway";
 public static String db_user = "root";
 public static String db_pass = "893225523";
 
 public static Connection getConn () {
  Connection conn = null;
  
  try {
   Class.forName("com.mysql.jdbc.Driver");//加载驱动
   conn = DriverManager.getConnection(db_url, db_user, db_pass);
  } catch (Exception e) {
   e.printStackTrace();
  }
  
  return conn;
 }
 
 /**
  * 关闭连接
  * @param state
  * @param conn
  */
 public static void close (Statement state, Connection conn) {
  if (state != null) {
   try {
    state.close();
   } catch (SQLException e) {
    e.printStackTrace();
   }
  }
  
  if (conn != null) {
   try {
    conn.close();
   } catch (SQLException e) {
    e.printStackTrace();
   }
  }
 }
 
 public static void close (ResultSet rs, Statement state, Connection conn) {
  if (rs != null) {
   try {
    rs.close();
   } catch (SQLException e) {
    e.printStackTrace();
   }
  }
  
  if (state != null) {
   try {
    state.close();
   } catch (SQLException e) {
    e.printStackTrace();
   }
  }
  
  if (conn != null) {
   try {
    conn.close();
   } catch (SQLException e) {
    e.printStackTrace();
   }
  }
 }
}
posted @ 2019-03-28 21:06  Double晨  阅读(204)  评论(0编辑  收藏  举报