石家庄地铁售票系统源代码
石家庄地铁售票系统
源代码:
前端:
主界面index.jsp
<%@ 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> <style type="text/css"> .form{ width: 260px; height :350px; border:5px solid #808080;/*solid细线*/ padding: 10px 20px 30px 40px; margin: 0px auto; background:url("lantian.jpg") ; background-size: 100% 100%; float: left; } .logo{ width:100%; height :100px; background:url("SJZtube.jpg"); background-size: 100% 100%; float: left; } </style> </head> <body> <div class="logo"></div> <div class="form"> <form action ="Tubeservlet?method=linecheck" method="post" onsubmit="return check()" > <br>请选择线路: <select name="originline" id="a"> <option value=0>起点</option> <option value=1>1</option> <option value=2>2</option> <option value=3>3</option> <option value=4>4</option> <option value=5>5</option> <option value=6>6</option> </select> <select name="endline" id="b"> <option value=0>终点</option> <option value=1>1</option> <option value=2>2</option> <option value=3>3</option> <option value=4>4</option> <option value=5>5</option> <option value=6>6</option> </select><br> 起点:<input name="origin" id="origin" type="text" value="西王" size="20" maxlength="6"/><br> 终点:<input name="end" id="end" type="text" value="军医医院" size="20" maxlength="6"/><br> <p> <input type="submit" value="确认"> <input type="reset" value="重填"/></p> </form> </div> <script type="text/javascript"> function check() { var a = document.getElementById("a"); var b= document.getElementById("b"); //非空 if(a.value == '0') { alert('请选择起点线路'); a.focus(); return false; } if(b.value == '0') { alert('请选择终点线路'); b.focus(); return false; } } </script> <div > <img style="width: 600px;height :400px;float: left;" src="tubemap.jpg" id="map"/> </div> <script type="text/javascript"> var myimage = document.getElementById("map"); if (myimage.addEventListener) { //addEventListener适用于版本较新的IE浏览器 ,如IE9以及火狐浏览器, myimage.addEventListener("mousewheel", MouseWheelHandler, false); // Firefox浏览器使用的滚轮事件是 DOMMouseScroll myimage.addEventListener("DOMMouseScroll", MouseWheelHandler, false); } //attachEvent适用于旧的 IE浏览器,IE 6/7/8 else myimage.attachEvent("onmousewheel", MouseWheelHandler); function MouseWheelHandler(e) { var e = window.event; var delta = Math.max(-1, Math.min(1, (e.wheelDelta))); myimage.style.width = Math.max(600, myimage.width + (30 * delta)) + "px"; myimage.style.height = Math.max(400, myimage.height + (30 * delta)) + "px"; return false; } </script> </body> </html>
需要换乘transfer.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 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> .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"> <h1 style="color: red;">需换乘</h1> <a href="index.jsp">返回主页</a> <table class="tb"> <tr> <td>名称</td> </tr> <c:forEach items="${tubestations1}" var="item1"> <tr> <td>${item1.name}</td> </tr> </c:forEach> <tr> <td style="color: red;">换乘</td> </tr> <c:forEach items="${tubestations2}" var="item2"> <tr> <td>${item2.name}</td> </tr> </c:forEach> </table> </div> </body> </html>
不需要换乘notransfer.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 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> .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"> <h1 style="color: red;">无需换乘</h1> <a href="index.jsp">返回主页</a> <table class="tb"> <tr> <td>名称</td> </tr> <c:forEach items="${tubestations}" var="item"> <tr> <td>${item.name}</td> </tr> </c:forEach> </table> </div> </body> </html>
后台:
import java.io.IOException; 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 entity.Tubestation; import service.Tubeservice; /** * Servlet implementation class Tubeservlet */ @WebServlet("/Tubeservlet") public class Tubeservlet extends HttpServlet { private static final long serialVersionUID = 1L; Tubeservice service=new Tubeservice(); protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { req.setCharacterEncoding("utf-8"); String method = req.getParameter("method"); if(method.equals("linecheck")){ linecheck(req,resp); }else if(method.equals("showNotransferASC")){ showNotransferASC(req,resp); }else if(method.equals("showNotransferDESC")){ showNotransferDESC(req,resp); }else if(method.equals("compareId")){ compareId(req,resp); }else if(method.equals("showTransfer")){ showTransfer(req,resp); } } private void linecheck(HttpServletRequest req, HttpServletResponse resp)throws ServletException, IOException{ /** * 检查是否需要换乘 */ req.setCharacterEncoding("utf-8"); String originline = req.getParameter("originline");//从jsp上得到名为originline的value值,这里是起点线路 String endline = req.getParameter("endline");//从jsp上得到名为endline的value值 int originlineint=Integer.parseInt(originline);//字符串转化为整数 int endlineint=Integer.parseInt(endline); if(originlineint==endlineint) { //无需换乘 req.getRequestDispatcher("Tubeservlet?method=compareId").forward(req,resp); }else{ //需要换乘 req.getRequestDispatcher("Tubeservlet?method=showTransfer").forward(req,resp); } } private void compareId(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException{ //比较站点id String end = req.getParameter("end"); String origin = req.getParameter("origin"); String originline = req.getParameter("originline"); String endline = req.getParameter("endline"); req.setCharacterEncoding("utf-8"); int k=service.compareId(origin,end,originline,endline); if(k==1) { req.getRequestDispatcher("Tubeservlet?method=showNotransferASC").forward(req,resp); }else if(k==2){ req.getRequestDispatcher("Tubeservlet?method=showNotransferDESC").forward(req,resp); }else if(k==0){ req.setAttribute("message", "失败"); req.getRequestDispatcher("index.jsp").forward(req,resp); } } private void showNotransferASC(HttpServletRequest req, HttpServletResponse resp)throws ServletException, IOException { // 升序列出数据 String originline = req.getParameter("originline"); String endline = req.getParameter("endline"); String end = req.getParameter("end"); String origin = req.getParameter("origin"); req.setCharacterEncoding("utf-8"); List<Tubestation> tubestations = service.showNotransferASC(origin,end,originline,endline); req.setAttribute("tubestations", tubestations); req.getRequestDispatcher("notransfer.jsp").forward(req,resp); } private void showNotransferDESC(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException{ // 降序列出数据 String end = req.getParameter("end"); String origin = req.getParameter("origin"); String originline = req.getParameter("originline"); String endline = req.getParameter("endline"); req.setCharacterEncoding("utf-8"); List<Tubestation> tubestations = service.showNotransferDESC(origin,end,originline,endline); req.setAttribute("tubestations", tubestations); req.getRequestDispatcher("notransfer.jsp").forward(req,resp); } private void showTransfer(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException{ /* * 列出换乘信息 * 寻找换乘点 */ String end = req.getParameter("end"); String origin = req.getParameter("origin"); String originline = req.getParameter("originline"); String endline = req.getParameter("endline"); req.setCharacterEncoding("utf-8"); String transfername=service.searchTransfer(originline,endline); int k=service.compareId(origin, transfername, originline, originline); if(k==1) {//列出第一段,起点-换乘站 List<Tubestation> tubestations1 = service.showNotransferASC(origin,transfername,originline,originline); req.setAttribute("tubestations1", tubestations1); int a=service.compareId(transfername, end,endline,endline); if(a==1) {//列出第二段,换乘站-终点 List<Tubestation> tubestations2 = service.showNotransferASC(transfername, end,endline,endline); req.setAttribute("tubestations2", tubestations2); req.getRequestDispatcher("transfer.jsp").forward(req,resp);//最后跳转到界面,结束程序 }else if(a==2){ List<Tubestation> tubestations2 = service.showNotransferDESC(transfername, end,endline,endline); req.setAttribute("tubestations2", tubestations2); req.getRequestDispatcher("transfer.jsp").forward(req,resp); } }else if(k==2){ List<Tubestation> tubestations1 = service.showNotransferDESC(origin,transfername,originline,originline); req.setAttribute("tubestations1", tubestations1); int a=service.compareId( transfername, end,endline,endline); if(a==1) {//列出第二段,换乘站-终点 List<Tubestation> tubestations2 = service.showNotransferASC(transfername, end,endline,endline); req.setAttribute("tubestations2", tubestations2); req.getRequestDispatcher("transfer.jsp").forward(req,resp);//最后跳转到界面,结束程序 }else if(a==2){ List<Tubestation> tubestations2 = service.showNotransferDESC(transfername, end,endline,endline); req.setAttribute("tubestations2", tubestations2); req.getRequestDispatcher("transfer.jsp").forward(req,resp); } }else if(k==0){ req.setAttribute("message", "失败"); req.getRequestDispatcher("index.jsp").forward(req,resp); } } }
import java.util.List; import dao.Tubedao; import entity.Tubestation; public class Tubeservice { Tubedao dao=new Tubedao(); public List<Tubestation> showNotransferASC(String origin, String end, String originline, String endline) { return dao.showNotransferASC(origin,end,originline,endline); } public int compareId(String origin, String end, String originline, String endline) { int f=0; f=dao.compareId(origin,end,originline,endline); return f; } public List<Tubestation> showNotransferDESC(String origin, String end, String originline, String endline) { return dao.showNotransferDESC(origin,end,originline,endline); } public String searchTransfer(String originline, String endline) { return dao.searchTransfer(originline,endline); } }
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 DButil.Shujuku; import entity.Tubestation; public class Tubedao { public List<Tubestation> showNotransferASC(String origin,String end, String originline, String endline) { /* * 升序列出数据 * 起点id小于终点 */ String sql = "select name from "+selectline(originline)+" " + "where id between (select id from "+selectline(originline)+" where name = '" + origin + "') " + "and (select id from "+selectline(originline)+" where name = '" + end + "')" + " order by id ASC"; Connection conn = Shujuku.conn(); Statement state = null; ResultSet rs = null; List<Tubestation> list = new ArrayList<>(); try { state = conn.createStatement(); rs = state.executeQuery(sql); Tubestation bean = null; while (rs.next()) { String name = rs.getString("name"); bean= new Tubestation(name); list.add(bean); } } catch (SQLException e) { e.printStackTrace(); } finally { Shujuku.close(rs, state, conn); } return list; } public List<Tubestation> showNotransferDESC(String origin, String end, String originline, String endline) { /* * 降序列出数据 * 起点id大于终点 */ String sql="select name from "+selectline(originline)+" " + "where id between (select id from "+selectline(originline)+" where name = '" + end + "') " + "and (select id from "+selectline(originline)+" where name = '" + origin + "') " + "order by id DESC"; Connection conn = Shujuku.conn(); Statement state = null; ResultSet rs = null; List<Tubestation> list = new ArrayList<>(); try { state = conn.createStatement(); rs = state.executeQuery(sql); Tubestation bean = null; while (rs.next()) { String name = rs.getString("name"); bean= new Tubestation(name); list.add(bean); } } catch (SQLException e) { e.printStackTrace(); } finally { Shujuku.close(rs, state, conn); } return list; } public int compareId(String origin, String end, String originline, String endline) { /** * 通过站名比较id大小 * 决定降序或升序输出 */ int a=getIdbyname(origin,originline); int b=getIdbyname(end,endline); int d=0; if(a>b) { d=2; }else { d=1; } return d; } public int getIdbyname(String name,String line) { //通过站名得到id String sql = "select id from "+selectline(line)+" where name ='" + name + "'"; Connection conn = Shujuku.conn(); Statement state = null; ResultSet rs = null; int id=0; try { state = conn.createStatement(); rs = state.executeQuery(sql); while (rs.next()) { id = rs.getInt("id"); } } catch (SQLException e) { e.printStackTrace(); } finally { Shujuku.close(rs, state, conn); } return id; } public String selectline(String line) { /* * 从网页上得到线路,确定连接哪张表 * 说明:一条线路一张表 */ int lineint=Integer.parseInt(line); String sqlline=null; if(lineint==1){ sqlline="tubestation1"; }else if(lineint==2) { sqlline="tubestation2"; }else if(lineint==3) { sqlline="tubestation3"; }else if(lineint==4) { sqlline="tubestation4"; }else if(lineint==5) { sqlline="tubestation5"; }else if(lineint==6) { sqlline="tubestation6"; } return sqlline; } public String searchTransfer(String originline, String endline) { /* * 根据line寻找换乘点 */ int oline=Integer.parseInt(originline); int dline=Integer.parseInt(endline); int a=0; int b=0; if(oline>dline) { a=dline; b=oline; }else { a=oline; b=dline; } int c=a*10+b; String sql="select * from "+selectline(originline)+" where line='"+c+"'"; Connection conn = Shujuku.conn(); Statement state = null; ResultSet rs = null; String name=null; try { state = conn.createStatement(); rs = state.executeQuery(sql); while (rs.next()) { name = rs.getString("name"); } } catch (SQLException e) { e.printStackTrace(); } finally { Shujuku.close(rs, state, conn); } return name; } }
import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; public class Shujuku { public static Connection conn(){ String url="jdbc:sqlserver://localhost:1433;DatabaseName=SJZtube";//填写你的数据库名 String userName="sa";//填写你的用户名,我的是sa String userPwd="tzk19991029";//填写你的密码tzk19991029 Connection connection=null; try{ Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver"); System.out.println("加载驱动成功!"); }catch(Exception e){ e.printStackTrace(); System.out.println("加载驱动失败!"); } try{ connection=DriverManager.getConnection(url,userName,userPwd); System.out.println("连接数据库成功!"); }catch(Exception e){ e.printStackTrace(); System.out.print("SQL Server连接失败!"); } return connection; } 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 class Tubestation { private String name; private String line; private int id; public void setName(String name) { this.name=name; } public String getName() { return name; } public void setLine(String line) { this.line=line; } public String getLine() { return line; } public void setId(int id) { this.id=id; } public int getId() { return id; } public Tubestation() { } public Tubestation(String name) { this.name=name; } public Tubestation(int id, String name) { this.name=name; this.id=id; } }
实验截图:
主页面:(地图可放大)
一条线路,无需换乘:
需要换乘: