其他分享
首页 > 其他分享> > 石家庄地铁查询系统开发进度

石家庄地铁查询系统开发进度

作者:互联网

 

开发人:张小军        合作同伴:刘世朝

         基于石家庄地铁规划图,这两天我们两个想了许多方法来实现该查询系统,首先我们从一条线路来实现,我们选取了一号线来实现。在数据库中建立了一个名为subway的表。表内有三个属性,分别为id、name、line。name为站名,line为线路号。其源代码如下所示:

common.Subway.java:

package common;

public class Subway {
    private String id;
    private String name;
    private String line;
    public String getId() {
        return id;
    }
    public void setId(String id) {
        this.id=id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name=name;
    }
    public String getLine() {
        return line;
    }
    public void setLine(String line) {
        this.line=line;
    }
    public Subway() {}    
    public Subway(String id, String name, String line) {
        this.id = id;
        this.name = name;
        this.line=line;    
    }
    public Subway( String name, String line) {    
        this.name = name;
        this.line=line;    
    }
}

 

dao.SubwayDao.java:

//这里是构建的方法。

package dao;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import common.Subway;
import helper.SbHelper;
public class SubwayDao {

    /*
     * 搜索一号线的所有路线
     */
        public List<Subway> subway(String name, String line) {
            String sql = "select * from subway order by id ASC";
            List<Subway> list = new ArrayList<>();
            Connection conn = SbHelper.getConn();
            Statement state = null;
            ResultSet rs = null;

            try {
                state = conn.createStatement();
                rs = state.executeQuery(sql);
                Subway bean = null;
                while (rs.next()) {
                    String id = rs.getString("id");
                    String name1 = rs.getString("name");
                    String line1 = rs.getString("line");
                    bean = new Subway(id, name1,line1);
                    list.add(bean);
                }
            } catch (SQLException e) {
                e.printStackTrace();
            } finally {
                SbHelper.close(rs, state, conn);
            }
            
            return list;
        }
        /*
         * 根据站台名获取该站台的id
         */
        public String id(String name)
        {
            String sql="select id from subway where name = '"+name+"'";
            Connection conn = SbHelper.getConn();
            Statement state = null;
            String id=null;
            ResultSet rs = null;
            try
            {
                state=conn.createStatement();
                rs = state.executeQuery(sql);
                while(rs.next())
                {
                     id=rs.getString("id");
                }
            }catch (SQLException e) {
                e.printStackTrace();
            } finally {
                SbHelper.close(rs, state, conn);
            }
            return id;
            
        }
        /*
         * 根据站台名获取该站台属于几号线路
         */
        public String line(String name)
        {
            String sql="select line from subway where name = '"+name+"'";
            Connection conn = SbHelper.getConn();
            Statement state = null;
            String line=null;

            ResultSet rs = null;
            try
            {
                state=conn.createStatement();
                rs = state.executeQuery(sql);
                while(rs.next())
                {
                     line=rs.getString("line");
                     line+="号线";
                }
            }catch (SQLException e) {
                e.printStackTrace();
            } finally {
                SbHelper.close(rs, state, conn);
            }
            return line;
            
        }
        /*
         * 根据获取的id值搜索出其中间的站台升序
         */
        public String station1(int id1,int id2)
        {
            String sql="select name from subway where id between '"+id1+"' and '"+id2+"' order by id ASC" ;
            Connection conn = SbHelper.getConn();
            Statement state = null;
            ResultSet rs = null;
            String route = "";
            try
            {
                state=conn.createStatement();
                rs = state.executeQuery(sql);
                if(rs.next())
                    route=rs.getString("name");
                while(rs.next())
                {
                    String name=rs.getString("name");
                    route+="->"+name;
                }
            }catch (SQLException e) {
                e.printStackTrace();
            } finally {
                SbHelper.close(rs, state, conn);
            }
            return route;
        }
        /*
         * 根据获取的id值搜索出其中间的站台降序
         */
        public String station2(int id1,int id2)
        {
            String sql="select name from subaway where id between '"+id1+"' and '"+id2+"' order by id DESC" ;
            Connection conn = SbHelper.getConn();
            Statement state = null;
            ResultSet rs = null;
            String route = "";
            try
            {
                state=conn.createStatement();
                rs = state.executeQuery(sql);
                if(rs.next())
                    route=rs.getString("name");
                while(rs.next())
                {
                    String name=rs.getString("name");
                    route+="->"+name;
                }
            }catch (SQLException e) {
                e.printStackTrace();
            } finally {
                SbHelper.close(rs, state, conn);
            }
            return route;
        }
}

 

helper.SbHleper.java:

package helper;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class SbHelper {
    public static String driverName="com.microsoft.sqlserver.jdbc.SQLServerDriver";
    public static String  dbURL="jdbc:sqlserver://127.0.0.1:1433;DatabaseName=java";
    public static  String userName="sa";
    public static String userPwd="995893";
    
    public static Connection getConn () {
        Connection conn = null;
        
        try {
            Class.forName(driverName);;//加载驱动
            conn = DriverManager.getConnection(dbURL, dbURL, userPwd);
            
        } 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();
            }
        }
    }

}

 servlet.SubwayServlet.java

package servlet;
import java.io.IOException;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


import common.Subway;
import dao.SubwayDao;
public class SubwayServlet extends HttpServlet {
   private static final long serialVersionUID = 1L;
    
