设计思想:用六个表存储六条线上的站点信息,另用一个表jiedian存储六条线间的交点信息,
网页获取起点,终点的站点名及其对应的线路号,然后通过一些方法调出数据库中对应表的数据并存在数组中,
然后在jiedian表中查询出两条线的交点(如果只有一条线就不会有这一步),最后在两条线中查询交点所在位置,进而分(起点——交点——终点)两段输出。(队友:何伟豪)
package util; 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:3306/subway?serverTimezone=UTC"; public static String db_user = "root"; public static String db_pass = "root"; 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(); } } } } package dao; //用于从数据库获取数据并以ZhanDian类的形式输出 import java.sql.Connection; import java.sql.ResultSet; import java.sql.Statement; import entity.ZhanDian; import util.DBUtil; public class SubwayDao { //通过id和表名获取对应表的对应站点 public ZhanDian getCourseByBiaoId(String biao,int id){ String sql = "select * from " + biao + " where id ='" + id + "'"; Connection conn = DBUtil.getConn(); Statement state = null; ResultSet rs = null; ZhanDian zhandian = null; try { state = conn.createStatement(); rs = state.executeQuery(sql); while (rs.next()) { String name = rs.getString("name"); zhandian = new ZhanDian(id,name); } } catch (Exception e) { e.printStackTrace(); } finally { DBUtil.close(rs, state, conn); } return zhandian; } /* //通过id获取对应1的站点 public ZhanDian getCourseById_1(int id){ String sql = "select * from one where id ='" + id + "'"; Connection conn = DBUtil.getConn(); Statement state = null; ResultSet rs = null; ZhanDian zhandian = null; try { state = conn.createStatement(); rs = state.executeQuery(sql); while (rs.next()) { String name = rs.getString("name"); zhandian = new ZhanDian(id,name); } } catch (Exception e) { e.printStackTrace(); } finally { DBUtil.close(rs, state, conn); } return zhandian; } //通过id获取对应3的站点 public ZhanDian getCourseById_3(int id){ String sql = "select * from three where id ='" + id + "'"; Connection conn = DBUtil.getConn(); Statement state = null; ResultSet rs = null; ZhanDian zhandian = null; try { state = conn.createStatement(); rs = state.executeQuery(sql); while (rs.next()) { String name = rs.getString("name"); zhandian = new ZhanDian(id,name); } } catch (Exception e) { e.printStackTrace(); } finally { DBUtil.close(rs, state, conn); } return zhandian; } */ //通过line获取jiedian表中的对应的中转站点 public ZhanDian getCourseByLine(int line){ String sql = "select * from jiedian where line ='" + line + "'"; Connection conn = DBUtil.getConn(); Statement state = null; ResultSet rs = null; ZhanDian zhandian = null; try { state = conn.createStatement(); rs = state.executeQuery(sql); while (rs.next()) { String name = rs.getString("name"); zhandian = new ZhanDian(name); } } catch (Exception e) { e.printStackTrace(); } finally { DBUtil.close(rs, state, conn); } return zhandian; } //通过表名、站点名获取对应表中的对应id public int getIdByBNName(String biao,String name) { String sql ="select * from " + biao + " where name = '" + name + "'"; Connection conn = DBUtil.getConn(); Statement state = null; ResultSet rs = null; ZhanDian zhandian = null; try { state = conn.createStatement(); rs = state.executeQuery(sql); while (rs.next()) { int id = rs.getInt("id"); zhandian = new ZhanDian(id); } } catch (Exception e) { e.printStackTrace(); } finally { DBUtil.close(rs, state, conn); } return zhandian.getId(); } //通过中转站名字和line获取ZhanDian对象 public ZhanDian getZhanDianByName(String name,int line) { String sql ="select * from jiedian where name = '" + name + "' and line = '" + line + "' "; Connection conn = DBUtil.getConn(); Statement state = null; ResultSet rs = null; ZhanDian zhandian = null; try { state = conn.createStatement(); rs = state.executeQuery(sql); while (rs.next()) { String qidian = rs.getString("qidian"); String zhongdian = rs.getString("zhongdian"); zhandian = new ZhanDian(qidian,zhongdian); } } catch (Exception e) { e.printStackTrace(); } finally { DBUtil.close(rs, state, conn); } return zhandian; } } package entity; public class ZhanDian { private int id; private int line; private String name; private String qidian; private String zhongdian; public int getId() { return id; } public void setId(int id) { this.id = id; } public int getLine() { return line; } public void setLine(int line) { this.line = line; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getQidian() { return qidian; } public void setQidian(String qidian) { this.qidian = qidian; } public String getZhongdian() { return zhongdian; } public void setZhongdian(String zhongdian) { this.zhongdian = zhongdian; } public ZhanDian(int id,int line,String name) { this.id=id; this.line=line; this.name=name; } public ZhanDian(int id,String name) { this.id=id; this.name=name; } public ZhanDian(String qidian,String zhongdian) { this.qidian=qidian; this.zhongdian=zhongdian; } public ZhanDian(String name) { this.name=name; } public ZhanDian(int id) { this.id=id; } public ZhanDian() { } } package assist; import dao.SubwayDao; //import entity.ZhanDian; public class Transform { //判断输入的地点名在哪一个线路和对应线路上的对应表中对应id /* public ZhanDian transform(String name) { int line=0; int n; SubwayDao sd = new SubwayDao(); ZhanDian zd = new ZhanDian();//bean //将站点信息存储在数组中 String one[] = new String[29]; String three[] = new String[34]; //将数据库中one表中的数据存储到数组one中 int id=1; for(n=0;n<29;n++) { one[n]=sd.getCourseById_1(id).getName(); id++; } //将数据库中one表中的数据存储到数组one中 id=1; for(n=0;n<34;n++) { three[n]=sd.getCourseById_3(id).getName(); id++; } //判断是否在一号线 for(n=0;n<29;n++) { if(one[n].equals(name)) { line=1; id=n+1; } } //判断是否在三号线 for(n=0;n<34;n++) { if(three[n].equals(name)) { line=3; id=n+1; } } zd.setLine(line); zd.setId(id); return zd; } */ //通过起点所在线路的数组,起点和中转站的id和名字得到起点到中转站的站点名字数组 public String[] zhongZhanQ(String [] zhongzhan,int id1,int id,String name1,String name) { String shuchu[]=new String [zhongzhan.length]; int n=0; int i; //起点id1小于中转站id时 if((id1-id)<0) { for(i=(id1-1);i<id;i++) { shuchu[n]=zhongzhan[i]; n++; } } //起点id大于中转站id时 else { for(i=(id1-1);i>(id-2);i--) { shuchu[n]=zhongzhan[i]; n++; } } return shuchu; } //将两个数字和并成一个数字 public int HeBing(int line1,int line2) { String sline1=String.valueOf(line1);//将int型转化为String型 String sline2=String.valueOf(line2);//将int型转化为String型 String sline3; sline3=sline1+sline2; int line3= Integer.parseInt(sline3); //将String型转化为int型 return line3; } //通过终点所在线路的数组,终点和中转站的id和名字得到起点到中转站的站点名字数组 public String[] zhongZhanZ(String [] zhongzhan,int id,int id2,String name,String name2) { String shuchu[]=new String [zhongzhan.length]; int n=0; int i; //终点id小于中转站id时 if((id2-id)<0) { for(i=(id-2);i>(id2-2);i--) { shuchu[n]=zhongzhan[i]; n++; } } //终点id等于中转站id时 //else if((id2-id)==0){ //} //终点id大于中转站id时 else { for(i=id;i<id2;i++) { shuchu[n]=zhongzhan[i]; n++; } } return shuchu; } //通过表名将其数组形式输出 public String[] zu(String biao) { String zu[] = new String[40]; SubwayDao sd = new SubwayDao(); //将数据库中表中的数据存储到数组中 int id=1; int xunhuan=0; if(biao=="one") { xunhuan = 29; }else if(biao == "two"){ xunhuan = 37; }else if(biao=="three") { xunhuan = 34; }else if(biao=="four") { xunhuan = 18; }else if(biao=="five") { xunhuan = 21; }else if(biao=="six") { xunhuan = 18; } for(int n=0;n<xunhuan;n++) { zu[n]=sd.getCourseByBiaoId(biao,id).getName(); id++; } return zu; } /* //将表one以数组的形式输出 public String[] oneZu() { String onezu[] = new String[29]; SubwayDao sd = new SubwayDao(); //将数据库中one表中的数据存储到数组one中 int id=1; for(int n=0;n<29;n++) { onezu[n]=sd.getCourseById_1(id).getName(); id++; } return onezu; } //将表three以数组的形式输出 public String[] threeZu() { String threezu[] = new String[34]; SubwayDao sd = new SubwayDao(); //将数据库中one表中的数据存储到数组one中 int id=1; for(int n=0;n<34;n++) { threezu[n]=sd.getCourseById_3(id).getName(); id++; } return threezu; } */ //判断起点和终点是否在一条线上 public boolean tiaoShu(int qidian,int zhongdian) { if(qidian==zhongdian) { return true; }else { return false; } } //通过line获得对应的字符串 public String[] biaoByLine(int line) { String biao[]=new String[40]; switch(line) { case 1:{ biao=zu("one"); break; } case 2:{ biao=zu("two"); break; } case 3:{ biao=zu("three"); break; } case 4:{ biao=zu("four"); break; } case 5:{ biao=zu("five"); break; } case 6:{ biao=zu("six"); break; } } return biao; } //通过int型line获取string型的表名 public String stringByLine(int line) { String biaoname=null; switch(line) { case 1:{ biaoname = "one"; break; } case 2:{ biaoname = "two"; break; } case 3:{ biaoname = "three"; break; } case 4:{ biaoname = "four"; break; } case 5:{ biaoname = "five"; break; } case 6:{ biaoname = "six"; break; } } return biaoname; } } package main; import assist.Transform; //import assist.IntStringInt; import dao.SubwayDao; public class Main { /* public static void main(String []arge) { Transform tf = new Transform(); //IntStringInt isi = new IntStringInt(); SubwayDao sd = new SubwayDao(); Scanner input = new Scanner(System.in); int line1,line2; int id1,id2; String qibiao,zhongbiao; int qiid,zhongid; String qiname,zhongname; System.out.println("输入起始站"); //起始点名字 qiname=input.nextLine(); System.out.println("输入终点站"); //终点名字 zhongname=input.nextLine(); //得出起始站点所在线路和id line1=tf.transform(qiname).getLine(); id1=tf.transform(qiname).getId(); //得出终点所在线路和对应id line2=tf.transform(zhongname).getLine(); id2=tf.transform(zhongname).getId(); //如果两个点在一条线上 if(tf.tiaoShu(line1,line2)) { String zhengzu[] = tf.zhongZhanQ(tf.biaoByLine(line1), id1, id2, qiname, zhongname); System.out.println("起点至终点的站点信息如下:"); System.out.print("开始 ——> "); int sum=0; int mn; for(mn=0;mn<zhengzu.length&&zhengzu[mn]!=null;mn++) { System.out.print(zhengzu[mn] + " ——> "); } System.out.println("结束"); sum=mn; System.out.println("总共" + (sum-1) + "站"); } //否则在两条线上 else { //得出换站点的line及名字 int line=tf.HeBing(line1,line2); String name=sd.getCourseByLine(line).getName(); //通过名字获取所交的两个表名 qibiao=sd.getZhanDianByName(name,line).getQidian(); zhongbiao=sd.getZhanDianByName(name,line).getZhongdian(); //通过表名和名字获取对应表里的id值 qiid=sd.getIdByBNName(qibiao,name); zhongid=sd.getIdByBNName(zhongbiao,name); //输出前半段路程数组 String qizu[] = tf.zhongZhanQ(tf.biaoByLine(line1), id1, qiid, qiname, name); String zhongzu[] = tf.zhongZhanZ(tf.biaoByLine(line2), zhongid, id2, name, zhongname); //输出中途站点信息 System.out.println("起点至终点的站点信息如下:"); System.out.print("开始 ——> "); int sum=0; int m,n; for(m=0;m<qizu.length&&qizu[m]!=null;m++) { System.out.print(qizu[m] + " ——> "); } for(n=0;n<zhongzu.length&&zhongzu[n]!=null;n++) { System.out.print(zhongzu[n] + " ——> "); } sum=m+n; System.out.println("结束"); System.out.println("总共" + (sum-1) + "站"); } input.close(); } */ public String zhu(String qidian,String zhongdian,int qixian,int zhongxian) { String string="";//存储所要输出的字符串 Transform tf = new Transform(); //IntStringInt isi = new IntStringInt(); SubwayDao sd = new SubwayDao(); int id1,id2; String qibiao,zhongbiao; int qiid,zhongid; String qiname,zhongname; //起始点名字 qiname=qidian; //终点名字 zhongname=zhongdian; //得出起始站点id1 id1=sd.getIdByBNName((tf.stringByLine(qixian)), qidian); //得出终点对应id2 id2=sd.getIdByBNName((tf.stringByLine(zhongxian)), zhongdian); //如果两个点在一条线上 if(tf.tiaoShu(qixian,zhongxian)) { String zhengzu[] = tf.zhongZhanQ(tf.biaoByLine(qixian), id1, id2, qiname, zhongname); string = string +"起点至终点的站点信息:开始 ——> "; int mn; for(mn=0;mn<zhengzu.length&&zhengzu[mn]!=null;mn++) { string = string + mn + String.valueOf(zhengzu[mn]) + "——>"; } string = string + "结束"; string = string + "总共" + String.valueOf(mn-1) + "站"; } //否则在两条线上 else { //得出换站点的line及名字 int line=tf.HeBing(qixian,zhongxian); String name=sd.getCourseByLine(line).getName(); //通过名字获取所交的两个表名 qibiao=sd.getZhanDianByName(name,line).getQidian(); zhongbiao=sd.getZhanDianByName(name,line).getZhongdian(); //通过表名和名字获取对应表里的id值 qiid=sd.getIdByBNName(qibiao,name); zhongid=sd.getIdByBNName(zhongbiao,name); //输出前半段路程数组 String qizu[] = tf.zhongZhanQ(tf.biaoByLine(qixian), id1, qiid, qiname, name); String zhongzu[] = tf.zhongZhanZ(tf.biaoByLine(zhongxian), zhongid, id2, name, zhongname); //输出中途站点信息 string=string + "起点至终点的站点信息:开始 ——>"; int m=0; int n=0; for(m=0;m<qizu.length&&qizu[m]!=null;m++) { if(m==qizu.length) { string = string + m + String.valueOf(qizu[m]) + "(此站需要换到 " + zhongxian + " 号线)——>"; }else { string = string + m + String.valueOf(qizu[m]) + "——>"; } } for(n=0;n<zhongzu.length&&zhongzu[n]!=null;n++) { string = string + m + String.valueOf(zhongzu[n]) + "——>"; m++; } string ="总共经过 " + String.valueOf(m-1) + "站" + string + "结束"; } return string; } } package servlet; import java.io.IOException; 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 main.Main; @WebServlet("/Servlet") public class Servlet extends HttpServlet{ private static final long serialVersionUID = 1L; protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { req.setCharacterEncoding("utf-8"); String method = req.getParameter("method"); if ("lucheng".equals(method)) { lucheng(req, resp); } } //查找线路 private void lucheng(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException{ Main zhu = new Main(); req.setCharacterEncoding("utf-8"); String qidian = req.getParameter("qidian"); String zhongdian = req.getParameter("zhongdian"); int qixian = Integer.parseInt(req.getParameter("qixian")); int zhongxian = Integer.parseInt(req.getParameter("zhongxian")); String string=zhu.zhu(qidian, zhongdian,qixian,zhongxian); req.setAttribute("message", string); req.getRequestDispatcher("lucheng.jsp").forward(req,resp); } }
<%@ 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; } </style> </head> <body> <div align="center" style="100%" style = "heigh:20%"> <h1 style="color: red">石家庄地铁</h1> <form action="Servlet?method=lucheng" method="post" onsubmit="return check()"> <div class="a"> 起点站所在线:<input type="text" id="qixian" name="qixian"/> </div> <div class="a"> 起点站的名字:<input type="text" id="qidian" name="qidian"/> </div> <div class="a"> 终点站所在线:<input type="text" id="zhongxian" name="zhongxian"/> </div> <div class="a"> 终点站的名字:<input type="text" id="zhongdian" name="zhongdian"/> </div> <div class="a"> <button type="submit" class="b">确 定</button> </div> </form> </div> <div align="center" style="100%" style="heigh:500px"> <img src="image/tu.jpg" style="100%" style="hight:500px"> </div> <script type="text/javascript"> function check() { var qidian = document.getElementById("qidian"); var zhongdian = document.getElementById("zhongdian"); var qixian = document.getElementById("qixian"); var zhongxian = document.getElementById("zhongxian"); if(qixian.value==''){ alert('请填写起点站所在线'); return false; }else if(qidian.value=='') { alert('请填写起点站'); return false; }else if(zhongxian.value==''){ alert('请填写终点站所在线'); return false; }else if(zhongdian.value==''){ alert('请填写终点站'); return false; }else if((qixian.value!=1||2||3||4||5||6)&&(zhongxian.value!=1||2||3||4||5||6)){ alert('第一项和第三项只可填写 :1,2,3,4,5,6 中的一个数字); return false; } } </script> </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> .luxian{ 100%; heigh:30% } </style> </head> <body> <div class="luxian"> <h1 style="color:red"><%=request.getAttribute("message")%></h1> </div> </body> </html>
运行截图如下:
编程总结分析:在此次实验中遇到最多的问题就是数据查不到,其中有很多错误的地方,例如:因为我是将数据存储在数组中的,所以我在存储时遇到了无法找到对应表的问题,无法确定表中有多少数据的问题,等诸多小问题。基本所遇到的问题都是通过在指定位置输出指定数据来找到具体的问题位置所在,及所出现的问题是什么原因(例如:哪个数据获取错误)造成的。
这个项目我预计一天可以做好后台,半天了解网页技术,半天设计网页,再有半天实现网页和后台的连接及网页美化。最后需要一天的时间做好后台优化及整个项目的整体优化。就是说要需要三天半的时间。