石家庄地铁系统

Posted zhaoxinhui

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了石家庄地铁系统相关的知识,希望对你有一定的参考价值。

我和袁鹏一组,代码如下

util层的代码是:

package util;

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

/**
 * 数据库连接工具
 * @author YP
 */
public class Util {
    public static String db_url = "jdbc:mysql://localhost:3306/test";
    public static String db_user = "root";
    public static String db_pass = "zxh521+.";
    public static Connection getConnection () {
        Connection connection = null;    
        try {
            Class.forName("com.mysql.jdbc.Driver");
            connection = DriverManager.getConnection(db_url, db_user, db_pass);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return connection;
    }
    /**
     * 关闭连接
     * @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();
            }
        }
    }
}

bean层代码为:

package bean;

public class Subway {
    private int id;
    private int number;
    private String name;
    private String message;
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public int getNumber() {
        return number;
    }
    public void setNumber(int number) {
        this.number = number;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getMessage() {
        return message;
    }
    public void setMessage(String message) {
        this.message = message;
    }
    public Subway(int id,int number,String name,String message){
        this.id=id;
        this.name=name;
        this.number=number;
        this.message=message;
    }
    public Subway(int number,String name,String message){
        this.name=name;
        this.number=number;
        this.message=message;
    }
}

Dao层代码为:

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 bean.Subway;
import util.Util;

public class SDao {
    /**
     * 查询线路信息
     */
    public List<Subway> getSubwayByNumber(int number) {
        String sql = "select * from subway where number = ‘" + number + "‘";
        List<Subway> list = new ArrayList<>();
        Connection conn = Util.getConnection();
        Statement state = null;
        ResultSet rs = null;
        try {
            state = conn.createStatement();
            rs = state.executeQuery(sql);
            Subway subway = null;
            while (rs.next()) {
                int id = rs.getInt("id");
                String name = rs.getString("name");
                String message = rs.getString("message");
                subway = new Subway(id, number, name, message);
                list.add(subway);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            Util.close(rs, state, conn);
        }    
        return list;
    }
    /**
     * 通过name得到地铁信息
     */
    public Subway getSubwayByName(String name) {
        String sql = "select * from subway where name =‘" + name + "‘";
        Connection conn = Util.getConnection();
        Statement state = null;
        ResultSet rs = null;
        Subway subway = null;
        try {
            state = conn.createStatement();
            rs = state.executeQuery(sql);
            while (rs.next()) {
                int id = Integer.parseInt(rs.getString("id"));
                int number = Integer.parseInt(rs.getString("number"));
                String message = rs.getString("message");
                subway = new Subway(id, number, name, message);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            Util.close(rs, state, conn);
        }
        return subway;
    }
    /**
     * 查询路线
     */
    @SuppressWarnings("null")
    public List<Subway> search(String name1,String name2) {
        List<Subway> list = new ArrayList<Subway>();
        Connection conn = Util.getConnection();
        Statement state1 = null;
        Statement state2 = null;
        Statement state3 = null;
        Statement state4 = null;
        Statement state5 = null;
        ResultSet rs1 = null;
        ResultSet rs2 = null;
        ResultSet rs3 = null;
        ResultSet rs4 = null;
        ResultSet rs5 = null;
        try {
            Subway subway1 = null;
            Subway subway2 = null;
            String sql1 = "select * from subway where name =‘" + name1 + "‘";
            String sql2 = "select * from subway where name =‘" + name2 + "‘";
            state1 = conn.createStatement();
            rs1 = state1.executeQuery(sql1);
            while (rs1.next()) {
                int id1 = rs1.getInt("id");
                int number1 = rs1.getInt("number");
                String message1 = rs1.getString("message");
                subway1 = new Subway(id1, number1, name1, message1);
            }
            state2 = conn.createStatement();
            rs2 = state2.executeQuery(sql2);
            while (rs2.next()) {
                int id2 = rs2.getInt("id");
                int number2 = rs2.getInt("number");
                String message2 = rs2.getString("message");
                subway2 = new Subway(id2, number2, name2, message2);
            }
            if(subway1.getNumber()==subway2.getNumber()){
                Subway subway3 = null;
                if(subway1.getId()<subway2.getId()){
                    String sql3 = "select * from subway where id between ‘" + subway1.getId() + "‘ and ‘" + subway2.getId() + "‘";
                    state3 = conn.createStatement();
                    rs3 = state3.executeQuery(sql3);
                    while (rs3.next()) {
                        int id3 = rs3.getInt("id");
                        int number3 = rs3.getInt("number");
                        String name3 = rs3.getString("name");
                        String message3 = rs3.getString("message");
                        subway3 = new Subway(id3, number3, name3, message3);
                        list.add(subway3);
                    }
                } else if(subway1.getId()>subway2.getId()){
                    String sql3 = "select * from subway where id between ‘" + subway2.getId() + "‘ and ‘" + subway1.getId() + "‘";
                    rs3 = state3.executeQuery(sql3);
                    while (rs3.next()) {
                        int id3 = Integer.parseInt(rs3.getString("id"));
                        int number3 = Integer.parseInt(rs3.getString("number"));
                        String name3 = rs3.getString("name");
                        String message3 = rs3.getString("message");
                        subway3 = new Subway(id3, number3, name3, message3);
                        list.add(subway3);
                    }
                } 
            }
            else {
                String sql4 = "select * from subway where number = ‘" + subway1.getNumber() + "‘";
                String sql5 = "select * from subway where number = ‘" + subway2.getNumber() + "‘";
                state4 = conn.createStatement();
                rs4 = state3.executeQuery(sql4);
                
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            Util.close(rs1, state1, conn);
            Util.close(rs2, state2, conn);
            Util.close(rs3, state3, conn);
        }
        return list;
    }
}

servlet层的代码为:

package servlet;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

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 bean.Subway;
import dao.SDao;

@WebServlet("/Sservlet")
public class Sservlet extends HttpServlet {
    private static final long serialVersionUID = 1L;
    SDao dao = new SDao();
    /**
     * 方法选择
     */
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        String method = req.getParameter("method");
        if ("getsubwaybynumber".equals(method)) {
            getSubwayByNumber(req, resp);
        } else if ("getsubwaybyname".equals(method)) {
            getSubwayByName(req, resp);
        }
    }
    /**
     * 查询线路信息
     */
    private void getSubwayByNumber(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException{
        req.setCharacterEncoding("utf-8");
        int number = Integer.parseInt(req.getParameter("number"));
        List<Subway> subway = dao.getSubwayByNumber(number);
        if(subway == null) {
            req.setAttribute("message", "无此路线!");
            req.getRequestDispatcher("road.jsp").forward(req,resp);
        } else {
            req.setAttribute("subway", subway);
            req.getRequestDispatcher("roadlist.jsp").forward(req,resp);
        }
    }
    /**
     * 通过name得到Subway
     */
    private void getSubwayByName(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException{
        req.setCharacterEncoding("utf-8");
        String name1 = req.getParameter("name1");
        String name2 = req.getParameter("name2");
        Subway subway1 = dao.getSubwayByName(name1);
        Subway subway2 = dao.getSubwayByName(name2);
        List<Subway> subway3=new ArrayList<>();
        subway3 = dao.search(name1,name2);
        int m = 0;
        if(subway1.getNumber()==subway2.getNumber()){
            m = Math.abs(subway1.getId()-subway2.getId());
        }
        if(subway1.getNumber()!=subway2.getNumber()){
            
        }
        req.setAttribute("subway1", subway1);
        req.setAttribute("subway2", subway2);
        req.setAttribute("m", m);
        req.setAttribute("subway3", subway3);
        req.getRequestDispatcher("searchlist.jsp").forward(req,resp);
    }
}

现在的代码只能完成单条线路和一次换车,无法完成多次换车。

以上是关于石家庄地铁系统的主要内容,如果未能解决你的问题,请参考以下文章

结对项目开发(石家庄地铁乘车系统)

结对项目开发(石家庄地铁乘车系统)

石家庄地铁线路查询系统

石家庄地铁线路查询系统

石家庄地铁选站系统进度报告

高德地图 地铁图adcode 城市代码