石家庄地铁查询系统开发进度
开发者:刘世朝 合作伙伴:张小军
基于石家庄地铁规划图,首先我们从一条线路来实现,我们选取了一号线来实现。在数据库中建立了一个名为subway的表。表内有三个属性,分别为id、name、line。name为站名,line为线路号。其源代码如下所示:
common.Subway.java
1 package common; 2 3 public class Subway { 4 private String id; 5 private String name; 6 private String line; 7 public String getId() { 8 return id; 9 } 10 public void setId(String id) { 11 this.id=id; 12 } 13 public String getName() { 14 return name; 15 } 16 public void setName(String name) { 17 this.name=name; 18 } 19 public String getLine() { 20 return line; 21 } 22 public void setLine(String line) { 23 this.line=line; 24 } 25 public Subway() {} 26 public Subway(String id, String name, String line) { 27 this.id = id; 28 this.name = name; 29 this.line=line; 30 } 31 public Subway( String name, String line) { 32 this.name = name; 33 this.line=line; 34 } 35 }
dao.SubwayDao.java:
//这里是构建的方法。
1 package dao; 2 import java.sql.Connection; 3 import java.sql.ResultSet; 4 import java.sql.SQLException; 5 import java.sql.Statement; 6 import java.util.ArrayList; 7 import java.util.List; 8 import common.Subway; 9 import helper.SbHelper; 10 public class SubwayDao { 11 12 /* 13 * 搜索一号线的所有路线 14 */ 15 public List<Subway> subway(String name, String line) { 16 String sql = "select * from subway order by id ASC"; 17 List<Subway> list = new ArrayList<>(); 18 Connection conn = SbHelper.getConn(); 19 Statement state = null; 20 ResultSet rs = null; 21 22 try { 23 state = conn.createStatement(); 24 rs = state.executeQuery(sql); 25 Subway bean = null; 26 while (rs.next()) { 27 String id = rs.getString("id"); 28 String name1 = rs.getString("name"); 29 String line1 = rs.getString("line"); 30 bean = new Subway(id, name1,line1); 31 list.add(bean); 32 } 33 } catch (SQLException e) { 34 e.printStackTrace(); 35 } finally { 36 SbHelper.close(rs, state, conn); 37 } 38 39 return list; 40 } 41 /* 42 * 根据站台名获取该站台的id 43 */ 44 public String id(String name) 45 { 46 String sql="select id from subway where name = '"+name+"'"; 47 Connection conn = SbHelper.getConn(); 48 Statement state = null; 49 String id=null; 50 ResultSet rs = null; 51 try 52 { 53 state=conn.createStatement(); 54 rs = state.executeQuery(sql); 55 while(rs.next()) 56 { 57 id=rs.getString("id"); 58 } 59 }catch (SQLException e) { 60 e.printStackTrace(); 61 } finally { 62 SbHelper.close(rs, state, conn); 63 } 64 return id; 65 66 } 67 /* 68 * 根据站台名获取该站台属于几号线路 69 */ 70 public String line(String name) 71 { 72 String sql="select line from subway where name = '"+name+"'"; 73 Connection conn = SbHelper.getConn(); 74 Statement state = null; 75 String line=null; 76 77 ResultSet rs = null; 78 try 79 { 80 state=conn.createStatement(); 81 rs = state.executeQuery(sql); 82 while(rs.next()) 83 { 84 line=rs.getString("line"); 85 line+="号线"; 86 } 87 }catch (SQLException e) { 88 e.printStackTrace(); 89 } finally { 90 SbHelper.close(rs, state, conn); 91 } 92 return line; 93 94 } 95 /* 96 * 根据获取的id值搜索出其中间的站台升序 97 */ 98 public String station1(int id1,int id2) 99 { 100 String sql="select name from subway where id between '"+id1+"' and '"+id2+"' order by id ASC" ; 101 Connection conn = SbHelper.getConn(); 102 Statement state = null; 103 ResultSet rs = null; 104 String route = ""; 105 try 106 { 107 state=conn.createStatement(); 108 rs = state.executeQuery(sql); 109 if(rs.next()) 110 route=rs.getString("name"); 111 while(rs.next()) 112 { 113 String name=rs.getString("name"); 114 route+="->"+name; 115 } 116 }catch (SQLException e) { 117 e.printStackTrace(); 118 } finally { 119 SbHelper.close(rs, state, conn); 120 } 121 return route; 122 } 123 /* 124 * 根据获取的id值搜索出其中间的站台降序 125 */ 126 public String station2(int id1,int id2) 127 { 128 String sql="select name from subaway where id between '"+id1+"' and '"+id2+"' order by id DESC" ; 129 Connection conn = SbHelper.getConn(); 130 Statement state = null; 131 ResultSet rs = null; 132 String route = ""; 133 try 134 { 135 state=conn.createStatement(); 136 rs = state.executeQuery(sql); 137 if(rs.next()) 138 route=rs.getString("name"); 139 while(rs.next()) 140 { 141 String name=rs.getString("name"); 142 route+="->"+name; 143 } 144 }catch (SQLException e) { 145 e.printStackTrace(); 146 } finally { 147 SbHelper.close(rs, state, conn); 148 } 149 return route; 150 } 151 }
helper.SbHleper.java:
1 package helper; 2 3 import java.sql.Connection; 4 import java.sql.DriverManager; 5 import java.sql.ResultSet; 6 import java.sql.SQLException; 7 import java.sql.Statement; 8 9 public class SbHelper { 10 public static String driverName="com.microsoft.sqlserver.jdbc.SQLServerDriver"; 11 public static String dbURL="jdbc:sqlserver://127.0.0.1:1433;DatabaseName=java"; 12 public static String userName="sa"; 13 public static String userPwd="995893"; 14 15 public static Connection getConn () { 16 Connection conn = null; 17 18 try { 19 Class.forName(driverName);;//加载驱动 20 conn = DriverManager.getConnection(dbURL, dbURL, userPwd); 21 22 } catch (Exception e) { 23 e.printStackTrace(); 24 } 25 26 return conn; 27 } 28 29 /** 30 * 关闭连接 31 * @param state 32 * @param conn 33 */ 34 public static void close (Statement state, Connection conn) { 35 if (state != null) { 36 try { 37 state.close(); 38 } catch (SQLException e) { 39 e.printStackTrace(); 40 } 41 } 42 43 if (conn != null) { 44 try { 45 conn.close(); 46 } catch (SQLException e) { 47 e.printStackTrace(); 48 } 49 } 50 } 51 52 public static void close (ResultSet rs, Statement state, Connection conn) { 53 if (rs != null) { 54 try { 55 rs.close(); 56 } catch (SQLException e) { 57 e.printStackTrace(); 58 } 59 } 60 61 if (state != null) { 62 try { 63 state.close(); 64 } catch (SQLException e) { 65 e.printStackTrace(); 66 } 67 } 68 69 if (conn != null) { 70 try { 71 conn.close(); 72 } catch (SQLException e) { 73 e.printStackTrace(); 74 } 75 } 76 } 77 78 }
servlet.SubwayServlet.java
1 package servlet; 2 import java.io.IOException; 3 import java.util.List; 4 import javax.servlet.ServletException; 5 import javax.servlet.http.HttpServlet; 6 import javax.servlet.http.HttpServletRequest; 7 import javax.servlet.http.HttpServletResponse; 8 9 10 import common.Subway; 11 import dao.SubwayDao; 12 public class SubwayServlet extends HttpServlet { 13 private static final long serialVersionUID = 1L; 14 15 SubwayDao dao=new SubwayDao(); 16 17 protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException 18 { 19 req.setCharacterEncoding("utf-8"); 20 String method = req.getParameter("method"); 21 if ("one_line".equals(method)) 22 { 23 one_line(req, resp); 24 }else if("no_transfer1".equals(method)) 25 { 26 no_transfer1(req, resp); 27 } 28 } 29 /* 30 * 搜索一号线的所有路线 31 */ 32 private void one_line(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException{ 33 req.setCharacterEncoding("utf-8"); 34 String line = req.getParameter("line"); 35 String name = req.getParameter("name"); 36 List<Subway> ones = dao.subway(name,line); 37 req.setAttribute("ones", ones); 38 req.getRequestDispatcher("one_line_searchlist.jsp").forward(req,resp); 39 } 40 /* 41 * 根据站台名查询出其中间站台 42 */ 43 private void no_transfer1(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException{ 44 req.setCharacterEncoding("utf-8"); 45 46 String name= req.getParameter("name"); //从网页获取输入的站台 47 int id1 = Integer.parseInt(dao.id(name)); //获取其站台id 将其转换为int类型 48 49 String name2 = req.getParameter("name2"); 50 int id2 = Integer.parseInt(dao.id(name2)); 51 52 if(id1<id2) 53 { 54 String station=dao.station1(id1, id2); 55 req.setAttribute("stations", station); 56 57 58 String line=dao.line(name); 59 System.out.print(line); 60 req.setAttribute("lines", line); 61 62 req.getRequestDispatcher("no_transfer_searchlist.jsp").forward(req,resp);//升序车站 63 req.getRequestDispatcher("no_transfer_searchlist.jsp").forward(req,resp);//升序车站 64 }else 65 { 66 67 String station=dao.station2(id2, id1); 68 req.setAttribute("stations", station); 69 70 71 String line=dao.line(name); 72 System.out.print(line); 73 req.setAttribute("lines", line); 74 75 req.getRequestDispatcher("no_transfer_searchlist.jsp").forward(req,resp);//降序车站 76 req.getRequestDispatcher("no_transfer_searchlist.jsp").forward(req,resp);//升序车站 77 } 78 79 } 80 81 }
下面是jsp文件。
one_line_search.jsp
1 <%@ page language="java" contentType="text/html; charset=utf-8" 2 pageEncoding="utf-8"%> 3 <!DOCTYPE html> 4 <html> 5 <head> 6 <meta charset="utf-8"> 7 <title>一号线</title> 8 <style> 9 .a{ 10 margin-top: 20px; 11 } 12 .b{ 13 font-size: 20px; 14 width: 160px; 15 color: white; 16 background-color: greenyellow; 17 } 18 </style> 19 </head> 20 <body> 21 <div align="center"> 22 <form action="SubwayServlet?method=one_line" method="post" onsubmit="return check()"> 23 <div class="a"> 24 一号线 25 </div> 26 <div class="a"> 27 <button type="submit" class="b">查 询</button> 28 </div> 29 </form> 30 </div> 31 </body> 32 </html>
one_line_searchlist.jsp
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%> <!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> <div align="center"> <table class="tb"> <tr> <td>站台</td> <td>线路</td> <td>起点站——终点站</td> <td>站台</td> </tr> <!-- forEach遍历出adminBeans --> <c:forEach items="${ones}" var="item" varStatus="status"> <tr> <td>${item.id}</td> <td><a>${item.line}</a></td> <td>${item.intro}</td> <td>${item.route}</td> <td>${item.name}</td> </tr> </c:forEach> </table> </div> </body> </html>
no_transfer_search.jsp
1 <%@ page language="java" contentType="text/html; charset=UTF-8" 2 pageEncoding="UTF-8"%> 3 <!DOCTYPE html> 4 <html> 5 <head> 6 <meta charset="UTF-8"> 7 <title>Insert title here</title> 8 <style> 9 .a{ 10 margin-top: 20px; 11 } 12 .b{ 13 font-size: 20px; 14 width: 160px; 15 color: white; 16 background-color: greenyellow; 17 } 18 </style> 19 </head>。 20 <body> 21 <div align="center"> 22 <form action="SubwayServlet?method=no_transfer1" method="post" onsubmit="return check()"> 23 <div class="a"> 24 起点站<input type="text" id="name" name="name"/> 25 </div> 26 <div class="a"> 27 终点站<input type="text" id="name2" name="name2" /> 28 </div> 29 <div class="a"> 30 <button type="submit" class="b">查 询</button> 31 </div> 32 </form> 33 </div> 34 35 </body> 36 </html>
no_transfer_searchlist.jsp
1 <%@ page language="java" contentType="text/html; charset=UTF-8" 2 pageEncoding="UTF-8"%> 3 4 <!DOCTYPE html> 5 <html> 6 <head> 7 <meta charset="UTF-8"> 8 <title>Insert title here</title> 9 <style> 10 .a{ 11 margin-top: 20px; 12 } 13 .b{ 14 font-size: 20px; 15 width: 160px; 16 color: white; 17 background-color: greenyellow; 18 } 19 .tb, td { 20 border: 1px solid black; 21 font-size: 22px; 22 } 23 </style> 24 </head> 25 <body> 26 <div align="center"> 27 <table class="tb"> 28 <tr> 29 30 <td></td> 31 <td>线路</td> 32 <td>车次</td> 33 <tr></tr> 34 35 <tr> 36 <td>中间站</td> 37 <td><%=request.getAttribute("stations") %></td> 38 <td><%=request.getAttribute("lines") %></td> 39 </tr> 40 41 </table> 42 </div> 43 </body> 44 </html>
数据库中的表(这个学期开了数据库,所以使用的是SQL server):
运行结果:
虽然系统没有报错,但在最后运行时如上图所示出了错。找了许久我们目前还是没找出错误。以上是我们当前的开发进度。