石家庄地铁线路查询系统

石家庄地铁线路查询系统开发:

合作人:张旺

数据库的设计为:建立了一张表,有line_id(路线号)、stop_id(站号)、stop_name(站名)、change(某站可换乘的线号)这几列。

stop_id所显示的序号首位也可代表line_id,后两位为该站在其所在线路上的一个顺序排序序号,可以体现其位置。

设计思想:

  将所有的站点可分为两类:一种是只在一条线路上(普通点),一种是可在两条线路上,即为两条线路交点(换乘点)。

        所以可以分为3种情况:

  ①:起始点:普通点   终点:普通点

  ②:起始点:普通点   终点:换乘点      或     起始点:换乘点   终点:普通点

  ③:起始点:换乘点    终点:换乘点

        以上每种情况都可以分为以下几种:直达,一次换乘,二次换乘,多次换乘(此种情况很少,所以此次试验中并未考虑)。直达:通过得到的起始站和终点站的两个站名,获取到它们的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