地铁查询进度(1)

package lxj;

import java.sql.Connection;

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

/**
 *
 * @author Hu
 *
 */
public class DBUtil {
    
    public static String db_url = "jdbc:mysql://localhost:3307/metro?useSSL=false&useUnicode=true&characterEncoding=UTF-8";
    public static String db_user = "root";
    public static String db_pass = "320775";
    
    public static Connection getConn () {
        Connection conn = null;
        
        try {
            Class.forName("com.mysql.jdbc.Driver");//
            conn = DriverManager.getConnection(db_url, db_user, db_pass);
        } 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();
            }
        }
    }

    public static void main(String[] args) {
        Connection conn = getConn();
        System.out.println("1111");

    }
    
}
DBUtil.java
package lxj;

public class Metro {
    private int id;
    private String name;
    private int line;

    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    

    public int getLine() {
        return line;
    }
    public void setLine(int line) {
        this.line = line;
    }
    public Metro() {}
    
    public Metro(int id,int line, String name) {
        this.id = id;
        this.name = name;
        this.line = line;
    }
    
    public Metro(int line, String name) {
        this.name = name;
        this.line = line;
    }
}
Metro.java
package lxj;
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 java.util.Scanner;


import java.sql.*;



/**
 * 课程Dao=
 * Dao层操作数据
 * @author Hu
 *
 */

public class MetroDao {
    static PreparedStatement ps = null;
    static String sql = "select * from number1";
    
    
    
