chezhan.java
package bean; public class chezhan { int ID; String name; String no1; String no2; String no3; String no4; public int getID() { return ID; } public void setID(int iD) { ID = iD; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getNo1() { return no1; } public void setNo1(String no1) { this.no1 = no1; } public String getNo2() { return no2; } public void setNo2(String no2) { this.no2 = no2; } public String getNo3() { return no3; } public void setNo3(String no3) { this.no3 = no3; } public String getNo4() { return no4; } public void setNo4(String no4) { this.no4 = no4; } public chezhan(int iD,String name,String no1,String no2,String no3,String no4) { ID = iD; this.name = name; this.no1 = no1; this.no2 = no2; this.no3 = no3; this.no4 = no4; } }
dao.java
package dao; import java.sql.Connection; import java.util.ArrayList; import java.util.List; import java.sql.ResultSet; import java.sql.Statement; import dbutil.DBUtil; import bean.*; import java.util.Collections; public class dao { public static List<chezhan> selectkluxian(String chezhan1) { String sql = "select * from chezhan where no1="+chezhan1; Connection conn = DBUtil.getConn();//这里就是从DBUtil类里面得到连接 Statement state =null; List<chezhan> list = new ArrayList<>(); ResultSet rs = null; int flag=0; try { state = conn.createStatement(); rs=state.executeQuery(sql); chezhan kc=null; while(rs.next()) { int ID=rs.getInt("ID"); String name=rs.getString("name"); String no1=rs.getString("no1"); String no2=rs.getString("no2"); String no3=rs.getString("no3"); String no4=rs.getString("no4"); kc =new chezhan(ID,name,no1,no2,no3,no4); //System.out.println(name+"========="); list.add(kc); } } catch(Exception e) { e.printStackTrace(); } finally { DBUtil.close(rs, state, conn); } return list; } public static List<chezhan> selectkzhantai(String no) { //System.out.println("22222222222222"); String sql = "select * from chezhan where name='"+no+"'"; Connection conn = DBUtil.getConn();//这里就是从DBUtil类里面得到连接 Statement state =null; List<chezhan> list = new ArrayList<>(); ResultSet rs = null; chezhan kc=null; try { state = conn.createStatement(); rs=state.executeQuery(sql); while(rs.next()) { int ID=rs.getInt("ID"); String name=rs.getString("name"); String no1=rs.getString("no1"); String no2=rs.getString("no2"); String no3=rs.getString("no3"); String no4=rs.getString("no4"); System.out.println(name); no3="1"; if(rs.getString("no3")==null) no3=" "; if(rs.getString("no2")==null) no2=" "; if(rs.getString("no4")==null) no4=" "; kc =new chezhan(ID,name,no1,no2,no3,no4); list.add(kc); } } catch(Exception e) { e.printStackTrace(); } finally { DBUtil.close(rs, state, conn); } return list; } public static List<chezhan> chaxunluxian(String name1,String name2) { //System.out.println("2--------"); String sql = "select * from chezhan where name='"+name1+"'"; Connection conn = DBUtil.getConn();//这里就是从DBUtil类里面得到连接 Statement state =null; List<chezhan> list = new ArrayList<>(); ResultSet rs = null; List<chezhan> kc1=selectkzhantai(name2); // System.out.println(kc1.get(0).getNo1()); try { chezhan kc=null; state = conn.createStatement(); rs=state.executeQuery(sql); while(rs.next()) { int ID=rs.getInt("ID"); String name=rs.getString("name"); String no1=rs.getString("no1"); String no2=rs.getString("no2"); String no3=rs.getString("no3"); String no4=rs.getString("no4"); kc =new chezhan(ID,name,no1,no2,no3,no4); //list4.add(kc); //System.out.println("++++++++++"); System.out.println(kc.getNo1()+" 11111"); System.out.println(kc1.get(0).getNo1()+" 222"); if(kc.getNo1().equals(kc1.get(0).getNo1())) { //System.out.println("------------"); list=yitiaoxian(kc.getID(),kc1.get(0).getID(),1); break; } else { List<chezhan> listzhuancheng1 = new ArrayList<>(); List<chezhan> list2 = new ArrayList<>(); int huanchengID=xunzhaohuanchengdian(no1,ID); listzhuancheng1=fanhuizhuanzhanxinxi(huanchengID); list=yitiaoxian(ID,huanchengID,1); huanchengID=changeid(huanchengID,listzhuancheng1.get(0).getName()); list2=yitiaoxian(huanchengID,kc1.get(0).getID(),2); list.addAll(list2); } } } catch(Exception e) { e.printStackTrace(); } finally { DBUtil.close(rs, state, conn); } return list; } public static List<chezhan> yitiaoxian(int id1,int id2,int i) { String sql; if(i==1) { if(id1<=id2) { sql = "select * from chezhan where ID>='"+id1+"' and ID<='"+id2+"' "; } else { sql = "select * from chezhan where ID>='"+id2+"' and ID<='"+id1+"' Order By ID Desc " ; } } else { if(id1<=id2) { sql = "select * from chezhan where ID>'"+id1+"' and ID<='"+id2+"' "; } else { sql = "select * from chezhan where ID>'"+id2+"' and ID<='"+id1+"' Order By ID Desc " ; } } Connection conn = DBUtil.getConn();//这里就是从DBUtil类里面得到连接 Statement state =null; List<chezhan> list = new ArrayList<>(); ResultSet rs = null; try { state = conn.createStatement(); rs=state.executeQuery(sql); chezhan kc=null; while(rs.next()) { int ID=rs.getInt("ID"); String name=rs.getString("name"); String no1=rs.getString("no1"); String no2=rs.getString("no2"); String no3=rs.getString("no3"); String no4=rs.getString("no4"); kc =new chezhan(ID,name,no1,no2,no3,no4); //System.out.println(name+"========="); list.add(kc); } } catch(Exception e) { e.printStackTrace(); } finally { DBUtil.close(rs, state, conn); } return list; } public static int xunzhaohuanchengdian(String No1,int Id) { int min=-9999; int dingwei=1; int w=0; String sql = "select ID from chezhan where no1='"+No1+"' and no2!='"+null+"'"; Connection conn = DBUtil.getConn();//这里就是从DBUtil类里面得到连接 Statement state =null; ResultSet rs = null; try { state = conn.createStatement(); rs=state.executeQuery(sql); while(rs.next()) { w++; int ID=rs.getInt("ID"); //System.out.println(ID+" 1"); if(w==1) { min=Math.abs(ID-Id); } if(min>Math.abs(ID-Id)) { dingwei=ID; min=Math.abs(ID-Id); //System.out.println(dingwei+" 1111111111111"); } //System.out.println(name+"========="); } } catch(Exception e) { e.printStackTrace(); } finally { DBUtil.close(rs, state, conn); } return dingwei; } public static List<chezhan> fanhuizhuanzhanxinxi(int id) { String sql = "select * from chezhan where ID='"+id+"' "; Connection conn = DBUtil.getConn();//这里就是从DBUtil类里面得到连接 Statement state =null; List<chezhan> list = new ArrayList<>(); ResultSet rs = null; try { state = conn.createStatement(); rs=state.executeQuery(sql); chezhan kc=null; while(rs.next()) { int ID=rs.getInt("ID"); String name=rs.getString("name"); String no1=rs.getString("no1"); String no2=rs.getString("no2"); String no3=rs.getString("no3"); String no4=rs.getString("no4"); kc =new chezhan(ID,name,no1,no2,no3,no4); //System.out.println(name+"========="); list.add(kc); } } catch(Exception e) { e.printStackTrace(); } finally { DBUtil.close(rs, state, conn); } return list; } public static int changeid(int Id,String Name) { int ID=1; String sql = "select ID from chezhan where name='"+Name+"' and ID!='"+Id+"'"; Connection conn = DBUtil.getConn();//这里就是从DBUtil类里面得到连接 Statement state =null; ResultSet rs = null; try { state = conn.createStatement(); rs=state.executeQuery(sql); while(rs.next()) { ID=rs.getInt("ID"); } } catch(Exception e) { e.printStackTrace(); } finally { DBUtil.close(rs, state, conn); } return ID; } }
DBUtil.java
package dbutil; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; public class DBUtil {//这个类就是套模板如果需要用的时候只需要改数据库(db)的名字即可******* //public static String db_url="jdbc:mysql://localhost:3306/database?uerUnicode=true&characterEncoding=UTF-8"; public static String db_url="jdbc:mysql://localhost:3306/ditie?serverTimezone=GMT%2B8&serverTimezone=GMT%2B8&useUnicode=true&characterEncoding=UTF-8";//db是数据库的名字 //这是连接数据库,servlet是数据库的名称,uerUnicode=true&characterEncoding=UTF-8是将字符集设置为utf-8,避免乱码。 public static String db_user="root";//数据的用户名 public static String db_password="UJuqCT-az5(f";//数据库的密码 public static Connection getConn()//获取连接,返回Connection类型,必须设置为static这样才能在其他类中使用 { Connection conn=null; try { Class.forName("com.mysql.cj.jdbc.Driver");//加载驱动 conn=DriverManager.getConnection(db_url,db_user,db_password);//连接数据库 } catch(Exception e) { e.printStackTrace(); } return 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.java
package servlet; import java.io.BufferedReader; import java.io.IOException; import java.util.List; import java.io.PrintWriter; 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 dao.*; import bean.*; import dao.*; @WebServlet("/r2")//r3是资源路径 public class Servlet extends HttpServlet{ public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, java.io.IOException { /* * 利用post获取体数据进行使用 //获取请求消息体--请求参数 //1 获取字符流 BufferedReader br=request.getReader(); //2 读取数据 String line=null; while((line==br.readLine()) ) { System.out.println("line"); } */ return; } protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { req.setCharacterEncoding("utf-8");// 设置字符集,避免乱码 // 获取jsp界面需要进行的操作, String method = req.getParameter("method");//1 req.getParameter("参数名")根据参数名获取参数值 //2 String[] getParametervalue("参数名"):根据参数名称获取参数值的数组 //3 Enumeration<String> getParameterNames():获取所有请求参数名称 如果是数组的话只能获取到第一个 //4 Map<String,String[]> getParameterMap():获取所有参数map集合 就是字符或者字符串都能存储 //System.out.println("============"); if (method.equals("xianlu")) { selectluxian(req,resp); } if (method.equals("zhantai")) { selectzhantai(req,resp); } if (method.equals("chaxun")) { chaxunluxian(req,resp); } } private void selectluxian(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { req.setCharacterEncoding("utf-8"); String luxian=req.getParameter("luxian"); // System.out.println(req.getParameter("luxian")+"========="); List<chezhan> kc=dao.selectkluxian(luxian); String inf1="共拥有"+kc.size()+"站"; req.setAttribute("kc", kc); req.setAttribute("inf1", inf1); req.getRequestDispatcher("1daohang.jsp").forward(req, resp); } private void selectzhantai(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { req.setCharacterEncoding("utf-8"); String zhantai=req.getParameter("name"); // System.out.println(req.getParameter("name")+"========="); List<chezhan> kc=dao.selectkzhantai(zhantai); req.setAttribute("kc", kc); req.getRequestDispatcher("1xianlu.jsp").forward(req, resp); } private void chaxunluxian(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { req.setCharacterEncoding("utf-8"); String name1=req.getParameter("name1"); String name2=req.getParameter("name2"); List<chezhan> kc=dao.chaxunluxian(name1,name2); req.setAttribute("kc", kc); req.getRequestDispatcher("1zhantai.jsp").forward(req, resp); } }
1.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> <link rel="stylesheet" href="layui/css/layui.css"> <script src="layui/layui.js"></script> </head> <body> <div class="layui-layout layui-layout-admin"> <div class="layui-header"> <div class="layui-logo layui-hide-xs layui-bg-black">layout demo</div> <!-- 头部区域(可配合layui 已有的水平导航) --> <ul class="layui-nav layui-layout-left"> <!-- 移动端显示 --> <li class="layui-nav-item layui-show-xs-inline-block layui-hide-sm" lay-header-event="menuLeft"> <i class="layui-icon layui-icon-spread-left"></i> </li> <li class="layui-nav-item layui-hide-xs"><a href="">nav 1</a></li> <li class="layui-nav-item layui-hide-xs"><a href="">nav 2</a></li> <li class="layui-nav-item layui-hide-xs"><a href="">nav 3</a></li> <li class="layui-nav-item"> <a href="javascript:;">nav groups</a> <dl class="layui-nav-child"> <dd><a href="">menu 11</a></dd> <dd><a href="">menu 22</a></dd> <dd><a href="">menu 33</a></dd> </dl> </li> </ul> <ul class="layui-nav layui-layout-right"> <li class="layui-nav-item layui-hide layui-show-md-inline-block"> <a href="javascript:;"> <img src="//tva1.sinaimg.cn/crop.0.0.118.118.180/5db11ff4gw1e77d3nqrv8j203b03cweg.jpg" class="layui-nav-img"> tester </a> <dl class="layui-nav-child"> <dd><a href="">Your Profile</a></dd> <dd><a href="">Settings</a></dd> <dd><a href="">Sign out</a></dd> </dl> </li> <li class="layui-nav-item" lay-header-event="menuRight" lay-unselect> <a href="javascript:;"> <i class="layui-icon layui-icon-more-vertical"></i> </a> </li> </ul> </div> <div class="layui-side layui-bg-black"> <div class="layui-side-scroll"> <!-- 左侧导航区域(可配合layui已有的垂直导航) --> <ul class="layui-nav layui-nav-tree" lay-filter="test"> <li class="layui-nav-item layui-nav-itemed"> <a class="" href="javascript:;">menu group 1</a> <dl class="layui-nav-child"> <dd><a href="javascript:;">menu 1</a></dd> <dd><a href="javascript:;">menu 2</a></dd> <dd><a href="javascript:;">menu 3</a></dd> <dd><a href="">the links</a></dd> </dl> </li> <li class="layui-nav-item"> <a href="javascript:;">menu group 2</a> <dl class="layui-nav-child"> <dd><a href="javascript:;">list 1</a></dd> <dd><a href="javascript:;">list 2</a></dd> <dd><a href="">超链接</a></dd> </dl> </li> <li class="layui-nav-item"><a href="javascript:;">click menu item</a></li> <li class="layui-nav-item"><a href="">the links</a></li> </ul> </div> </div> <div class="layui-body"> <!-- 内容主体区域 --> <div style="padding: 15px;">内容主体区域。记得修改 layui.css 和 js 的路径</div> </div> <div class="layui-footer"> <!-- 底部固定区域 --> 底部固定区域 </div> </div> <script src="./layui/layui.js"></script> <script> //JS layui.use(['element', 'layer', 'util'], function(){ var element = layui.element ,layer = layui.layer ,util = layui.util ,$ = layui.$; //头部事件 util.event('lay-header-event', { //左侧菜单事件 menuLeft: function(othis){ layer.msg('展开左侧菜单的操作', {icon: 0}); } ,menuRight: function(){ layer.open({ type: 1 ,content: '<div style="padding: 15px;">处理右侧面板的操作</div>' ,area: ['260px', '100%'] ,offset: 'rt' //右上角 ,anim: 5 ,shadeClose: true }); } }); }); </script> </body> </html>
1daohang.jsp
<%@page import="java.util.*"%> <%@page import="bean.*"%> <%@page import="dao.*"%> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%> <%@ 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> <link rel="stylesheet" href="layui/css/layui.css"> <script src="layui/layui.js"></script> <style> </style> </head> <body> <ul class="layui-nav layui-nav-child-c" lay-filter=""> <li class="layui-nav-itemm"><a href="1daohang.jsp">站点查询</a></li> <li class="layui-nav-item"><a href="1xianlu.jsp">线路查询</a></li> <li class="layui-nav-item"><a href="1zhantai.jsp">起点-终点查询</a></li> </ul> <form action="/chezhan/r2?method=xianlu" method="post" aligen="center"> <div class="layui-inline"> <input type="text" name="luxian" required lay-verify="required" placeholder="请输入线路号" autocomplete="off" class="layui-input"> <input class="layui-btn layui-btn-sm" type="submit" value="查询"> </div> </form> ${inf1} <table class="layui-table" layui-data= "{skin: even:true, } "> <tr> <th>站点名称</th> <th>所属线路</th> <th>站点ID</th> </tr> <c:forEach items="${kc}" var="chezhan"> <tr> <td><c:out value="${chezhan.name}"></c:out></td> <td><c:out value="${chezhan.no1}"></c:out></td> <td><c:out value="${chezhan.ID}"></c:out></td> </tr> </c:forEach> </table> <%-- <%List<chezhan> list=(List<chezhan>)request.getAttribute("kc"); for(chezhan kc : list){ %> <%=kc.getName() %> <% } %> --%> <script type="text/javascript"> layui.use('element',function (){ var element=layui.element; }); </script> </body> </html>
1xianlu.jsp
<%@page import="java.util.*"%> <%@page import="bean.*"%> <%@page import="dao.*"%> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%> <%@ 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> <link rel="stylesheet" href="layui/css/layui.css"> <script src="layui/layui.js"></script> <style> </style> </head> <body> <ul class="layui-nav layui-nav-child-c" lay-filter=""> <li class="layui-nav-item"><a href="1daohang.jsp">站点查询</a></li> <li class="layui-nav-item"><a href="1xianlu.jsp">线路查询</a></li> <li class="layui-nav-item"><a href="1zhantai.jsp">起点-终点查询</a></li> </ul> <form action="/chezhan/r2?method=zhantai" method="post" aligen="center"> <div class="layui-inline"> <input type="text" name="name" required lay-verify="required" placeholder="请输入站台号" autocomplete="off" class="layui-input"> <input class="layui-btn layui-btn-sm" type="submit" value="查询"> </div> </form> ${inf1} <table class="layui-table" layui-data= "{skin: even:true, } "> <tr> <th>站点名称</th> <th>所属线路</th> <th>站点ID</th> </tr> <c:forEach items="${kc}" var="chezhan"> <tr> <td><c:out value="${chezhan.name}"></c:out></td> <td><c:out value="${chezhan.no1}"></c:out></td> <td><c:out value="${chezhan.ID}"></c:out></td> </tr> </c:forEach> </table> <script type="text/javascript"> layui.use('element',function (){ var element=layui.element; }); </script> </body> </html>
1zhantai.jsp
<%@page import="java.util.*"%> <%@page import="bean.*"%> <%@page import="dao.*"%> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%> <%@ 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> <link rel="stylesheet" href="layui/css/layui.css"> <script src="layui/layui.js"></script> <style> </style> </head> <body> <ul class="layui-nav layui-nav-child-c" lay-filter=""> <li class="layui-nav-item"><a href="1daohang.jsp">站点查询</a></li> <li class="layui-nav-item"><a href="1xianlu.jsp">线路查询</a></li> <li class="layui-nav-item"><a href="1zhantai.jsp">起点-终点查询</a></li> </ul> <form action="/chezhan/r2?method=chaxun" method="post" aligen="center"> <div class="layui-container"> <div class="layui-row"> <div class="layui-col-md5"> </div> <div class="layui-col-md3"> <input type="text" name="name1" required lay-verify="required" placeholder="请输入起点站" autocomplete="off" class="layui-input"> </div> <div class="layui-col-md2"> <input type="text" name="name2" required lay-verify="required" placeholder="请输入终点站" autocomplete="off" class="layui-input"> </div> <div class="layui-col-md2"> <input class="layui-btn layui-btn-sm" type="submit" value="查询"> </div> </div></div> </form> ${inf1} <table class="layui-table" layui-data= "{skin: even:true, } "> <tr> <th>站点名称</th> <th>所属线路</th> <th>站点ID</th> </tr> <c:forEach items="${kc}" var="chezhan"> <tr> <td><c:out value="${chezhan.name}"></c:out></td> <td><c:out value="${chezhan.no1}"></c:out></td> <td><c:out value="${chezhan.ID}"></c:out></td> </tr> </c:forEach> </table> <script type="text/javascript"> layui.use('element',function (){ var element=layui.element; }); </script> </body> </html>
标签:北京地铁,String,rs,车站,系统,public,state,ID,name From: https://www.cnblogs.com/1774323810com/p/16855143.html