地铁系统PC端代码
Posted liyiyang
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了地铁系统PC端代码相关的知识,希望对你有一定的参考价值。
代码顺序为项目文件顺序从上到下
package org.example.dao;
import org.example.pojo.Station;
import java.sql.ResultSet;
import java.util.List;
public interface SubWay
List<String> stations(String line);
List<String> lines(String stationname);
List<Station> getShortLine(String start, String end);
List<Station> getLessLines(String start, String end);
List<String> res_line(ResultSet resultSet);
List<Integer> res_stationid(ResultSet resultSet);
package org.example.pojo;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.util.List;
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Station
private int stationid;
private String stationname;
private List<String> lines;
private List<Integer> next;
private int provideid;
package org.example.service;
import lombok.SneakyThrows;
import org.example.dao.SubWay;
import org.example.pojo.Station;
import org.example.utils.SqlUtils;
import java.sql.Array;
import java.sql.ResultSet;
import java.util.*;
public class SubWayService implements SubWay
@SneakyThrows
@Override
public List<String> stations(String line)
SqlUtils.getConnection();
String sql="select * from line where "+line+"!=-1 order by stationid";
ResultSet resultSet=SqlUtils.query(sql,null);
if (!resultSet.next()) return null;
resultSet.previous();
List<String> stations=new ArrayList<>();
while (resultSet.next())
stations.add(resultSet.getString("stationname"));
SqlUtils.closeConnection();
return stations;
@SneakyThrows
@Override
public List<String> lines(String stationname)
SqlUtils.getConnection();
System.out.println(stationname);
String sql="select * from line where stationname = ?";
ResultSet resultSet=SqlUtils.query(sql,new String[]stationname);
if (!resultSet.next()) return null;
resultSet.previous();
resultSet.next();
List<String> lines=res_line(resultSet);
SqlUtils.closeConnection();
return lines;
@SneakyThrows
public List<Station> getShortLine(String start, String end)
SqlUtils.getConnection();
String sql="select * from line";;
HashMap<Integer,Station> stationHashMap=new HashMap<>();
HashMap<Integer,Integer> dis=new HashMap<>();
ResultSet resultSet=SqlUtils.query(sql,null);
if (!resultSet.next()) return null;
resultSet.previous();
int st=0;
int en=0;
while (resultSet.next())
dis.put(resultSet.getInt(1),0x3f3f3f3f);
if(resultSet.getString(2).equals(start)) st=resultSet.getInt(1);
if(resultSet.getString(2).equals(end)) en=resultSet.getInt(1);
stationHashMap.put(resultSet.getInt(1),
new Station(resultSet.getInt(1),
resultSet.getString(2),
res_line(SqlUtils.query("select * from line where stationid=?",new Integer[]resultSet.getInt(1))),
res_stationid(SqlUtils.query("select * from transfer where startid=?",new Integer[]resultSet.getInt(1))),
-1
));
Queue<Integer> queue=new LinkedList<>();
queue.offer(st);
dis.put(st,0);
List<Integer> temp=null;
while (!queue.isEmpty())
int t= queue.poll();
if(t==en) break;
temp=stationHashMap.get(t).getNext();
if(temp==null) continue;
for(int i:temp)
if(dis.get(i)>dis.get(t)+1)
dis.put(i,dis.get(t)+1);
stationHashMap.get(i).setProvideid(t);
if(!queue.contains(i)) queue.add(i);
List<Station> res=new ArrayList<>();
int k=en;
while (k!=-1)
res.add(stationHashMap.get(k));
k=stationHashMap.get(k).getProvideid();
Collections.reverse(res);
SqlUtils.closeConnection();
return res;
@SneakyThrows
public List<Station> getLessLines(String start, String end)
SqlUtils.getConnection();
String sql="select * from line";
HashMap<Integer,Station> stationHashMap=new HashMap<>();
HashMap<Integer,Integer> dis=new HashMap<>();
ResultSet resultSet=SqlUtils.query(sql,null);
if (!resultSet.next()) return null;
resultSet.previous();
int st=0;
int en=0;
while (resultSet.next())
dis.put(resultSet.getInt(1),0x3f3f3f3f);
if(resultSet.getString(2).equals(start)) st=resultSet.getInt(1);
if(resultSet.getString(2).equals(end)) en=resultSet.getInt(1);
stationHashMap.put(resultSet.getInt(1),
new Station(resultSet.getInt(1),
resultSet.getString(2),
res_line(SqlUtils.query("select * from line where stationid=?",new Integer[]resultSet.getInt(1))),
res_stationid(SqlUtils.query("select * from transfer where startid=?",new Integer[]resultSet.getInt(1))),
-1
));
Queue<Integer> queue=new LinkedList<>();
queue.offer(st);
dis.put(st,0);
List<Integer> temp=null;
while (!queue.isEmpty())
int t= queue.poll();
temp=stationHashMap.get(t).getNext();
if(temp==null) continue;
for(int i:temp)
if(Collections.disjoint(stationHashMap.get(t).getLines(),stationHashMap.get(i).getLines()))
if(dis.get(i)>dis.get(t)+1)
dis.put(i,dis.get(t)+1);
stationHashMap.get(i).setProvideid(t);
if(!queue.contains(i)) queue.add(i);
else
if(dis.get(i)>dis.get(t))
dis.put(i,dis.get(t));
stationHashMap.get(i).setProvideid(t);
if(!queue.contains(i)) queue.add(i);
int k=en;
List<Station> res=new ArrayList<>();
while (k!=-1)
res.add(stationHashMap.get(k));
k=stationHashMap.get(k).getProvideid();
Collections.reverse(res);
SqlUtils.closeConnection();
return res;
@SneakyThrows
public List<String> res_line(ResultSet resultSet)
resultSet.next();
List<String> lines=new ArrayList<>();
if(resultSet.getInt(3)>0) lines.add("地铁1号线");
if(resultSet.getInt(4)>0) lines.add("地铁八通线");
if(resultSet.getInt(5)>0) lines.add("地铁2号线");
if(resultSet.getInt(6)>0) lines.add("地铁4号线");
if(resultSet.getInt(7)>0) lines.add("地铁大兴线");
if(resultSet.getInt(8)>0) lines.add("地铁5号线");
if(resultSet.getInt(9)>0) lines.add("地铁6号线");
if(resultSet.getInt(10)>0) lines.add("地铁7号线");
if(resultSet.getInt(11)>0) lines.add("地铁8号线");
if(resultSet.getInt(12)>0) lines.add("地铁9号线");
if(resultSet.getInt(13)>0) lines.add("地铁10号线");
if(resultSet.getInt(14)>0) lines.add("地铁13号线");
if(resultSet.getInt(15)>0) lines.add("地铁14号线");
if(resultSet.getInt(16)>0) lines.add("地铁15号线");
if(resultSet.getInt(17)>0) lines.add("地铁房山线");
if(resultSet.getInt(18)>0) lines.add("地铁昌平线");
if(resultSet.getInt(19)>0) lines.add("地铁亦庄线");
if(resultSet.getInt(20)>0) lines.add("机场线");
return lines;
@SneakyThrows
public List<Integer> res_stationid(ResultSet resultSet)
List<Integer> id=new ArrayList<>();
while (resultSet.next())
id.add(resultSet.getInt(2));
return id;
package org.example.servlet;
import javax.servlet.*;
import java.io.IOException;
public class AllFilter implements Filter
@Override
public void init(FilterConfig filterConfig) throws ServletException
System.out.println("过滤器加载");
@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException
servletRequest.setCharacterEncoding("UTF-8");
servletResponse.setCharacterEncoding("UTF-8");
filterChain.doFilter(servletRequest,servletResponse);
@Override
public void destroy()
System.out.println("过滤器销毁");
package org.example.servlet;
import org.example.dao.SubWay;
import org.example.pojo.Station;
import org.example.service.SubWayService;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
public class SelectLessLines extends HttpServlet
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException
SubWay subWay=new SubWayService();
String start=req.getParameter("start");
String end=req.getParameter("end");
List<Station> stations=subWay.getLessLines(start,end);
req.setAttribute("stations",stations);
req.getRequestDispatcher("/resless").forward(req,resp);
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException
doGet(req,resp);
package org.example.servlet;
import org.example.dao.SubWay;
import org.example.service.SubWayService;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
public class SelectLines extends HttpServlet
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException
SubWay service=new SubWayService();
List<String> list=service.lines(req.getParameter("stationname"));
req.setAttribute("lines",list);
req.getRequestDispatcher("/resline").forward(req,resp);
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException
doGet(req,resp);
package org.example.servlet;
import org.example.dao.SubWay;
import org.example.pojo.Station;
import org.example.service.SubWayService;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
public class SelectShortLine extends HttpServlet
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException
SubWay subWay=new SubWayService();
String start=req.getParameter("start");
String end=req.getParameter("end");
List<Station> stations=subWay.getShortLine(start,end);
req.setAttribute("stations",stations);
req.getRequestDispatcher("/resshort").forward(req,resp);
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException
doGet(req,resp);
package org.example.servlet;
import org.example.dao.SubWay;
import org.example.service.SubWayService;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;
public class SelectStations extends HttpServlet
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException
SubWay service=new SubWayService();
List<String> list=service.stations(req.getParameter("kind"));
req.setAttribute("stations",list);
req.getRequestDispatcher("/resstation").forward(req,resp);
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException
doGet(req,resp);
package org.example.utils;
import lombok.SneakyThrows;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
public class SqlUtils
private static String driver = "com.mysql.jdbc.Driver";
private static String url = "jdbc:mysql://localhost:3307/subway";
private static String username = "root";
private static String password = "123456";
private static Connection connection;
private static PreparedStatement preparedStatement;
@SneakyThrows
public static void getConnection()
Class.forName(driver);
connection = DriverManager.getConnection(url, username, password);
@SneakyThrows
public static int update(String sql, Object[] s)
preparedStatement = connection.prepareStatement(sql, ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE);
if(s==null) return preparedStatement.executeUpdate();
for (int i = 0; i < s.length; i++)
if (s[i] == null)
return 0;
preparedStatement.setObject(i + 1, s[i]);
return preparedStatement.executeUpdate();
@SneakyThrows
public static ResultSet query(String sql, Object[] s)
preparedStatement = connection.prepareStatement(sql, ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE);
if(s==null) return preparedStatement.executeQuery();
for (int i = 0; i < s.length; i++)
if (s[i] == null)
break;
preparedStatement.setObject(i + 1, s[i]);
return preparedStatement.executeQuery();
@SneakyThrows
public static void closeConnection()
if(preparedStatement!=null) preparedStatement.close();
if(connection!=null) connection.close();
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ page import="java.util.List" %>
<%@ page import="org.example.pojo.Station" %>
<%@ page import="org.apache.commons.collections.CollectionUtils" %>
<%@ page import="java.util.ArrayList" %>
<%@ page import="java.util.Collections" %>
<html>
<head>
<title>最少换乘路线</title>
<style>
th
border: orange solid 2px;
font-size: 40pt
td
border: orange solid 2px;
font-size: 20pt
</style>
</head>
<body>
<table>
<%
List<Station> stations=(List<Station>) request.getAttribute("stations");
if(stations!=null)
boolean flag=false;
List<String> a=new ArrayList<>();
List<String> b=new ArrayList<>();
List<String> c=new ArrayList<>();
List<String> ab=new ArrayList<>();
List<String> bc=new ArrayList<>();
%>
<tr>
<th>路线站点名</th>
</tr>
<tr>
<td>站点数量:<%=stations.size()%></td>
</tr>
<%for(int i=0;i<stations.size();i++)
a=null;
b=null;
c=null;
flag=false;
if(i-1>0) a=stations.get(i-1).getLines();
b=stations.get(i).getLines();
if(i+1<stations.size()) c=stations.get(i+1).getLines();
if(a!=null&&c!=null)
bc= (List<String>) CollectionUtils.intersection(b,c);
flag=!Collections.disjoint(a,c);
else flag=true;
%>
<tr>
<td>
<%=stations.get(i).getStationname()%>
<%
if(!flag)
response.getWriter().print("(换乘"+bc.get(0)+")");
%>
</td>
</tr>
<%%>
<%%>
</table>
</body>
</html>
<%@ page import="java.util.List" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>路线结果</title>
</head>
<body >
<table>
<%
List<String> lines=(List<String>) request.getAttribute("lines");
if(lines!=null)
%>
<tr>
<th >所属线路</th>
</tr>
<%for(String s:lines)%>
<tr>
<td ><%=s%></td>
</tr>
<%%>
<%
else
%>
<%%>
</table>
</body>
</html>
<%@ page import="java.util.List" %>
<%@ page import="org.example.pojo.Station" %>
<%@ page import="org.apache.commons.collections.CollectionUtils" %>
<%@ page import="java.util.ArrayList" %>
<%@ page import="java.util.Collections" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>最短路线</title>
<style>
th
border: orange solid 2px;
font-size: 40pt
td
border: orange solid 2px;
font-size: 20pt
</style>
</head>
<body>
<table>
<%
List<Station> stations=(List<Station>) request.getAttribute("stations");
if(stations!=null)
boolean flag=false;
List<String> a=new ArrayList<>();
List<String> b=new ArrayList<>();
List<String> c=new ArrayList<>();
List<String> ab=new ArrayList<>();
List<String> bc=new ArrayList<>();
%>
<tr>
<th>路线站点名</th>
</tr>
<tr>
<td>站点数量:<%=stations.size()%></td>
</tr>
<%for(int i=0;i<stations.size();i++)
a=null;
b=null;
c=null;
flag=false;
if(i-1>0) a=stations.get(i-1).getLines();
b=stations.get(i).getLines();
if(i+1<stations.size()) c=stations.get(i+1).getLines();
if(a!=null&&c!=null)
bc= (List<String>) CollectionUtils.intersection(b,c);
flag=!Collections.disjoint(a,c);
else flag=true;
%>
<tr>
<td>
<%=stations.get(i).getStationname()%>
<%
if(!flag)
response.getWriter().print("(换乘"+bc.get(0)+")");
%>
</td>
</tr>
<%%>
<%%>
</table>
</body>
</html>
<%@ page import="java.util.List"%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>站点结果</title>
</head>
<body >
<table>
<%
List<String> stations=(List<String>) request.getAttribute("stations");
if(stations!=null)
%>
<tr>
<th >站点名称</th>
</tr>
<%for (String s:stations)%>
<tr>
<td ><%=s%></td>
</tr>
<%%>
<%
%>
</table>
</body>
</html>
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0">
<servlet>
<servlet-name>SelectStations</servlet-name>
<servlet-class>org.example.servlet.SelectStations</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>SelectStations</servlet-name>
<url-pattern>/selectstations</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>SelectLines</servlet-name>
<servlet-class>org.example.servlet.SelectLines</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>SelectLines</servlet-name>
<url-pattern>/selectlines</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>resstation</servlet-name>
<jsp-file>/WEB-INF/Stations.jsp</jsp-file>
</servlet>
<servlet-mapping>
<servlet-name>resstation</servlet-name>
<url-pattern>/resstation</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>resline</servlet-name>
<jsp-file>/WEB-INF/Lines.jsp</jsp-file>
</servlet>
<servlet-mapping>
<servlet-name>resline</servlet-name>
<url-pattern>/resline</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>selectshortline</servlet-name>
<servlet-class>org.example.servlet.SelectShortLine</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>selectshortline</servlet-name>
<url-pattern>/selectshort</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>selectlessline</servlet-name>
<servlet-class>org.example.servlet.SelectLessLines</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>selectlessline</servlet-name>
<url-pattern>/selectless</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>resshort</servlet-name>
<jsp-file>/WEB-INF/ShortLine.jsp</jsp-file>
</servlet>
<servlet-mapping>
<servlet-name>resshort</servlet-name>
<url-pattern>/resshort</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>resless</servlet-name>
<jsp-file>/WEB-INF/LessLine.jsp</jsp-file>
</servlet>
<servlet-mapping>
<servlet-name>resless</servlet-name>
<url-pattern>/resless</url-pattern>
</servlet-mapping>
<filter>
<filter-name>allfilter</filter-name>
<filter-class>org.example.servlet.AllFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>allfilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
<%@ page import="java.util.List" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>北京地铁系统</title>
<style>
body
text-align: center;
span
margin: 5px;
border: 2px solid blanchedalmond;
font-size: 15pt;
background: blanchedalmond;
form
text-align: center;
border: deepskyblue solid 2px;
width: 400px;
height: 400px;
margin-left: 200px;
margin-top: 50px;
input
font-size: 15pt;
</style>
<script>
function a1()
document.getElementById(\'f1\').style.display=\'block\'
document.getElementById(\'f2\').style.display=\'none\'
document.getElementById(\'f3\').style.display=\'none\'
document.getElementById(\'f4\').style.display=\'none\'
document.getElementById(\'t1\').style.background=\'paleturquoise\'
document.getElementById(\'t2\').style.background=\'blanchedalmond\'
document.getElementById(\'t3\').style.background=\'blanchedalmond\'
document.getElementById(\'t4\').style.background=\'blanchedalmond\'
function a2()
document.getElementById(\'f1\').style.display=\'none\'
document.getElementById(\'f2\').style.display=\'block\'
document.getElementById(\'f3\').style.display=\'none\'
document.getElementById(\'f4\').style.display=\'none\'
document.getElementById(\'t1\').style.background=\'blanchedalmond\'
document.getElementById(\'t2\').style.background=\'paleturquoise\'
document.getElementById(\'t3\').style.background=\'blanchedalmond\'
document.getElementById(\'t4\').style.background=\'blanchedalmond\'
function a3()
document.getElementById(\'f1\').style.display=\'none\'
document.getElementById(\'f2\').style.display=\'none\'
document.getElementById(\'f3\').style.display=\'block\'
document.getElementById(\'f4\').style.display=\'none\'
document.getElementById(\'t1\').style.background=\'blanchedalmond\'
document.getElementById(\'t2\').style.background=\'blanchedalmond\'
document.getElementById(\'t3\').style.background=\'paleturquoise\'
document.getElementById(\'t4\').style.background=\'blanchedalmond\'
function a4()
document.getElementById(\'f1\').style.display=\'none\'
document.getElementById(\'f2\').style.display=\'none\'
document.getElementById(\'f3\').style.display=\'none\'
document.getElementById(\'f4\').style.display=\'block\'
document.getElementById(\'t1\').style.background=\'blanchedalmond\'
document.getElementById(\'t2\').style.background=\'blanchedalmond\'
document.getElementById(\'t3\').style.background=\'blanchedalmond\'
document.getElementById(\'t4\').style.background=\'paleturquoise\'
</script>
</head>
<body>
<span onclick="a1()" id="t1">线路站点查询</span>
<span onclick="a2()" id="t2">线路名称查询</span>
<span onclick="a3()" id="t3">最短路径查询查询</span>
<span onclick="a4()" id="t4">最少换乘查询</span>
<form id="f1" action="/selectstations" method="post">
<h1>线路站点选择</h1>
<select name="kind" >
<option value="line1">地铁1号线</option>
<option value="line2">地铁八通线</option>
<option value="line3">地铁2号线</option>
<option value="line4">地铁4号线</option>
<option value="line5">地铁大兴线</option>
<option value="line6">地铁5号线</option>
<option value="line7">地铁6号线</option>
<option value="line8">地铁7号线</option>
<option value="line9">地铁8号线</option>
<option value="line10">地铁9号线</option>
<option value="line11">地铁10号线</option>
<option value="line12">地铁13号线</option>
<option value="line13">地铁14号线</option>
<option value="line14">地铁15号线</option>
<option value="line15">地铁房山线</option>
<option value="line16">地铁昌平线</option>
<option value="line17">地铁亦庄线</option>
<option value="line18">机场线</option>
</select>
<input type="submit" value="查询" >
</form>
<form id="f2" action="/selectlines" method="post">
<h1>站点名</h1>
<input type="text" name="stationname" placeholder="请输入站点名" required="required">
<br/>
<input type="submit" value="查询" >
</form>
<form id="f3" method="post" action="/selectshort">
<h1>起点</h1>
<input type="text" name="start"placeholder="输入起始点" required="required">
<h1>终点</h1>
<input type="text" name="end" placeholder="请输入终点" required="required">
<br/>
<input type="submit" value="查询" >
</form>
<form id="f4" method="post" action="/selectless">
<h1>起点</h1>
<input type="text" name="start" placeholder="输入起始点" required="required">
<h1>终点</h1>
<input type="text" name="end" placeholder="请输入终点" required="required">
<br/>
<input type="submit" value="查询" >
</form>
</body>
</html>
以上是关于地铁系统PC端代码的主要内容,如果未能解决你的问题,请参考以下文章