    SubwayDao dao=new SubwayDao();
    
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException
    {
        req.setCharacterEncoding("utf-8");
        String method = req.getParameter("method");
        if ("one_line".equals(method)) 
        {
            one_line(req, resp);
        }else if("no_transfer1".equals(method)) 
        {
            no_transfer1(req, resp);
        }
    }
    /*
     * 搜索一号线的所有路线
     */
    private void one_line(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException{
        req.setCharacterEncoding("utf-8");
        String line = req.getParameter("line");
        String name = req.getParameter("name");
        List<Subway> ones = dao.subway(name,line);
        req.setAttribute("ones", ones);
        req.getRequestDispatcher("one_line_searchlist.jsp").forward(req,resp);
    }
    /*
     * 根据站台名查询出其中间站台
     */
    private void no_transfer1(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException{
        req.setCharacterEncoding("utf-8");
        
        String name= req.getParameter("name");      //从网页获取输入的站台
        int id1 = Integer.parseInt(dao.id(name));     //获取其站台id   将其转换为int类型
        
        String name2 = req.getParameter("name2");
        int id2 = Integer.parseInt(dao.id(name2));

        if(id1<id2)
        {    
            String station=dao.station1(id1, id2);
            req.setAttribute("stations", station);
            
            
            String line=dao.line(name);
            System.out.print(line);
            req.setAttribute("lines", line);
            
            req.getRequestDispatcher("no_transfer_searchlist.jsp").forward(req,resp);//升序车站
            req.getRequestDispatcher("no_transfer_searchlist.jsp").forward(req,resp);//升序车站
        }else
        {
            
            String station=dao.station2(id2, id1);
            req.setAttribute("stations", station);
            
            
            String line=dao.line(name);
            System.out.print(line);
            req.setAttribute("lines", line);
            
            req.getRequestDispatcher("no_transfer_searchlist.jsp").forward(req,resp);//降序车站
            req.getRequestDispatcher("no_transfer_searchlist.jsp").forward(req,resp);//升序车站
        }
    
    }
    
}

下面是jsp文件。

one_line_search.jsp

<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>一号线</title>
<style>
    .a{
        margin-top: 20px;
    }
    .b{
        font-size: 20px;
        width: 160px;
        color: white;
        background-color: greenyellow;
    }
</style>
</head>
<body>
    <div align="center">
        <form action="SubwayServlet?method=one_line" method="post" onsubmit="return check()">
                <div class="a">
                    一号线
            </div>
            <div class="a">
                <button type="submit" class="b">查&nbsp;&nbsp;&nbsp;&nbsp;询</button>
            </div>
        </form>
    </div>
</body>
</html>

one_line_searchlist.jsp

<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>一号线</title>
<style>
    .a{
        margin-top: 20px;
    }
    .b{
        font-size: 20px;
        width: 160px;
        color: white;
        background-color: greenyellow;
    }
    .tb, td {
        border: 1px solid black;
        font-size: 22px;
    }
</style>
</head>
<body>
    <div align="center">
    <table class="tb">
            <tr>
                <td>站台</td>
                <td>线路</td>
                <td>起点站——终点站</td>
                <td>站台</td>
            </tr>
            <!-- forEach遍历出adminBeans -->
            <c:forEach items="${ones}" var="item" varStatus="status">
                <tr>
                    <td>${item.id}</td>
                    <td><a>${item.line}</a></td>
                    <td>${item.intro}</td>
                    <td>${item.route}</td>
                    <td>${item.name}</td>
                </tr>
            </c:forEach>
        </table>
    </div>

</body>
</html>

no_transfer_search.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<style>
    .a{
        margin-top: 20px;
    }
    .b{
        font-size: 20px;
        width: 160px;
        color: white;
        background-color: greenyellow;
    }
</style>
</head>。
<body>
    <div align="center">
        <form action="SubwayServlet?method=no_transfer1" method="post" onsubmit="return check()">
            <div class="a">
                起点站<input type="text" id="name" name="name"/>
            </div>
            <div class="a">
                终点站<input type="text" id="name2" name="name2" />
            </div>
            <div class="a">
                <button type="submit" class="b">查&nbsp;&nbsp;&nbsp;&nbsp;询</button>
            </div>
        </form>
    </div>

</body>
</html>

no_transfer_searchlist.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<style>
    .a{
        margin-top: 20px;
    }
    .b{
        font-size: 20px;
        width: 160px;
        color: white;
        background-color: greenyellow;
    }
    .tb, td {
        border: 1px solid black;
        font-size: 22px;
    }
</style>
</head>
<body>
    <div align="center">
        <table class="tb">
            <tr>
                
                <td></td>
                <td>线路</td>
                <td>车次</td>
            <tr></tr>
            
            <tr>
                <td>中间站</td>
                <td><%=request.getAttribute("stations") %></td>
                <td><%=request.getAttribute("lines") %></td>
            </tr>
            
        </table>
    </div>
</body>
</html>

 

数据库中的表:

 

 

运行结果:

 

        虽然系统没有报错,但在最后运行时如上图所示出了错。找了许久我们目前还是没找出错误。这是我们开发进度。

 

标签:name,rs,石家庄,id,state,地铁,开发进度,line,String
来源: https://www.cnblogs.com/lover995/p/10651040.html