石家庄地铁线路查询系统
石家莊地鐵線路查詢系統開發:
合作人:張旺
數據庫的設計為:建立了一張表,有line_id(路線號)、stop_id(站號)、stop_name(站名)、change(某站可換乘的線號)這幾列。
stop_id所顯示的序號首位也可代表line_id,后兩位為該站在其所在線路上的一個順序排序序號,可以體現其位置。
設計思想:
將所有的站點可分為兩類:一種是只在一條線路上(普通點),一種是可在兩條線路上,即為兩條線路交點(換乘點)。
? ? ? ? 所以可以分為3種情況:
?、?#xff1a;起始點:普通點? ?終點:普通點
?、?#xff1a;起始點:普通點? ?終點:換乘點? ? ? 或? ? ?起始點:換乘點? ?終點:普通點
?、?#xff1a;起始點:換乘點? ? 終點:換乘點
? ? ? ? 以上每種情況都可以分為以下幾種:直達,一次換乘,二次換乘,多次換乘(此種情況很少,所以此次試驗中并未考慮)。直達:通過得到的起始站和終點站的兩個站名,獲取到它們的stop_id,然后查詢到兩站之間的站名存入數組輸出即可。一次換乘:找到起始站和終點站所在線路的交點,然后轉化成求兩段直達線路。二次換乘,找到起始站兩側最近的中轉站A和B(若兩側都存在都要考慮),以A為例,計算起始站與A的直達線路,再計算A與終點站的一次換乘。
已實現的功能:
普通點與普通點、普通點與換乘點、換乘點A與換乘點B(A與B分別所在的兩條線路號之間沒有交集)之間的線路查詢。
待實現:
? ? ? ? ? 中轉站A與中轉站B(A與B分別所在的兩條線路號之間有交集)之間的線路查詢,以及各種線路查詢后結果中有起始站、終點站、及中轉站所在線路的標注,和系統主頁面的設計。
已實現功能代碼如下:
1 public List<String> search01(String stop_name01,String stop_name02) 2 { 3 String sql01="select stop_id from stop where stop_name='"+stop_name01+"'";//查詢起點站的id 4 String sql02="select stop_id from stop where stop_name='"+stop_name02+"'";//查詢終點站的id 5 Connection conn=mysql.getconn();//連接數據庫 6 List<String> list01=new ArrayList<>(); 7 Statement s=null; 8 ResultSet rs=null; 9 subway subway=null; 10 int stop_id01 = 0,stop_id02=0; 11 try { 12 s=conn.createStatement(); 13 rs=s.executeQuery(sql01); 14 //得到站號 15 while(rs.next()) { 16 stop_id01=rs.getInt("stop_id"); 17 list01.add(String.valueOf(stop_id01)); 18 } 19 if(list01.size()==1) 20 { 21 list01.add("0"); 22 } 23 //list01.add(String.valueOf(stop_id01)); 24 rs=s.executeQuery(sql02); 25 while(rs.next()) { 26 stop_id02=rs.getInt("stop_id"); 27 list01.add(String.valueOf(stop_id02)); 28 } 29 if(list01.size()==3) 30 { 31 list01.add("0"); 32 } 33 //list01.add(String.valueOf(stop_id02)); 34 } catch (SQLException e) { 35 // TODO Auto-generated catch block 36 e.printStackTrace(); 37 } 38 finally { 39 mysql.close(rs, s, conn); 40 } 41 return list01; 42 } 43 //直達 1 44 public void stright01(int stop_id01,int stop_id02,List<String> list02) 45 { 46 String sql=null; 47 Statement s=null; 48 ResultSet rs=null; 49 Connection conn=mysql.getconn(); 50 if(stop_id01<stop_id02) //起點終點所在位置前后 51 { 52 sql="select stop_name from stop where stop_id >='"+stop_id01+"'"+"and stop_id <='"+stop_id02+"'"; 53 try { 54 s=conn.createStatement(); 55 rs=s.executeQuery(sql); 56 while(rs.next()) 57 { 58 String stop_name=rs.getString("stop_name"); 59 list02.add(stop_name); 60 } 61 } catch (SQLException e) { 62 // TODO Auto-generated catch block 63 e.printStackTrace(); 64 } 65 } 66 else 67 { 68 sql="select stop_name from stop where stop_id >='"+stop_id02+"'"+"and stop_id <='"+stop_id01+"' order by stop_id DESC"; 69 try { 70 s=conn.createStatement(); 71 rs=s.executeQuery(sql); 72 while(rs.next()) 73 { 74 String stop_name=rs.getString("stop_name"); 75 list02.add(stop_name); 76 } 77 } catch (SQLException e) { 78 // TODO Auto-generated catch block 79 e.printStackTrace(); 80 }finally { 81 mysql.close(rs, s, conn); 82 } 83 } 84 } 85 86 //直達 2 不重復輸出起始點 87 public void stright02(int stop_id01,int stop_id02,List<String> list02) 88 { 89 String sql=null; 90 Statement s=null; 91 ResultSet rs=null; 92 Connection conn=mysql.getconn(); 93 if(stop_id01<stop_id02) //起點終點所在位置前后 94 { 95 sql="select stop_name from stop where stop_id >'"+stop_id01+"'"+"and stop_id <='"+stop_id02+"'"; 96 try { 97 s=conn.createStatement(); 98 rs=s.executeQuery(sql); 99 while(rs.next()) 100 { 101 String stop_name=rs.getString("stop_name"); 102 list02.add(stop_name); 103 } 104 } catch (SQLException e) { 105 // TODO Auto-generated catch block 106 e.printStackTrace(); 107 } 108 } 109 else 110 { 111 sql="select stop_name from stop where stop_id >='"+stop_id02+"'"+"and stop_id <'"+stop_id01+"' order by stop_id DESC"; 112 try { 113 s=conn.createStatement(); 114 rs=s.executeQuery(sql); 115 while(rs.next()) 116 { 117 String stop_name=rs.getString("stop_name"); 118 list02.add(stop_name); 119 } 120 } catch (SQLException e) { 121 // TODO Auto-generated catch block 122 e.printStackTrace(); 123 }finally { 124 mysql.close(rs, s, conn); 125 } 126 } 127 } 128 129 //直達 2 不重復輸出終點站 130 public void stright03(int stop_id01,int stop_id02,List<String> list02) 131 { 132 String sql=null; 133 Statement s=null; 134 ResultSet rs=null; 135 Connection conn=mysql.getconn(); 136 if(stop_id01<stop_id02) //起點終點所在位置前后 137 { 138 sql="select stop_name from stop where stop_id >='"+stop_id01+"'"+"and stop_id <'"+stop_id02+"'"; 139 try { 140 s=conn.createStatement(); 141 rs=s.executeQuery(sql); 142 while(rs.next()) 143 { 144 String stop_name=rs.getString("stop_name"); 145 list02.add(stop_name); 146 } 147 } catch (SQLException e) { 148 // TODO Auto-generated catch block 149 e.printStackTrace(); 150 } 151 } 152 else 153 { 154 sql="select stop_name from stop where stop_id >'"+stop_id02+"'"+"and stop_id <='"+stop_id01+"' order by stop_id DESC"; 155 try { 156 s=conn.createStatement(); 157 rs=s.executeQuery(sql); 158 while(rs.next()) 159 { 160 String stop_name=rs.getString("stop_name"); 161 list02.add(stop_name); 162 } 163 } catch (SQLException e) { 164 // TODO Auto-generated catch block 165 e.printStackTrace(); 166 }finally { 167 mysql.close(rs, s, conn); 168 } 169 } 170 } 171 //判斷兩條線有沒有交點 返回值不為空時,則有交點 可能有兩個交點 172 public List<String> ifhave(int line_id01,int line_id02) 173 { 174 List<String> point=new ArrayList<>(); 175 String sql="select s2.stop_name from stop s1 join stop s2 " //查詢是否有交點 176 + "on s1.stop_name=s2.stop_name " 177 + "where s1.stop_id!=s2.stop_id " 178 + "and s1.stop_id >='"+line_id01*100+"'and s1.stop_id <='"+(line_id01+1)*100+"'" 179 + "and s2.stop_id >='"+line_id02*100+"'and s2.stop_id <='"+(line_id02+1)*100+"'"; 180 Connection conn=mysql.getconn(); 181 Statement s=null; 182 ResultSet rs=null; 183 try { 184 s=conn.createStatement(); 185 rs=s.executeQuery(sql); 186 while(rs.next()) 187 { 188 String x=rs.getString("stop_name"); 189 point.add(x); 190 } 191 if(point.size()==1) 192 { 193 point.add("0"); 194 } 195 } catch (SQLException e) { 196 // TODO Auto-generated catch block 197 e.printStackTrace(); 198 }finally { 199 mysql.close(rs, s, conn); 200 } 201 202 return point; 203 } 204 205 //求換乘最短路徑 換乘1次 206 public List<String> change01(int line_id01,int line_id02,int stop_id01,int stop_id02,List<String> list02) 207 { 208 String sql=null; 209 Connection conn=mysql.getconn(); 210 Statement s=null; 211 ResultSet rs=null; 212 int point_id=0; 213 List<String> listone=new ArrayList<>(); 214 List<String> listtwo=new ArrayList<>(); 215 List<String> listthree=new ArrayList<>(); 216 List<String> listfour=new ArrayList<>(); 217 List<String> point=new ArrayList<>(); 218 point=ifhave(line_id01,line_id02); 219 if(point.size()!=0) 220 { 221 if(!(point.get(0).equals("0"))&&point.get(1).equals("0")) //兩條直線有一個交點 222 { 223 try { 224 sql="select stop_id from stop where stop_name='"+point.get(0)+"'and stop_id>='"+line_id01*100+"'and stop_id <='"+(line_id01+1)*100+"'"; 225 s=conn.createStatement(); 226 rs=s.executeQuery(sql); 227 while(rs.next()) 228 { 229 point_id=rs.getInt("stop_id"); 230 } 231 stright01(stop_id01,point_id,listone); 232 sql="select stop_id from stop where stop_name='"+point.get(0)+"'and stop_id>='"+line_id02*100 +"'and stop_id <='"+(line_id02+1)*100+"'"; 233 rs=s.executeQuery(sql); 234 while(rs.next()) 235 { 236 point_id=rs.getInt("stop_id"); 237 } 238 stright02(point_id,stop_id02,listtwo); 239 listone.addAll(listtwo); 240 list02.addAll(listone); 241 } catch (SQLException e) { 242 // TODO Auto-generated catch block 243 e.printStackTrace(); 244 } 245 } 246 if(!(point.get(0).equals("0"))&&!(point.get(1).equals("0"))) //兩個交點 247 { 248 try { 249 sql="select stop_id from stop where stop_name='"+point.get(0)+"'and stop_id>='"+line_id01*100+"'and stop_id <='"+(line_id01+1)*100+"'"; 250 s=conn.createStatement(); 251 rs=s.executeQuery(sql); 252 while(rs.next()) 253 { 254 point_id=rs.getInt("stop_id"); 255 } 256 stright01(stop_id01,point_id,listone); 257 sql="select stop_id from stop where stop_name='"+point.get(0)+"'and stop_id>='"+line_id02*100 +"'and stop_id <='"+(line_id02+1)*100+"'"; 258 rs=s.executeQuery(sql); 259 while(rs.next()) 260 { 261 point_id=rs.getInt("stop_id"); 262 } 263 stright02(point_id,stop_id02,listtwo); 264 listone.addAll(listtwo); 265 } catch (SQLException e) { 266 // TODO Auto-generated catch block 267 e.printStackTrace(); 268 } 269 //第二個交點 270 try { 271 sql="select stop_id from stop where stop_name='"+point.get(1)+"'and stop_id>='"+line_id01*100+"'and stop_id <='"+(line_id01+1)*100+"'"; 272 s=conn.createStatement(); 273 rs=s.executeQuery(sql); 274 while(rs.next()) 275 { 276 point_id=rs.getInt("stop_id"); 277 } 278 stright01(stop_id01,point_id,listthree); 279 sql="select stop_id from stop where stop_name='"+point.get(1)+"'and stop_id>='"+line_id02*100 +"'and stop_id <='"+(line_id02+1)*100+"'"; 280 rs=s.executeQuery(sql); 281 while(rs.next()) 282 { 283 point_id=rs.getInt("stop_id"); 284 } 285 stright02(point_id,stop_id02,listfour); 286 listthree.addAll(listfour); 287 } catch (SQLException e) { 288 // TODO Auto-generated catch block 289 e.printStackTrace(); 290 }finally { 291 mysql.close(rs, s, conn); 292 } 293 if(listone.size()>listthree.size()) //判斷長短 294 { 295 list02.addAll(listthree); 296 } 297 else 298 { 299 list02.addAll(listone); 300 } 301 } 302 } 303 else if(point.size()==0){ 304 //list02.clear(); 305 list02=null; 306 } 307 return list02; 308 } 309 //求換乘最短路徑 換乘2次 310 public List<String> change02(int line_id01,int line_id02,int stop_id01,int stop_id02,List<String> list03) 311 { 312 String sql01="select max(stop_id) max,stop_name from stop where `change`>0 and line_id='"+line_id01+"'" 313 + "and stop_id in (select max(stop_id) from stop where `change`>0 and line_id='" 314 + line_id01+"' and stop_id <'"+stop_id01+"')"; //求起始站最近的中轉站 315 String sql02="select min(stop_id) min,stop_name from stop where `change`>0 and line_id='"+line_id01+"'" 316 + "and stop_id in (select min(stop_id) from stop where `change`>0 and line_id='" 317 + line_id01+"' and stop_id >'"+stop_id01+"')"; //求起始站最近的中轉站 318 Connection conn=mysql.getconn(); 319 String sql03; 320 Statement s01=null,s02=null; 321 ResultSet rs01=null,rs02=null; 322 List<String> one=new ArrayList<>(); 323 List<String> two=new ArrayList<>(); 324 List<String> three=new ArrayList<>(); 325 List<String> four=new ArrayList<>(); 326 int id01=0,id02=0,lineid02=0; //第一次中轉站的站號 327 int a=0; 328 int b=0; 329 String name01=null;//第一次中轉站的站名 330 try { 331 s01=conn.createStatement(); 332 s02=conn.createStatement(); 333 rs01=s01.executeQuery(sql01); 334 rs02=s02.executeQuery(sql02); 335 while(rs01.next()) { 336 if(rs01.getString("stop_name")!=null) 337 a++; 338 } 339 while(rs02.next()) { 340 if(rs02.getString("stop_name")!=null) 341 b++; 342 } 343 rs01=s01.executeQuery(sql01); 344 rs02=s02.executeQuery(sql02); 345 if(a==0&&b!=0) 346 { 347 while(rs02.next()) 348 { 349 id01=rs02.getInt("min"); 350 name01=rs02.getString("stop_name"); 351 } 352 stright03(stop_id01,id01,three); 353 sql03="select stop_id,`change` from stop where stop_name='"+name01+"'and stop_id<>'"+id01+"'"; 354 rs02=s02.executeQuery(sql03); 355 while(rs02.next()) 356 { 357 id02=rs02.getInt("stop_id"); 358 lineid02=id02/100; 359 } 360 if(lineid02==line_id02) { 361 stright01(id02,stop_id02,four); 362 } 363 else 364 { 365 change01(lineid02,line_id02,id02,stop_id02,four); 366 } 367 three.addAll(four); 368 list03.addAll(three); 369 } 370 else if(b==0&&a!=0) { 371 while(rs01.next()) 372 { 373 id01=rs01.getInt("max"); 374 name01=rs01.getString("stop_name"); 375 } 376 stright03(stop_id01,id01,one); 377 sql03="select stop_id,`change` from stop where stop_name='"+name01+"'and stop_id<>'"+id01+"'"; 378 rs01=s01.executeQuery(sql03); 379 while(rs01.next()) 380 { 381 id02=rs01.getInt("stop_id"); 382 lineid02=id02/100; 383 } 384 if(lineid02==line_id02) 385 { 386 stright01(id02,stop_id02,two); 387 } 388 else 389 { 390 change01(lineid02,line_id02,id02,stop_id02,two); 391 } 392 one.addAll(two); 393 list03.addAll(one); 394 } 395 else if(a!=0&&b!=0) 396 { 397 while(rs01.next()) 398 { 399 id01=rs01.getInt("max"); 400 name01=rs01.getString("stop_name"); 401 } 402 stright03(stop_id01,id01,one); 403 sql03="select stop_id,`change` from stop where stop_name='"+name01+"'and stop_id<>'"+id01+"'"; 404 rs01=s01.executeQuery(sql03); 405 while(rs01.next()) 406 { 407 id02=rs01.getInt("stop_id"); 408 lineid02=id02/100; 409 } 410 if(lineid02==line_id02) 411 { 412 stright01(id02,stop_id02,two); 413 one.addAll(two); 414 } 415 else 416 { 417 List<String> point=new ArrayList<>(); 418 point=ifhave(lineid02,line_id02); 419 if(point.size()==0) //判斷兩側中轉站所在的另一條線與終點站所在的線有無交點 420 { 421 one.clear(); 422 } 423 else 424 { 425 change01(lineid02,line_id02,id02,stop_id02,two); 426 one.addAll(two); 427 } 428 } 429 430 while(rs02.next()) 431 { 432 id01=rs02.getInt("min"); 433 name01=rs02.getString("stop_name"); 434 } 435 stright03(stop_id01,id01,three); 436 sql03="select stop_id,`change` from stop where stop_name='"+name01+"'and stop_id<>'"+id01+"'"; 437 rs02=s02.executeQuery(sql03); 438 while(rs02.next()) 439 { 440 id02=rs02.getInt("stop_id"); 441 lineid02=id02/100; 442 } 443 if(lineid02==line_id02) 444 { 445 stright01(id02,stop_id02,four); 446 three.addAll(four); 447 } 448 else 449 { 450 List<String> point=new ArrayList<>(); 451 point=ifhave(lineid02,line_id02); 452 if(point.size()==0) //判斷兩側中轉站所在的另一條線與終點站所在的線有無交點 453 { 454 three.clear(); 455 } 456 else 457 { 458 change01(lineid02,line_id02,id02,stop_id02,four); 459 three.addAll(four); 460 } 461 } 462 if(one.size()!=0&&three.size()!=0) { 463 if(one.size()>three.size())list03.addAll(three); 464 else list03.addAll(one); 465 } 466 else if(one.size()!=0&&three.size()==0) { 467 list03.addAll(one); 468 } 469 else if(one.size()==0&&three.size()!=0) 470 { 471 list03.addAll(three); 472 } 473 } 474 475 } catch (SQLException e) { 476 // TODO Auto-generated catch block 477 e.printStackTrace(); 478 } 479 return list03; 480 } 481 //兩個普通點之間的比較路程比較大小 482 public List<String> min01(int line_id01,int line_id02,int a,int c,List<String> list02) 483 { 484 List<String> list_01=new ArrayList<>(); 485 List<String> list_02=new ArrayList<>(); 486 List<String> list_03=new ArrayList<>(); 487 List<String> list_04=new ArrayList<>(); 488 list_01=change01(line_id01,line_id02,a,c,list_03); 489 list_02=change02(line_id01,line_id02,a,c,list_04); 490 if(list_01==null) 491 { 492 list02.addAll(list_02); 493 } 494 else if(list_01.size()<list_02.size()) 495 { 496 list02.addAll(list_01); 497 } 498 else 499 { 500 list02.addAll(list_02); 501 } 502 return list02; 503 } 504 505 //bijiao 506 public List<String> min02(int line_id01,int line_id02,int line_id03,int stop_id01,int stop_id02,int stop_id03,List<String> list02) 507 { //起始點的兩條線 //終點站的線路號 508 List<String> listone=new ArrayList<>(); 509 List<String> listtwo=new ArrayList<>(); 510 List<String> listthree=new ArrayList<>(); 511 List<String> listfour=new ArrayList<>(); 512 513 listthree=min01(line_id01,line_id03,stop_id01,stop_id03,listone); 514 listfour=min01(line_id02,line_id03,stop_id02,stop_id03,listtwo); 515 if(listthree.size()>listfour.size()) 516 { 517 list02.addAll(listfour); 518 519 } 520 else 521 { 522 list02.addAll(listthree); 523 } 524 return list02; 525 } 526 527 528 // 529 public List<String> search02(List<String> list01) 530 { 531 Connection conn=mysql.getconn();//連接數據庫 532 List<String> list02=new ArrayList<>(); 533 subway subway=null; 534 int a=Integer.valueOf(list01.get(0));//字符串轉換為整數類型 站的站號 535 int b=Integer.valueOf(list01.get(1));//字符串轉換為整數類型 站的站號 536 int c=Integer.valueOf(list01.get(2));//字符串轉換為整數類型 站的站號 537 int d=Integer.valueOf(list01.get(3));//字符串轉換為整數類型 站的站號 538 if(b==0&&d==0) //兩站都不為交點 分兩種情況:一條線上,兩條線上 539 { 540 int line_id01=a/100;//起始站所在的線路號 線路號 541 int line_id02=c/100;//終點站所在的線路號 線路號 542 if(line_id01==line_id02) //如果一條線上 543 { 544 stright01(a,c,list02); //調用直達函數 545 } 546 else //兩條線上 547 { 548 min01(line_id01,line_id02,a,c,list02); 549 } 550 } 551 else if(b!=0&&d==0) //起始站為中轉站,終點站為普通站 552 { 553 int line_id01=a/100;//起始站所在的線路號 線路號 554 int line_id02=b/100; 555 int line_id03=c/100;//終點站所在的線路號 線路號 556 if(line_id01==line_id03) 557 { 558 stright01(a,c,list02); 559 } 560 else if(line_id02==line_id03) 561 { 562 stright01(b,c,list02); 563 } 564 else if(line_id01!=line_id03&&line_id02!=line_id03) 565 { 566 min02(line_id01,line_id02,line_id03,a,b,c,list02); 567 } 568 569 } 570 else if(b==0&&d!=0) //起始站為普通站,終點站為終點站 571 { 572 int line_id01=a/100;//起始站所在的線路號 線路號 573 int line_id02=c/100; 574 int line_id03=d/100;//終點站所在的線路號 線路號 575 if(line_id01==line_id02) 576 { 577 stright01(a,c,list02); 578 } 579 else if(line_id01==line_id03) 580 { 581 stright01(a,d,list02); 582 } 583 else if(line_id01!=line_id02&&line_id01!=line_id03) 584 { List<String> List=new ArrayList<>(); 585 min02(line_id02,line_id03,line_id01,c,d,a,List); 586 for(int i=(List.size()-1);i>=0;i--) 587 { 588 list02.add(List.get(i)); 589 } 590 } 591 } 592 else if(b!=0&&d!=0) 593 { 594 int line_id01=a/100;//起始站所在的線路號 線路號 595 int line_id02=b/100; 596 int line_id03=c/100;//終點站所在的線路號 線路號 597 int line_id04=d/100; 598 if(line_id01==line_id03&&line_id02!=line_id04) 599 { 600 stright01(a,c,list02); 601 } 602 if(line_id01==line_id04&&line_id02!=line_id03) 603 { 604 stright01(a,d,list02); 605 } 606 if(line_id02==line_id03&&line_id01!=line_id04) 607 { 608 stright01(b,c,list02); 609 } 610 if(line_id02==line_id04&&line_id01!=line_id03) 611 { 612 stright01(b,d,list02); 613 } 614 if(line_id01==line_id03&&line_id02==line_id04) 615 { 616 List<String> List1=new ArrayList<>(); 617 List<String> List2=new ArrayList<>(); 618 stright01(a,c,List1); 619 stright01(b,d,List2); 620 if(List1.size()<List2.size()) 621 { 622 list02.addAll(List1); 623 } 624 else list02.addAll(List2); 625 } 626 if(line_id01==line_id04&&line_id02==line_id03) 627 { 628 List<String> List1=new ArrayList<>(); 629 List<String> List2=new ArrayList<>(); 630 stright01(a,d,List1); 631 stright01(b,c,List2); 632 if(List1.size()<List2.size()) 633 { 634 list02.addAll(List1); 635 } 636 else list02.addAll(List2); 637 } 638 if((line_id01!=line_id03)&&(line_id02!=line_id04)) 639 { 640 641 } 642 } 643 644 return list02; 645 }?
轉載于:https://www.cnblogs.com/liyuchao/p/10652427.html
總結
以上是生活随笔為你收集整理的石家庄地铁线路查询系统的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Web_Toy
- 下一篇: Selenium最全超时等待问题的处理方