    public List<Metro> list() {
        String sql = "select * from number1";
        List<Metro> list = new ArrayList<>();
        Connection conn = DBUtil.getConn();
        Statement state = null;
        ResultSet rs = null;

        try {
            state = conn.createStatement();
            rs = state.executeQuery(sql);
            Metro bean = null;
            while (rs.next()) {
                int id = rs.getInt("id");
                String name2 = rs.getString("name");
                int line2 = rs.getInt("line");
                bean = new Metro(id,line2, name2);
                list.add(bean);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DBUtil.close(rs, state, conn);
        }
        
        return list;
    }





    
    
    public static List<Metro> Load(Metro user1 , Metro user2) {
        int a= user1.getId();
        int b=user2.getId();
        
        if(user1.getLine()==user2.getLine()) {
        
    Connection conn = DBUtil.getConn();
    String sql="select * from number1 where id between "+ a +" and "+ b +" ";
    Statement state = null;
    ResultSet rs = null;
    
    List<Metro> users = new ArrayList<Metro>();
    Metro user = null;
    try {
        state = conn.createStatement();
        rs = state.executeQuery(sql);
        while(rs.next()) {
            user = new Metro();
            user.setId(rs.getInt("id"));
            user.setLine(rs.getInt("line"));
            user.setName(rs.getString("name"));

            
            users.add(user);
            //System.out.println(user);
        }
    } catch (SQLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }finally {
        DBUtil.close(rs,state, conn);
        
    }
return  users;
}
        else if(user1.getLine()!=user2.getLine()){
            
        }
    
        return null;
        
    
}
    
    
    
    public static Metro search(String sname) {
        Metro user=new Metro();
        String sql = "select * from number1 where name='" + sname + "'";
    
        Connection conn = DBUtil.getConn();
        Statement state = null;
        ResultSet rs = null;
        try {
            state = conn.createStatement();
            rs = state.executeQuery(sql);
            while (rs.next()) {

                int id = rs.getInt("id");
                String name = rs.getString("name");
                int line = rs.getInt("line");
                user = new Metro(id ,line,  name);
                

            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DBUtil.close(rs, state, conn);
        }

        return user;
    }
    
        
    
    
/*public static void main(String[] args) {
        List<Metro> users = new ArrayList<Metro>();
        System.out.println("AAAAAA");
        Scanner scan=new Scanner(System.in);
        String a=scan.next();
        String b=scan.next();
        Metro asd1=new Metro();
        Metro asd2=new Metro();
        asd1=search("上庄");
        asd2=search("谈固");
        Load(asd1,asd2);
        for(Metro u:users) {
            System.out.println(u.getId());
            System.out.println(u.getName());
        }
}*/
}
MetroDao.java

 

我们两个的水平说实话确实不高,想着把页面做的更好看一点,但是地铁查询这个题目看起来简单,但是需要考虑的东西还是蛮多的,反向乘车,按不同需求乘车,等等,算法有好有坏,我们想写自己的最好水平,所以下手慢了一些,好好考虑了一段时间,但是感觉这个项目毕竟是两个人的团队项目,还是稳一点好,基本思路就是 输入起始站,获取并保存id及这一行的所有信息,在输入终点站,获取并保存id及这一行的所有信息,然后返回之间所有id及信息,我们现在还没完成换乘,但是基本查询以及页面完成了

package lxj;
import java.util.List;


/**
 * CourseService
 * �����
 * @author Hu
 *
 */
public class MetroService {

    MetroDao cDao = new MetroDao();

    public List<Metro> list() {
        return cDao.list();
    }
    
}
MetroService.java
package lxj;
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;




@WebServlet("/MetroServlet")
public class MetroServlet extends HttpServlet{
    private static final long serialVersionUID = 1L;
    MetroService service=new MetroService();
    /**
     * 方法选择
     */
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        resp.setContentType("text/html;charset=utf-8");
        String method = req.getParameter("method");
        if ("add".equals(method)) {
    /*        add(req, resp);
        } else if ("del".equals(method)) {
            del(req, resp);
        } else if ("update".equals(method)) {
            update(req, resp);*/
        } else if ("search".equals(method)) {
            search(req, resp);
        /*} else if ("getcoursebyid".equals(method)) {
            getCourseById(req, resp);
        } else if ("getcoursebyname".equals(method)) {
            getCourseByName(req, resp);*/
        } else if ("list".equals(method)) {
            list(req, resp);
        }
    }

    
    
    /**
     * 查询(两个数的)
     * 
     * **/
    
    private void search(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException{
        
        
        req.setCharacterEncoding("UTF-8");
        Metro dao=new Metro();
        
        String sname = req.getParameter("sname");
        String ename = req.getParameter("ename");
        
        Metro user1=MetroDao.search(sname);
        Metro user2=MetroDao.search(ename);
        
        if(user1.getLine()==(user2.getLine())) {
        /*    System.out.println(user1.getLine());
            System.out.println(user2.getLine());*/
                List<Metro> users = new ArrayList<Metro>();
//                User user=new User();
                

                users=MetroDao.Load(user1,user2);
                req.setAttribute("users", users);
                req.getRequestDispatcher("Login.jsp").forward(req,resp);
            }
        else if(user1.getLine()!=user2.getLine()){
            req.setCharacterEncoding("UTF-8");
            req.getRequestDispatcher("123.jsp").forward(req,resp);
        }
            }
            
/*    public static void main(String[] args) {
        List<Metro> users = new ArrayList<Metro>();
        System.out.println("AAAAAA");

        Metro asd=new Metro();
        asd=MetroDao.search("上庄");
        Metro user1=MetroDao.search("上庄");
        Metro user2=MetroDao.search("谈固");
        
        if(user1.getLine()==(user2.getLine())) {
            System.out.println(user1.getLine());
            System.out.println(user2.getLine());
            System.out.println(user1.getName());
            System.out.println(user2.getName());
        }
    }*/
        /**
     * 全部
     * @param req
     * @param resp
     * @throws ServletException 
     */
    private void list(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException{
        req.setCharacterEncoding("utf-8");
        List<Metro> metros = service.list();
        req.setAttribute("metros", metros);
        req.getRequestDispatcher("list.jsp").forward(req,resp);
    }
}
MetroServlet.java
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
<style>
body{
background-image:url(image/9470416_192942306623_2.jpg);
}
</style>
</head>
<body>
<div align="center">
<div class="a">
<h1>由于编代码的技术暂时不够,换乘问题太多,为不给大家带来不便,故暂时不支持换乘,敬请期待!!!</h1>
<div class="a">
</body>
</html>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>    
<head>
<meta charset="UTF-8">
<title>石家庄地铁查询系统</title>
<style>
body{
background-image:url(image/1bc35bcc78134c544dc0672debf19b63.jpeg.jpg);
}
.a{
        font-size: 26px;
        margin-top: 20px;
    }
        .a:hover{font-size:31px;}
</style>
</head>
<body>
<h3>
   今天的日期是: <%= (new java.util.Date()).toLocaleString()%>
</h3>
<div align="center">
<div class="a">
<h1>石家庄地铁查询系统</h1>
<div class="a">
<a href="MetroServlet?method=list">站名列表</a>
</div>
    <div class="a">
            <a href="query.jsp">查询界面</a>
        </div>
</div>
    
</body>
</html>
index.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!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>
    <%
         Object message = request.getAttribute("message");
         if(message!=null && !"".equals(message)){
     
    %>
         <script type="text/javascript">
              alert("<%=request.getAttribute("message")%>");
         </script>
    <%} %>
    <div align="center">
        <h1 style="color: red;">石家庄地铁站信息列表</h1>
    <input type="button" value="返回主界面" onclick="window.location.href='index.jsp';"/><br>
        <table class="tb">
            <tr>
                <td>id</td>
                <td>线路</td>
                <td>站名</td>
            </tr>
            <c:forEach items="${metros}" var="item">
                <tr>
                    <td>${item.id}</td>
                    <td>${item.line}</td>
                    <td>${item.name}</td>
                </tr>
            </c:forEach>
        </table>
    </div>
</body>
</html>
list.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
     <%@page import="lxj.Metro" %>
     <%@page import="lxj.MetroDao" %>
    <%@page import="java.util.List" %>
    <%@page import="java.util.ArrayList" %>
    <%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>显示全部站点</title>
</head>

<body>
    <table>
        <tr>
            <td>站台名称</td>
            <td>线路号</td>
            
        
        </tr>
        <tr>
        <c:forEach items="${users}" var="item">
                <tr>
                
                    <td>${item.line}</td>
                    <td>${item.name}</td>                    
                </tr>
            </c:forEach>
            </table>
<input type="button" value="返回主界面" onclick="window.location.href='index.jsp';"/><br>
</body>
</html>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<%
         Object message = request.getAttribute("message");
         if(message!=null && !"".equals(message)){
     
    %>
         <script type="text/javascript">
              alert("<%=request.getAttribute("message")%>");
         </script>
    <%} %>
    <div align="center">
        <h1 style="color: red;">通过站名查询路线</h1>
        <a href="index.jsp">返回主页</a>
        <form action="MetroServlet?method=search" method="post" onsubmit="return check()">
            <div class="a">
                起始站<input type="text" id="sname" name="sname"/>
            </div>
            <div class="a">
                终点站<input type="text" id="ename" name="ename" />
            </div>
            <div class="a">
                <button type="submit" class="b">&nbsp;&nbsp;&nbsp;</button>
            </div>
            
        </form>
    </div>
</body>
</html>
query.jsp

 

posted on 2019-04-01 14:01  白杰一号  阅读(301)  评论(0编辑  收藏  举报