设计思路:首先在jsp中录入起始站和终点站,传值进入servlet,通过servlet中的方法调用dao层方法,去查询数据库中两个站点是否在一条线上,接受返回值,返回值为一个对象,通过对象调取方法,检测两个站点的number是否相同,相同则为同一条线,如不相同,则需换乘,目前只做到了一条线的,换乘的还在进行中,还未写完方法,一条线的需在调用一个方法,通过id将中间地站点输出,存在一个对象中,然后在另一个jsp中通过循环读取出来,一条线的可以成功运行,需换成的还在开发中
合作人:信1705-2 张利杰
代码:
package Dao; import java.sql.Connection; import java.sql.Statement; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import java.util.ArrayList; import java.util.List; import com.mysql.jdbc.Util; import Entity.User; import DB.DB; import java.sql.*; public class Dao { static String sql = "select * from first"; private static final Statement DBUtil = null; public User search(String sname) { User user = new User(); String sql = "select * from first where name='" + sname + "'"; Connection conn = DB.getConn(); Statement state = null; ResultSet rs = null; String number = null; try { state = conn.createStatement(); rs = state.executeQuery(sql); while (rs.next()) { number = rs.getString("number"); int id = rs.getInt("id"); String name = rs.getString("name"); String beizhu = rs.getString("beizhu"); user = new User(id , number, name,beizhu); } } catch (SQLException e) { e.printStackTrace(); } finally { DB.close(rs, state, conn); } return user; } public List<User> Load(User user1 , User user2) { int a= user1.getId(); int b=user2.getId(); if(user1.getNumber().equals(user2.getNumber())) { Connection conn = DB.getConn(); String sql="select * from first where id between "+ a +" and "+ b +" "; Statement state = null; ResultSet rs = null; List<User> users = new ArrayList<User>(); User user = null; try { state = conn.createStatement(); rs = state.executeQuery(sql); while(rs.next()) { user = new User(); user.setId(rs.getInt("id")); user.setNumber(rs.getString("number")); user.setName(rs.getString("name")); user.setBeizhu(rs.getString("beizhu")); users.add(user); System.out.println(user); } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); }finally { DB.close(rs,state, conn); } return users; } return null; } }
package DB; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; public class DB { public static String db_url = "jdbc:mysql://localhost:3306/test"; public static String db_user = "root"; public static String db_pass = "15568958907lx"; 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) throws SQLException { Connection conn = getConn(); PreparedStatement pstmt = null; ResultSet rs = null; String sql ="select * from USER"; pstmt = conn.prepareStatement(sql); rs = pstmt.executeQuery(); if(rs.next()){ System.out.println("缁岋拷"); }else{ System.out.println("娑撳秶鈹�"); } } }
package Entity; public class subway { private String name; private String number; public String getName() { return name; } public void setName(String name) { this.name = name; } public String getNumber() { return number; } public void setNumber(String number) { this.number = number; } public subway(String name,String number) { this.name = name; this.number = number; } }
package Entity; public class User { private int id; private String number; private String name; private String beizhu; public User() { } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getNumber() { return number; } public void setNumber(String number) { this.number = number; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getBeizhu() { return beizhu; } public void setBeizhu(String beizhu) { this.beizhu = beizhu; } public User(int id,String number,String name,String beizhu) { this.id=id; this.number = number; this.name = name; this.beizhu = beizhu; } }
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 Dao.Dao; import Entity.User; /** * Servlet implementation class Servlet */ @WebServlet("/Servlet") public class Servlet extends HttpServlet { private static final long serialVersionUID = 1L; /** * @see HttpServlet#HttpServlet() */ public Servlet() { super(); // TODO Auto-generated constructor stub } /** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */ protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { // TODO Auto-generated method stub //response.getWriter().append("Served at: ").append(request.getContextPath()); req.setCharacterEncoding("UTF-8"); Dao dao = new Dao(); String sname = req.getParameter("sname"); String ename = req.getParameter("ename"); User user1 = dao.search(sname); User user2 = dao.search(ename); if(user1.getNumber().equals(user2.getNumber())) { System.out.println(user1.getNumber()); System.out.println(user2.getNumber()); List<User> users = new ArrayList<User>(); // User user=new User(); users=dao.Load(user1,user2); req.setAttribute("users", users); req.getRequestDispatcher("login.jsp").forward(req,resp); } else if (!(user1.getNumber().equals(user2.getNumber()))) { System.out.println(user1.getNumber()); System.out.println(user2.getNumber()); List<User> users = new ArrayList<User>(); // User user=new User(); users=dao.Load(user1,user2); req.setAttribute("users", users); req.getRequestDispatcher("login.jsp").forward(req,resp); } } /** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub doGet(request, response); } }
<%@ 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>石家庄地铁信息查询系统</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"> <form action="Servlet" method="post" onsubmit="return check()"> <div class="a"> <p style="text-align:center">始发站<input type="text" size="20" id="sname" name="sname"/></p> </div> <div class="a"> <p style="text-align:center">终点站<input type="text" size="20" id="ename" name="ename" /></p> </div> <div class="a"> <button type="submit" class="b">查 询</button> </div> </form> </div> <script type="text/javascript"> function check() { var username = document.getElementById("sname");; var password = document.getElementById("ename"); //非空 if(username.value == '') { alert('始发站不能为空'); same.focus(); return false; } if(password.value == '') { alert('终点站不能为空'); ename.focus(); return false; } } </script> </body> </html>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@page import="Dao.Dao" %> <%@page import="Entity.User" %> <%@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.name}</td> <td>${item.number}</td> </tr> </c:forEach> </table> </tr> </table> **********************<br> <input type="button" value="返回功能界面" onclick="window.location.href='index.jsp';"/><br> **********************<br> </body> </html> </body> </html>
实验截图:
实验总结:由于基础低,导致写起来非常困难,也借助了别人的力量,通过讲解理解了容器的用处,弥补了我之前只能接受一个变量的返回值,现在学会了接受对象的返回值,总体来说还是有进步,目前在写查询换乘站的算法,写完就可以实现换乘了。