• 结对地铁开发


      要求:

      

    1.开发一套石家庄地铁线路查询系统。

    2.功能设计

    1)数据库设计:将石家庄地铁线路图的各个线路,各个站点,换乘信息等用数据库的形式保存起来,应该保存的信息有 {线路号,线路的各个站名,车站的换乘信息}。

    2)站点查询:用户可以输入任一一条线路或输入出发地和目的地信息,可以查询到相关内容。

    例如输入出发地:石家庄铁道大学   目的地 博物院

    返回经历的站名的个数,和路径,如果有换乘,请列出换乘的线路

      队友:李旭

      数据库:

      这只是一部分,线路较长,就不全部显示了。

      剩下的就是开发的事情了,

      观察地图

      我们得出的结论是,最多两次换乘之后,便能完成任务,我们按照这个思路来开发,

      这是我们的构架

     

      在这其中Linksql是一个工具包,搬过来修改一下数据库就可以用了,basic是基础,存的是一个站点的信息,既建立数据库时的信息,

    BasicMsic

     1 public class StationMesic {
     2 private int id;
     3 private int line;
     4 private String name;
     5 private int num;
     6 private int exchenge;
     7 public int getId() {
     8     return id;
     9 }
    10 public void setId(int id) {
    11     this.id = id;
    12 }
    13 public int getLine() {
    14     return line;
    15 }
    16 public void setLine(int line) {
    17     this.line = line;
    18 }
    19 public String getName() {
    20     return name;
    21 }
    22 public void setName(String name) {
    23     this.name = name;
    24 }
    25 public int getNum() {
    26     return num;
    27 }
    28 public void setNum(int num) {
    29     this.num = num;
    30 }
    31 public int getExchenge() {
    32     return exchenge;
    33 }
    34 public void setExchenge(int exchenge) {
    35     this.exchenge = exchenge;
    36 }
    37 
    38 }

    dao层

    /*
     * 第一次忘记数据库中含有多个相同的站名,但line与exchenge不一样
     */
    import java.sql.Connection;
    import java.sql.PreparedStatement;
    import java.sql.ResultSet;
    import java.sql.SQLException;
    import java.sql.Statement;
    //import java.util.Scanner;
    import java.util.ArrayList;
    
    import BasicMsic.StationMesic;
    import LinkMysql.LinkMysql;
    
    public class StationDao {
        PreparedStatement pst = null;
        ResultSet rst = null;
        Statement stmt = null;
        Connection conn = null;// 连接数据库
    
        public StationDao() {
            conn = LinkMysql.LinkMysq();
        }
    
        public void free() {
            try {
                if (rst != null)
                    rst.close();
                if (pst != null)
                    pst.close();
                if (conn != null)
                    conn.close();
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    
        public static void main(String[] args) throws SQLException {
            StationDao d = new StationDao();
            d.comeTrue();
        }
    
        @SuppressWarnings("unused")
        public ArrayList<StationMesic> check(String nameOne, String nameTwo) throws SQLException {
            // TODO Auto-generated method stub
            // 与数据库进行匹配
            ArrayList<StationMesic> best = new ArrayList<StationMesic>();// 存储最优路线
            String sql = "select * from station";
            StationMesic stmOne = null;
            StationMesic stmTwo = null;
            try {
                pst = (PreparedStatement) conn.prepareStatement(sql);
                ResultSet rst = pst.executeQuery();
                while (rst.next()) {
                    if (nameOne.equals(rst.getString("name"))) {
                        stmOne = new StationMesic();
                        stmOne.setId(rst.getInt("id"));
                        stmOne.setLine(rst.getInt("line"));
                        stmOne.setName(rst.getString("name"));
                        stmOne.setNum(rst.getInt("num"));
                        stmOne.setExchenge(rst.getInt("exchenge"));
                    } else if (nameTwo.equals(rst.getString("name"))) {
                        stmTwo = new StationMesic();
                        stmTwo.setId(rst.getInt("id"));
                        stmTwo.setLine(rst.getInt("line"));
                        stmTwo.setName(rst.getString("name"));
                        stmTwo.setNum(rst.getInt("num"));
                        stmTwo.setExchenge(rst.getInt("exchenge"));
                    }
                }
                // 得到起始点与终点的信息
                /*
                 * 找交点,从起点开始到交点遍历,再由交点到终点遍历。
                 */
                best = getMidPoint(stmOne, stmTwo);
            } catch (Exception e) {
                // TODO: handle exception
            }
            //对best的重名在进行一次函数运算。
            
            return best;
        }
    
        // 两点的关系
        private ArrayList<StationMesic> getMidPoint(StationMesic stmOne, StationMesic stmTwo) throws SQLException {
            // TODO Auto-generated method stub
            /*
             * 第一步,判断两个点是否在一条线上。 考虑它是不是换乘站
             */
            // System.err.println(stmOne.getLine()+"+"+stmOne.getExchenge()+"+"+stmTwo.getLine()+"+"+stmTwo.getExchenge());
            /*
             * 使用ArrayList函数来存储最优路线。
             */
            ArrayList<StationMesic> best = new ArrayList<StationMesic>();// 存储最优路线
            if ((stmOne.getLine() == stmTwo.getLine())
                    || ((stmOne.getExchenge() == stmTwo.getExchenge()) && stmOne.getExchenge() != 0)
                    || (stmOne.getExchenge() == stmTwo.getLine()) || (stmOne.getLine() == stmTwo.getExchenge())) {
                // 如果在一条线上,则直接遍历。
                // System.err.println("我进来了!");
                best = Traverse(stmOne, stmTwo);// 输出一次,换乘0
                // System.out.println(best.size());
            } else {
                // 如果不在一条线上,则找交点,再分别遍历。
                // 单换乘,与双换乘。
                // System.err.println("不再一条线上!");
                // 成功进入。
                ArrayList<StationMesic> bestOne = gettingOneMidPoint(stmOne, stmTwo);
                // 少换乘
                // System.out.println(bestOne.size());
                ArrayList<StationMesic> bestTwo = gettingTwoMidPoint(stmOne, stmTwo);
                // 最优路线
                // System.out.println(bestTwo.size());
                if (bestOne.size() < (bestTwo.size() - 2)) {
                    best = bestOne;
                } else {
                    best = bestTwo;
                }
            }
            
            return best;
        }
    
        @SuppressWarnings({ "unused", "unchecked" })
        private ArrayList<StationMesic> gettingTwoMidPoint(StationMesic stmOne, StationMesic stmTwo) throws SQLException {
            // TODO Auto-generated method stub
            /*
             * 最优路线 多换乘,将两条线上所有的换乘站存储起来, 以1.烈士陵园--》2.考试中心。
             * 最短路线为烈士陵园--新百广场--十一中--槐安大桥--考试中心,一共五站。
             * 首先,找L1上所有的换乘站E1,再找L2上所有的换乘站E2,如果E1的ex等于E2的ex 则这为一种情况,再由一个Int值来比较个个路线的长度。
             */
            /*
             * 得到所有的可能,然后选取最优的路线,(传到servlet层),在显示在网页上。
             */
            ArrayList<StationMesic> best = new ArrayList<StationMesic>();// 存储最优路线
            ArrayList<StationMesic> all = new ArrayList<StationMesic>();// 存储所有可能
            // System.out.println("最优路线");
            ArrayList<StationMesic> stmeOne = new ArrayList<StationMesic>();
            ArrayList<StationMesic> stmeTwo = new ArrayList<StationMesic>();
            stmeOne = getExchengeStation(stmOne);
            stmeTwo = getExchengeStation(stmTwo);
            // System.out.println("找到了!"+stmeTwo.size());
            // int numOne = stmeOne.size();
            // int numTwo = stmeTwo.size();
            // 得到两条线上的换乘点。
            /*
             * 使用for循环来找所有换乘点上的exchenge值相同的换乘点
             */
            for (StationMesic smOne : stmeOne) {
                // 第一次循环第一个点上的换乘点
                for (StationMesic smTwo : stmeTwo) {
                    // 第二次循环第二个点上的换乘点
                    if (smOne.getExchenge() == smTwo.getExchenge()) {
                        // 找到exchenge值相等的点,执行Traverse函数
                        // 1
                        StationMesic stOne = new StationMesic();
                        stOne.setId(smOne.getId());
                        stOne.setLine(smOne.getLine());
                        stOne.setName(smOne.getName());
                        stOne.setNum(smOne.getNum());
                        stOne.setExchenge(smOne.getExchenge());
                        // 2
                        StationMesic stTwo = new StationMesic();
                        stTwo.setId(smTwo.getId());
                        stTwo.setLine(smTwo.getLine());
                        stTwo.setName(smTwo.getName());
                        stTwo.setNum(smTwo.getNum());
                        stTwo.setExchenge(smTwo.getExchenge());
                        // System.err.print("起始点:");
                        // 起始点--》换乘1
                        // System.err.print("换乘1:");
                        // 换乘1--》换乘2
                        // exchenge值相等
                        // System.err.println(stOne.getExchenge()+"+"+stTwo.getExchenge());
                        // System.err.print("换乘2:");
                        // 换乘2--》终点
                        // System.err.println("终点。");
                        ArrayList<StationMesic> bestOne = Traverse(stmOne, stOne);
                        ArrayList<StationMesic> bestTwo = Traverse(stOne, stTwo);
                        ArrayList<StationMesic> bestThree = Traverse(stTwo, stmTwo);
                        // System.out.println(bestOne.size());
                        for (StationMesic stm : bestOne) {
                            StationMesic stmTime = new StationMesic();
                            stmTime.setId(stm.getId());
                            stmTime.setLine(stm.getLine());
                            stmTime.setName(stm.getName());
                            stmTime.setNum(stm.getNum());
                            stmTime.setExchenge(stm.getExchenge());
                            all.add(stmTime);
                        }
                        for (StationMesic stm : bestTwo) {
                            StationMesic stmTime = new StationMesic();
                            stmTime.setId(stm.getId());
                            stmTime.setLine(stm.getLine());
                            stmTime.setName(stm.getName());
                            stmTime.setNum(stm.getNum());
                            stmTime.setExchenge(stm.getExchenge());
                            all.add(stmTime);
                        }
                        for (StationMesic stm : bestThree) {
                            StationMesic stmTime = new StationMesic();
                            stmTime.setId(stm.getId());
                            stmTime.setLine(stm.getLine());
                            stmTime.setName(stm.getName());
                            stmTime.setNum(stm.getNum());
                            stmTime.setExchenge(stm.getExchenge());
                            all.add(stmTime);
                        }
                        //对best复制
                        if (best.size() == 0) {
                            best=(ArrayList<StationMesic>) all.clone();
                        }
                        else
                        {
                            // System.out.println(best.size()+"+"+all.size());
                            if(best.size()>all.size())
                            {
                                best=(ArrayList<StationMesic>) all.clone();
                            }
                        }
                        // System.out.println("前All.size:"+all.size());
                        all.clear();
                        // System.out.println("后All.size:"+all.size());
                        // System.out.println("Best.size:"+best.size());
                    }
                }
            }
            return best;
        }
    
        @SuppressWarnings({ "null", "unused" })
        private ArrayList<StationMesic> getExchengeStation(StationMesic stm) throws SQLException {
            // TODO Auto-generated method stub
            StationMesic stmex = null;// 换乘站数量
            int line = stm.getLine();
            int i = 0;//
            String sql = "select * from station";
            pst = (PreparedStatement) conn.prepareStatement(sql);
            ResultSet rst = pst.executeQuery();
            // 求L1/L2上所有的换乘站
            // ArrayList
            // System.err.println("进入循环!");
            ArrayList<StationMesic> list = new ArrayList<StationMesic>();
            while (rst.next()) {
                if (rst.getInt("exchenge") != 0 && rst.getInt("line") == line) {
                    stmex = new StationMesic();// 声明新数组,暂时存储
                    stmex.setId(rst.getInt("id"));
                    stmex.setLine(rst.getInt("line"));
                    stmex.setName(rst.getString("name"));
                    stmex.setNum(rst.getInt("num"));
                    stmex.setExchenge(rst.getInt("exchenge"));
                    list.add(stmex);
                    i++;
                }
            }
            return list;
        }
    
        // 找交点
        private ArrayList<StationMesic> gettingOneMidPoint(StationMesic stmOne, StationMesic stmTwo) throws SQLException {
            // TODO Auto-generated method stub
            /*
             * 根据传入的线路值,找到交点 因为每两条线只有一个或两个交点。 交点在两条线上,所以确定一条线L1,根据L1上exchenge为L2的点是换乘站。
             */
            ArrayList<StationMesic> best = new ArrayList<StationMesic>();// 存储最优路线
            int lineOne = stmOne.getLine();
            int lineTwo = stmTwo.getLine();
            String sql = "select * from station";
            pst = (PreparedStatement) conn.prepareStatement(sql);
            ResultSet rst = pst.executeQuery();
            while (rst.next()) {
                // System.out.println(rst.getString("name"));
                // System.out.println(rst.getInt("line"));
                int line = rst.getInt("line");
                if (line == lineOne) {
                    int exchenge = rst.getInt("exchenge");
                    if (exchenge == lineTwo) {
                        // System.out.println(rst.getString("name"));
                        StationMesic stmMid = new StationMesic();
                        stmMid.setId(rst.getInt("id"));
                        stmMid.setLine(rst.getInt("line"));
                        stmMid.setName(rst.getString("name"));
                        stmMid.setNum(rst.getInt("num"));
                        stmMid.setExchenge(rst.getInt("exchenge"));
                        ArrayList<StationMesic> bestOne = Traverse(stmOne, stmMid);
                        ArrayList<StationMesic> bestTwo = Traverse(stmMid, stmTwo);
                        for (StationMesic stm : bestOne) {
                            StationMesic stmTime = new StationMesic();
                            stmTime.setId(stm.getId());
                            stmTime.setLine(stm.getLine());
                            stmTime.setName(stm.getName());
                            stmTime.setNum(stm.getNum());
                            stmTime.setExchenge(stm.getExchenge());
                            best.add(stmTime);
                        }
                        for (StationMesic stm : bestTwo) {
                            StationMesic stmTime = new StationMesic();
                            stmTime.setId(stm.getId());
                            stmTime.setLine(stm.getLine());
                            stmTime.setName(stm.getName());
                            stmTime.setNum(stm.getNum());
                            stmTime.setExchenge(stm.getExchenge());
                            best.add(stmTime);
                        }
                    }
                    // System.out.println(best.size());
                }
            }
            return best;
        }
    
        // 遍历两点之间的站
        @SuppressWarnings("null")
        private ArrayList<StationMesic> Traverse(StationMesic stmOne, StationMesic stmTwo) throws SQLException {
            // TODO Auto-generated method stub
            /*
             * 开始只想到了两中条件,所以判断很不完善, 现在仔细想了一下,又在纸上写了一下发现还有两种情况,
             * 开始以为L1的line值和L2的line值或Exchenge值进行比较就能得到想要的结果 仔细一想并不是,这一二号线的四个值只有两个相等,但有四种情况。
             * 所以现在来不上最后两种情况。 完成所有情况的判断后,要选取最优路线。
             * 
             */
            // 前提是两点必须在一条直线上,
            StationMesic stm = null;
            ArrayList<StationMesic> list = new ArrayList<StationMesic>();
            if ((stmOne.getLine() == stmTwo.getLine())) {
                // System.err.println("非换乘站");
                // 情况1:line值相等
                int Line = stmOne.getLine();
                int numOne = stmOne.getNum();
                int numTwo = stmTwo.getNum();
                if (numOne < numTwo) {
                    String sql = "select * from station";
                    stmt = conn.createStatement();
                    rst = stmt.executeQuery(sql);
                    pst = (PreparedStatement) conn.prepareStatement(sql);
                    ResultSet rst = pst.executeQuery();
                    while (rst.next()) {
                        // 得到当前站的信息,num和线
                        int num = rst.getInt("num");
                        int line = rst.getInt("line");
                        // 正遍历
                        if (num >= numOne && num <= numTwo) {
                            if (line == Line) {
                                // System.out.println(rst.getString("name"));
                                stm = new StationMesic();// 声明新数组,暂时存储
                                stm.setId(rst.getInt("id"));
                                stm.setLine(rst.getInt("line"));
                                stm.setName(rst.getString("name"));
                                stm.setNum(rst.getInt("num"));
                                stm.setExchenge(rst.getInt("exchenge"));
                                list.add(stm);
                            }
                        }
                    }
    
                } else {
    
                    String sql = "select * from station order by num desc";
                    stmt = conn.createStatement();
                    rst = stmt.executeQuery(sql);
                    pst = (PreparedStatement) conn.prepareStatement(sql);
                    ResultSet rst = pst.executeQuery();
                    // 倒遍历
                    // System.err.println("倒遍历");
                    while (rst.next()) {
                        // 得到当前站的信息,num和线
                        int num = rst.getInt("num");
                        int line = rst.getInt("line");
                        if (num >= numTwo && num <= numOne) {
                            if (line == Line) {
                                // System.out.println(rst.getString("name"));
                                stm = new StationMesic();// 声明新数组,暂时存储
                                stm.setId(rst.getInt("id"));
                                stm.setLine(rst.getInt("line"));
                                stm.setName(rst.getString("name"));
                                stm.setNum(rst.getInt("num"));
                                stm.setExchenge(rst.getInt("exchenge"));
                                list.add(stm);
                            }
                        }
                    }
                }
            } else if ((stmOne.getExchenge() == stmTwo.getLine())) {
                // 情况2:L1的exchenge值与L2的line值相等
                // System.err.println("换乘站");
                // ex=line,以ex为主
                int Line = stmOne.getExchenge();
                int numOne = 0;// 作废
                int numTwo = stmTwo.getNum();
                // System.err.println(Line);
                // 重新获取numOne
                numOne = reGetNum(stmOne, stmTwo, 2);
                if (numOne < numTwo) {// *
                    String sql = "select * from station";
                    stmt = conn.createStatement();
                    rst = stmt.executeQuery(sql);
                    pst = (PreparedStatement) conn.prepareStatement(sql);
                    ResultSet rst = pst.executeQuery();
                    // System.err.println(numOne + "+" + numTwo);
                    while (rst.next()) {
                        // 得到当前站的信息,num和线
                        int num = rst.getInt("num");
                        int line = rst.getInt("line");
                        // 正遍历
    
                        if (num >= numOne && num <= numTwo) {
                            if (line == Line) {
                                // System.out.println(rst.getString("name"));
                                stm = new StationMesic();// 声明新数组,暂时存储
                                stm.setId(rst.getInt("id"));
                                stm.setLine(rst.getInt("line"));
                                stm.setName(rst.getString("name"));
                                stm.setNum(rst.getInt("num"));
                                stm.setExchenge(rst.getInt("exchenge"));
                                list.add(stm);
                            }
                        }
                    }
                } else {
                    String sql = "select * from station order by num desc";
                    stmt = conn.createStatement();
                    rst = stmt.executeQuery(sql);
                    pst = (PreparedStatement) conn.prepareStatement(sql);
                    ResultSet rst = pst.executeQuery();
                    // System.err.println(numOne + "+" + numTwo);
                    // 倒遍历
                    // System.err.println("倒遍历");
                    while (rst.next()) {
                        // 得到当前站的信息,num和线
                        int num = rst.getInt("num");
                        int line = rst.getInt("line");
                        if (num >= numTwo && num <= numOne) {
                            if (line == Line) {
                                // System.out.println(rst.getString("name"));
                                stm = new StationMesic();// 声明新数组,暂时存储
                                stm.setId(rst.getInt("id"));
                                stm.setLine(rst.getInt("line"));
                                stm.setName(rst.getString("name"));
                                stm.setNum(rst.getInt("num"));
                                stm.setExchenge(rst.getInt("exchenge"));
                                list.add(stm);
                            }
                        }
                    }
                }
            } else if ((stmOne.getExchenge() == stmTwo.getExchenge())) {
                // 情况3:exchenge值相等
                // System.err.println("换乘站");
                // ex=line,以ex为主
                // 以exchenge值为中心
                int Line = stmOne.getExchenge();
                int numOne = 0;// 传递过来的num值作废
                int numTwo = 0;// 作废
                // System.err.println(Line);
                // 重新获取numOne
                numOne = reGetNum(stmOne, stmTwo, 3);
                numTwo = reGetNum(stmTwo, stmTwo, 3);
                if (numOne < numTwo) {// *
                    String sql = "select * from station";
                    stmt = conn.createStatement();
                    rst = stmt.executeQuery(sql);
                    pst = (PreparedStatement) conn.prepareStatement(sql);
                    ResultSet rst = pst.executeQuery();
                    // System.err.println(numOne + "+" + numTwo);
                    while (rst.next()) {
                        // 得到当前站的信息,num和线
                        int num = rst.getInt("num");
                        int line = rst.getInt("line");
                        // 正遍历
    
                        if (num >= numOne && num <= numTwo) {
                            if (line == Line) {
                                // System.out.println(rst.getString("name"));
                                stm = new StationMesic();// 声明新数组,暂时存储
                                stm.setId(rst.getInt("id"));
                                stm.setLine(rst.getInt("line"));
                                stm.setName(rst.getString("name"));
                                stm.setNum(rst.getInt("num"));
                                stm.setExchenge(rst.getInt("exchenge"));
                                list.add(stm);
                            }
                        }
                    }
                } else {
                    String sql = "select * from station order by num desc";
                    stmt = conn.createStatement();
                    rst = stmt.executeQuery(sql);
                    pst = (PreparedStatement) conn.prepareStatement(sql);
                    ResultSet rst = pst.executeQuery();
                    // System.err.println(numOne + "+" + numTwo);
                    // 倒遍历
                    // System.err.println("倒遍历");
                    while (rst.next()) {
                        // 得到当前站的信息,num和线
                        int num = rst.getInt("num");
                        int line = rst.getInt("line");
                        if (num >= numTwo && num <= numOne) {
                            if (line == Line) {
                                // System.out.println(rst.getString("name"));
                                stm = new StationMesic();// 声明新数组,暂时存储
                                stm.setId(rst.getInt("id"));
                                stm.setLine(rst.getInt("line"));
                                stm.setName(rst.getString("name"));
                                stm.setNum(rst.getInt("num"));
                                stm.setExchenge(rst.getInt("exchenge"));
                                list.add(stm);
                            }
                        }
                    }
                }
            } else if ((stmOne.getLine() == stmTwo.getExchenge())) {
                // 情况4:L1的line值与L2的exchenge值相等
                // System.err.println("L1.line=L2.exchenge");
                // ex=line,以ex为主
                int Line = stmOne.getLine();
                int numOne = stmOne.getNum();// 作废
                int numTwo = 0;
                // System.err.println(Line);
                // 重新获取numOne
                numTwo = reGetNum(stmTwo, stmOne, 4);
                // System.out.println(numTwo);
                if (numOne < numTwo) {// *
                    String sql = "select * from station";
                    stmt = conn.createStatement();
                    rst = stmt.executeQuery(sql);
                    pst = (PreparedStatement) conn.prepareStatement(sql);
                    ResultSet rst = pst.executeQuery();
                    // System.err.println(numOne + "+" + numTwo);
                    while (rst.next()) {
                        // 得到当前站的信息,num和线
                        int num = rst.getInt("num");
                        int line = rst.getInt("line");
                        // 正遍历
    
                        if (num >= numOne && num <= numTwo) {
                            if (line == Line) {
                                // System.out.println(rst.getString("name"));
                                stm = new StationMesic();// 声明新数组,暂时存储
                                stm.setId(rst.getInt("id"));
                                stm.setLine(rst.getInt("line"));
                                stm.setName(rst.getString("name"));
                                stm.setNum(rst.getInt("num"));
                                stm.setExchenge(rst.getInt("exchenge"));
                                list.add(stm);
                            }
                        }
                    }
                } else {
                    String sql = "select * from station order by num desc";
                    stmt = conn.createStatement();
                    rst = stmt.executeQuery(sql);
                    pst = (PreparedStatement) conn.prepareStatement(sql);
                    ResultSet rst = pst.executeQuery();
                    // System.err.println(numOne + "+" + numTwo);
                    // 倒遍历
                    // System.err.println("倒遍历");
                    while (rst.next()) {
                        // 得到当前站的信息,num和线
                        int num = rst.getInt("num");
                        int line = rst.getInt("line");
                        if (num >= numTwo && num <= numOne) {
                            if (line == Line) {
                                // System.out.println(rst.getString("name"));
                                stm = new StationMesic();// 声明新数组,暂时存储
                                stm.setId(rst.getInt("id"));
                                stm.setLine(rst.getInt("line"));
                                stm.setName(rst.getString("name"));
                                stm.setNum(rst.getInt("num"));
                                stm.setExchenge(rst.getInt("exchenge"));
                                list.add(stm);
                            }
                        }
                    }
                }
            }
            return list;
    
        }
    
        // 重新获取Num的值
        private int reGetNum(StationMesic stmOne, StationMesic stmTwo, int i) throws SQLException {
            // TODO Auto-generated method stub
            String sql = "select * from station";
            stmt = conn.createStatement();
            rst = stmt.executeQuery(sql);
            pst = (PreparedStatement) conn.prepareStatement(sql);
            ResultSet rst = pst.executeQuery();
            // 如果以line为主,在名字相同的情况下,获取与主数相同的line值上的num
            int num = 0;
            // System.out.println(stmOne.getName()+"+"+stmOne.getExchenge()+"+"+stmOne.getLine());
            // System.out.println(stmTwo.getName()+"+"+stmTwo.getExchenge()+"+"+stmTwo.getLine());
            while (rst.next()) {
    
                if (rst.getString("name").equals(stmOne.getName())) {
                    // System.out.println(rst.getInt("line")+"+"+rst.getInt("num")+"+"+i);
                    // 以2号线的line值为主
                    if (rst.getInt("line") == stmOne.getExchenge() && i == 2) {
                        // System.out.println(rst.getInt("line"));
                        // System.out.println("我进来了!2");
                        num = rst.getInt("num");
                        break;
                    }
                    // 以两条线的exchenge值为主
                    if (rst.getInt("line") == stmTwo.getExchenge() && i == 3) {
                        // System.out.println(rst.getInt("line"));
                        // System.out.println("我进来了!3");
                        num = rst.getInt("num");
                        break;
                    }
                    // 以L1的exchenge值为主
                    if ((rst.getInt("line") == stmTwo.getLine()) && i == 4) {
                        // System.out.println("我进来了!4");
                        // System.out.println(rst.getInt("line"));
                        num = rst.getInt("num");
                        break;
                    }
    
                }
            }
            return num;
        }
    
        // 主函数,测试
        private void comeTrue() throws SQLException {
            // TODO Auto-generated method stub
            // @SuppressWarnings("resource")
            /*
             * @SuppressWarnings("resource") Scanner sc = new Scanner(System.in);
             * System.out.println("请输入始发站:"); String nameOne = sc.next();
             * System.out.println("请输入终点站:"); String nameTwo = sc.next(); check(nameOne,
             * nameTwo);
             */
            // check("中星路", "东三教");
            // check("中星路", "火车站");
            // check("火车站", "东三教");
            // check("新百广场","槐安大桥");
            // check("北国商城","槐安大桥");
            check("和平医院", "考试中心");
            // check("上庄","北国商城");//
            // check("上庄", "西王");
        }
    }

    linkMysql

    import java.io.IOException;
    import java.sql.SQLException;
    import java.util.ArrayList;
    
    import javax.servlet.ServletException;
    import javax.servlet.annotation.WebServlet;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    
    import BasicMsic.StationMesic;
    import Dao.StationDao;
    
    /**
     * Servlet implementation class Station
     */
    @WebServlet("/Station")
    public class StationServlet extends HttpServlet {
        private static final long serialVersionUID = 1L;
           
        /**
         * @see HttpServlet#HttpServlet()
         */
        public StationServlet() {
            super();
            // TODO Auto-generated constructor stub
        }
    
        /**
         * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
         */
        protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            // TODO Auto-generated method stub
            response.getWriter().append("Served at: ").append(request.getContextPath());
        }
    
        /**
         * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
         */
        protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            // TODO Auto-generated method stub
            response.setCharacterEncoding("text/html;UTF-8"); 
            request.setCharacterEncoding("UTF-8");
            response.setCharacterEncoding("GBK");
            //跳转网页时文字显示不乱码
            String nameOne=request.getParameter("one");
            String nameTwo=request.getParameter("two");
            //System.out.println(nameOne+nameTwo);
            ArrayList<StationMesic> best=new ArrayList<StationMesic>();
            StationDao std=new StationDao();
            try {
                //System.out.println(nameOne+nameTwo);
                best=std.check(nameOne, nameTwo);
                //System.out.println(nameOne+nameTwo);
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    
    }

    这些就是全部java代码了,

    main.jsp

    <%@ page language="java" contentType="text/html; charset=utf-8"
        pageEncoding="utf-8"%>
    <!DOCTYPE html>
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>石家庄地铁</title>
    <style type="text/css">
    body
    {
    background: url(station.jpg);
    background-size:1360px 720px;
    }
    </style>
    </head>
    <body>
    <form action="show.jsp" >
    <input type="text" name="one" placeholder="请输入出发站"><br>
    <input type="text" name="two" placeholder="请输入终点站"><br>
    <input type="submit" value="查询">
    </form>
    </body>
    </html>

    show.jsp

    <%@ page language="java" contentType="text/html; charset=utf-8"
        pageEncoding="utf-8"%>
    <%@ page import="Servlet.StationServlet"%>
    <%@ page import="BasicMsic.StationMesic"%>
    <%@ page import="Dao.StationDao"%>
    <%@ page import="java.util.ArrayList"%>
    <!DOCTYPE html>
    <html>
    <head>
    <meta charset="utf-8">
    <title>欢迎使用石家庄地铁</title>
    <style type="text/css">
    body {
        background: url(station.jpg);
        background-size: 1360px 720px;
    }
    </style>
    </head>
    <body>
        <table>
    
            <%
                String nameOne = request.getParameter("one");
                String nameTwo = request.getParameter("two");
                StationDao std = new StationDao();
                ArrayList<StationMesic> best = std.check(nameOne, nameTwo);
            %>
            <%
                for (StationMesic stm : best) {
            %>
            <tr>
                <td><%=stm.getName()%></td>
            </tr>
            <%
                }
            %>
    
        </table>
    </body>
    </html>
  • 相关阅读:
    instanceof方法
    Java 实现接口计算圆柱的体积和面积并添加颜色
    Java代码执行顺序
    Java饿汉单例模式
    斐波那契数(动态规划和递归)
    Java 接口实现计算器加减乘除(字符交互界面)
    局部内部类详解_转载
    Java引用类型
    递归打印字符串
    时间复杂度
  • 原文地址:https://www.cnblogs.com/msdog/p/11058991.html
Copyright © 2020-2023  润新知