<%@ page contentType="text/html; charset=gb2312" language="java" import="java.sql.*" errorPage="" %>
<%@ page import="java.util.*" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>站点查询</title>
<style type="text/css">
a,body,tr,td {
	text-align: center;
	font-family: 宋体;
	font-size: 13px
}
</style>
</head>

<body>
<p>
  <%      Connection con;
         ResultSet result1,result2,result3;
		 //定义三个sql对象
		 Statement sql3=null;
		 Statement sql1=null;
		 Statement sql2=null;
		 
		 int i=0,j=0,k=0,h=0,m=0,n=0;
		 //用来存放含有起始站而不含有终止站的车次号和细节
		 String S_BusID[]=new String[50];
		 String S_Detail[]= new String[50];
		 //用来存放含有终止站而不含有起始站的车次号和细节
		 String E_BusID[]=new String[50];
		 String E_Detail[]=new String[50];
		 //用来存储在起始站和终止站在同一车次的车次号和说细信息
		 String Result1[]=new String[50]; 
		 String Detail[]=new String[50];
		 String stations[]=new String[200];
		
		
		 String station1=null;
		 String station2=null;
		 String detail1=null;
		 String detail2=null;
		 
		 String Result2_ID1[]=new String[500];//中转车次一
		 String Result2_ID2[]=new String[500];//中转车次二
		 String Result2_tan[]=new String[500];//中转站
		 int count=0;
		 //获取表单信息(起始站和终止站)
		 String S_Station=new String(request.getParameter("S_query3").getBytes("ISO8859_1"),"GBK");
		 String E_Station=new String(request.getParameter("E_query3").getBytes("ISO8859_1"),"GBK");
		 
		 try{ Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
		      }
			 catch(ClassNotFoundException e){  e.printStackTrace(); } 
			try{   //加载数据源
			       con=DriverManager.getConnection("jdbc:odbc:nbos","",""); 
				   sql1=con.createStatement(); 
				   //从数据库中找出所有含有起始站和终止站的数据
			       String condition1="select * from bus where Detail LIKE '%"+S_Station+"%"+E_Station+"%' OR Detail LIKE '%"+E_Station+"%"+S_Station+"%' order by Sort ASC";
				   result1=sql1.executeQuery(condition1);
				    //把结果放在数组Result1[i]中
				   while(result1.next())
				     { 
				       Result1[i]=result1.getString("BusID");
					   Detail[i]=result1.getString("Detail");
					   
					   i++;
					   
					 } 
					 result1.close();//关闭第一个结果集
		          if(i>0)		 
				   
				    { 
					 //当起始站到终止站在同一车次时,输出以下表格
				      %>
</p>
<TABLE cellSpacing=1 cellPadding=3 width=650 align=center 
border=0>
  <TBODY>
    <TR>
         </TR>
    <TR>
      <TD><STRONG><FONT color=#000000>站站查询结果</FONT></STRONG></TD>
    </TR>
    <TR >
      <TD><!--查询结果显示开始-->
          <TABLE width=500 height="84" border=0 cellPadding=3 cellSpacing=1 bgColor=#003399>
            <TBODY>
              <TR bgColor=#ffffff>
                <TD height="19" colspan="2"><!--查询结果显示开始-->
                  <p><font color="000000"><%=S_Station%></font>到<font color="FF0000"><%=E_Station%></font>有直达车，请参考以下车次！</p></TD>
              </TR>
              <TR bgColor=#ffffff>
                <TD width=73 height="19"><strong>车次号</strong></TD>
                <TD width="412"><strong>经过的所有站点</strong></TD>
              </TR>
              <% for(int mark=0;mark<i;mark++)
	            {   
			     StringTokenizer str1=new StringTokenizer(Detail[mark],"->");
			     count=0;
		         while(str1.hasMoreTokens())
		          { stations[count]=str1.nextToken();
			         count++;
			      }//endwhile
			  
			 
			   %>
              <TR onmouseover="this.style.backgroundColor = '#ffffff'" 
        onmouseout="this.style.backgroundColor = ''" bgColor=#ffffff>
                <TD height="42"><%=Result1[mark]%></TD>
                <TD>
                  <div align="left">
                    <%   
		  for(int countstation=0;countstation<count;countstation++)
		    {  if(stations[countstation].indexOf(S_Station)!=-1)
		         {  %>
                    <font color="#000000"><%=stations[countstation]%> &nbsp;</font>
                    <% }//endif
				  else  
				     if(stations[countstation].indexOf(E_Station)!=-1)
		               {  %>
                          <font color="#000000"><%=stations[countstation]%> &nbsp;</font>
                        <% }//endif
				  
				     else    { %>
                         <font size="2"><%=stations[countstation]%>&nbsp;</font>
                      <% 
                  }//endelse
		 }//endfor			
			  
				 
			  	
		  %>
                </div></TD>
              </TR>
             
       <%      } //end for(int m=0;m<count;m++)
	          //end else
		
		 
	    
		
		%>
            </TBODY>
          </TABLE>
          <!--查询结果显示结束-->
          <BR>
          <input type=button name="close3" value="关闭窗口"
      width="78" height="21" border=0 
       onclick=window.close()> </TD>
    </TR>
    <TR>
        </TR>
  </TBODY>
</TABLE>
<base target="_blank">
<%     } else   //当起始站等于终止站,且没有回路时,跳转至错误提示页面
              if(S_Station.equals(E_Station))
                        { response.sendRedirect("errorquery3.jsp");
						   }
						   else{  
				//查询所有包含有起始站，而不包含终点站的车次信息		   
				   sql2=con.createStatement(); 
				   String condition2="select * from bus where Detail LIKE '%"+S_Station+"%' AND Detail NOT LIKE '%"+E_Station+"%'";
				   result2=sql2.executeQuery(condition2);
				   while(result2.next())
				     { 
				       S_BusID[j]=result2.getString("BusID");
				
					   S_Detail[j]=result2.getString("Detail"); 
					   j++;
					 } 
					 result2.close();//关闭第二个结果集
				//查询所有包含有终点站，不包含起始站的车次信息	 
				   sql3=con.createStatement(); 
				   String condition3="select * from bus where Detail LIKE '%"+E_Station+"%' AND Detail NOT LIKE '%"+S_Station+"%'";
				   result3=sql3.executeQuery(condition3);
				   while(result3.next())
				     { 
				       E_BusID[h]=result3.getString("BusID");
					   E_Detail[h]=result3.getString("Detail"); 
					   h++;
					 } 
					 result3.close();//关系第三个结果集
			  //把要转车的信息放Result2系列的数组 
			  for(k=0;k<j;k++)
		        { //初始临时变量,对S_Detail[]中的每一个元素进行比较
				 detail1=S_Detail[k];
				    //把detail1用'->'符号区分
                    StringTokenizer str1=new StringTokenizer(detail1,"->");
			      //对S_Detail[]中的每一个元素的每一个站点进行比较
				  while(str1.hasMoreTokens())
			       {  
			    	 station1 = str1.nextToken();//初始临时变量
				     //取出E_Detail[]数组中的每一个元素
					 for(n=0;n<h;n++)
				      {  detail2=E_Detail[n];//定义临时变量
					     StringTokenizer str2=new StringTokenizer(detail2,"->");
					     //取出E_Detail[]数组中的每一个元素的每一个站点
					     while(str2.hasMoreTokens())
						   {  station2 = str2.nextToken();
						      //把结果存储到结果集中
							  if(station1.equals(station2))
							  {   
					           Result2_ID1[count]= S_BusID[k];  
						       Result2_tan[count]= station1; 
						       Result2_ID2[count]= E_BusID[n];
				               count++;
						     
					           }  //endif  
				 
		                    }//endwhile
					    }//endfor
				     }//endwhile
				 
				  }//endfor
                  //输出转乘车次1,转乘车次2,以及中转站信息   
				if(count>0)
				     {  
					    %>
<table width="650" border="0"" cellpadding="3" cellspacing="1" align="center">
  <tr> 
    <td height="20" ></td>
  </tr>
  <tr> 
    <td><strong><font color="#000000">站点查询结果</font></strong></td>
  </tr>
  <tr > 
    <td>
	<!--查询结果显示开始-->
<strong></strong> <font color="000000"><%=S_Station%></font>到  <font color="000000"><%=E_Station%></font>没有直达车!请参照下列换乘建议.<br>
<table width="600" border="0" cellpadding="3" cellspacing="1"><tr><td width="150"><strong>起点</strong></td><td width="70"><strong>车次</strong></td><td width="160"><strong>中转站</strong></td><td width="70"><strong>车次</strong></td><td width="150"><strong>终点</strong></td></tr>
 <%  //循环输出结果集
       for(k=0;k<count;k++)
	   {   %>
<tr onMouseOver = "this.style.backgroundColor = '#FFF9EE'" onMouseOut = "this.style.backgroundColor = ''"><td width="150"><a href="resultquery2.jsp?query2=<%=S_Station%>" target="_blank"><%=S_Station%></a></td>
  <td width="70"><a href="resultquery1.jsp?query1=<%=Result2_ID1[k]%>" target="_blank"><%=Result2_ID1[k]%></a></td><td width="160"> 
	-&gt;<a href="resultquery2.jsp?query2=<%=Result2_tan[k]%>" target="_blank"><%=Result2_tan[k]%></a> 
	-&gt; </td>
  <td width="70"><a href="resultquery1.jsp?query1=<%=Result2_ID2[k]%>" target="_blank"><%=Result2_ID2[k]%></a></td><td width="150"><a href="resultquery2.jsp?query2=<%=E_Station%>" target="_blank"><%=E_Station%></a></td>
  </tr> <%  }   //endfor %>
  
  </table>	
	<!--查询结果显示结束-->
	<br>       <input type=button name="close4" value="关闭窗口"
      width="78" height="21" border=0 
       onclick=window.close()>	</td>
  </tr>
  <tr> 

  </tr>
</table>
<% } else 
		//假如即没有直达车,转车两次也无法到达,输出以下信息
		{		   %>
<table width="650" border="0"  cellpadding="3" cellspacing="1" align="center">

  <tr >
    <td><strong><font color="#000000">站点查询结果</font></strong></td>
  </tr>
  <tr >
    <td>
      <!--查询结果显示开始-->
      <font color="000000"><%=S_Station%></font>到 <strong></strong> <font color="000000"><%=E_Station%></font>没有直达车!请参照下列换乘建议.<br>
      <br>      抱歉，<strong></strong> <font color="000000"><%=S_Station%></font>到<font color="000000"><%=E_Station%></font> <strong></strong>没有直达车,换乘一次也无法到达!
      <!--查询结果显示结束-->
      <br>
             <input type=button name="close5" value="关闭窗口"
      width="78" height="21" border=0 
       onclick=window.close()> </td>
  </tr>
 
</table>
<p>　</p>
<%  }  	
		 } //endelse  
		
		
		 }	//endtry   
				catch(Exception e){ 
					 e.printStackTrace();
				}   
				   
				%>		
</body>
</html>