Web项目专项训练——火车车次信息管理系统代码分享

题目:火车车次信息管理

一、语言和环境

 

A、实现语言

 

   Java

 

B、环境要求

 

  JDK1.7、Eclipse、Tomcat7.*、mysql

二、功能要求

 

使用JSP+Servlet实现火车车次信息管理功能,mysql作为后台数据库,功能包括车次信息浏览、火车车次信息详情和车次信息删除两大功能。具体要求如下:

(1)打开火车车次信息管理首页,以列表方式显示所有车次信息,页面列表要求实现隔行变色效果,如图-1所示。

Web项目专项训练——火车车次信息管理系统代码分享_第1张图片

 

图-1

(2)单击“删除”链接可以进行火车车次信息删除,如果没有复选框被选中,当单击“删除”链接时需要提示“请选择车次!”信息,如图-2所示。

Web项目专项训练——火车车次信息管理系统代码分享_第2张图片

 

图-2

(3)如果选中复选框,单击“删除”链接,执行删除车次信息操作,并根据删除结果给出相应的信息提示,如图-3所示。

Web项目专项训练——火车车次信息管理系统代码分享_第3张图片

 

图-3

点击确定后,跳转至车次信息列表页面,并显示更新后的车次列表信息,如图-4所示。

Web项目专项训练——火车车次信息管理系统代码分享_第4张图片

图-4

 

(4)单击车次超链接,则跳转至指定车次的火车车次详情页面,如图-5所示。

Web项目专项训练——火车车次信息管理系统代码分享_第5张图片

 

图-5

 三、  

数据库设计

 

数据库表名称及要求:

表名

train_info

中文表名称

火车车次信息表

主键

train_no

序号

字段名称

字段说明

类型

长度

属性

备注

1

train_no

车次编号

varchar

50

主键

 

2

start_station

始发站

varchar

50

非空

 

3

arrival_station

终点站

varchar

50

非空

 

4

start_time

出发时间

varchar

50

非空

 

5

arrival_time

到达时间

varchar

50

非空

 

6

type

车次类型

varchar

50

非空

 

7

runtime

运行时间

varchar

50

非空

 

8

mile

里程

double(18, 1)

 

非空

 

Web项目专项训练——火车车次信息管理系统代码分享_第6张图片

创建实体类

package com.qf.entity;

public class TrainInfo {
	private String train_no;
	private String start_station;
	private String arrival_station;
	private String start_time;
	private String arrival_time;
	private String type;
	private String runtime;
	private double mile;
	public String getTrain_no() {
		return train_no;
	}
	public void setTrain_no(String train_no) {
		this.train_no = train_no;
	}
	public String getStart_station() {
		return start_station;
	}
	public void setStart_station(String start_station) {
		this.start_station = start_station;
	}
	public String getArrival_station() {
		return arrival_station;
	}
	public void setArrival_station(String arrival_station) {
		this.arrival_station = arrival_station;
	}
	public String getStart_time() {
		return start_time;
	}
	public void setStart_time(String start_time) {
		this.start_time = start_time;
	}
	public String getArrival_time() {
		return arrival_time;
	}
	public void setArrival_time(String arrival_time) {
		this.arrival_time = arrival_time;
	}
	public String getType() {
		return type;
	}
	public void setType(String type) {
		this.type = type;
	}
	public String getRuntime() {
		return runtime;
	}
	public void setRuntime(String runtime) {
		this.runtime = runtime;
	}
	public double getMile() {
		return mile;
	}
	public void setMile(double mile) {
		this.mile = mile;
	}
	
}

创建jdbc类对数据库的表进行操作

package com.qf.tools;


import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class DBUtils {
	
	public static Connection conn=null;
	public static PreparedStatement state=null;
	public static ResultSet rs=null;
	
	public static Connection getConnection() throws ClassNotFoundException, SQLException {
		Class.forName("com.mysql.jdbc.Driver");
		conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/lianxi", "root", "147258");
		return conn;
	}
	
	public static int update(String sql,Object...obs) {
		int result=0;
		try {
			DBUtils.getConnection();
			state=conn.prepareStatement(sql);
			for (int i = 0; i < obs.length; i++) {
				state.setObject(i+1, obs[i]);
			}
			result=state.executeUpdate();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}
	
	public static ResultSet query(String sql,Object...obs) {
		try {
			DBUtils.getConnection();
			state=conn.prepareStatement(sql);
			for (int i = 0; i < obs.length; i++) {
				state.setObject(i+1, obs[i]);
			}
			rs=state.executeQuery();
		}catch (Exception e) {
			e.printStackTrace();
		}
		return rs;
	}
	
	public static void closeObject() {
		try {
			if(rs!=null) {
				rs.close();
			}
			if(state!=null) {
				state.close();
			}
			if(conn!=null) {
				conn.close();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
}

创建DAO类对数据库的表进行增删改查操作

package com.qf.dao;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.qf.entity.TrainInfo;
import com.qf.tools.DBUtils;

public class TrainInfoDAO {

	public List queryAllTrainInfo() {
		List trains=new ArrayList();
		try {
			String sql="select train_no,start_station,arrival_station,start_time,arrival_time,type,runtime,mile from train_info order by start_time";
			DBUtils.rs=DBUtils.query(sql);
			while(DBUtils.rs.next()) {
				TrainInfo t=new TrainInfo();
				t.setTrain_no(DBUtils.rs.getString("train_no"));
				t.setStart_station(DBUtils.rs.getString("start_station"));
				t.setArrival_station(DBUtils.rs.getString("arrival_station"));
				t.setStart_time(DBUtils.rs.getString("start_time"));
				t.setArrival_time(DBUtils.rs.getString("arrival_time"));
				t.setType(DBUtils.rs.getString("type"));
				t.setRuntime(DBUtils.rs.getString("runtime"));
				t.setMile(DBUtils.rs.getDouble("mile"));
				trains.add(t);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			DBUtils.closeObject();
		}
		return trains;
	}

	public void deleteTrainInfo(String[] trains) {
		String sql="delete from train_info where train_no=?";
		for (int i = 0; i < trains.length; i++) {
			DBUtils.update(sql, trains[i]);
		}
		DBUtils.closeObject();
	}

	public TrainInfo queryTrainInfo(String train_no) {
		TrainInfo t=null;
		try {
			String sql="select train_no,start_station,arrival_station,start_time,arrival_time,type,runtime,mile from train_info where train_no=?";
			DBUtils.rs=DBUtils.query(sql,train_no);
			while(DBUtils.rs.next()) {
				t=new TrainInfo();
				t.setTrain_no(DBUtils.rs.getString("train_no"));
				t.setStart_station(DBUtils.rs.getString("start_station"));
				t.setArrival_station(DBUtils.rs.getString("arrival_station"));
				t.setStart_time(DBUtils.rs.getString("start_time"));
				t.setArrival_time(DBUtils.rs.getString("arrival_time"));
				t.setType(DBUtils.rs.getString("type"));
				t.setRuntime(DBUtils.rs.getString("runtime"));
				t.setMile(DBUtils.rs.getDouble("mile"));
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			DBUtils.closeObject();
		}
		return t;
	}

}

在web.xml中配置默认起始页



  NZjavaee1901Train
  
    QueryTrainInfo
  

查询数据库中所有火车信息的servlet代码

package com.qf.servlet;

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 com.qf.dao.TrainInfoDAO;
import com.qf.entity.TrainInfo;

/**
 * Servlet implementation class QueryTrainInfo
 */
@WebServlet("/QueryTrainInfo")
public class QueryTrainInfo extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset:utf-8");
		
		TrainInfoDAO trainInfoDao=new TrainInfoDAO();
		List trains=trainInfoDao.queryAllTrainInfo();
		request.setAttribute("trains", trains);
		request.getRequestDispatcher("homePage.jsp").forward(request, response);
	}

}

前端首页的jsp代码

<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>




Insert title here




	
删除 车次 发车--到达 发时--到时 车型 运行时间
${t.train_no} ${t.start_station}--${t.arrival_station} ${t.start_time}--${t.arrival_time} ${t.type} ${t.runtime}

删除火车信息的servlet代码

package com.qf.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 com.qf.dao.TrainInfoDAO;

/**
 * Servlet implementation class DeleteTrainServlet
 */
@WebServlet("/DeleteTrainServlet")
public class DeleteTrainServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");
		
		String trains1=request.getParameter("delete");
		String[] trains=trains1.split(",");
		TrainInfoDAO trainInfoDao=new TrainInfoDAO();
		trainInfoDao.deleteTrainInfo(trains);
		response.sendRedirect("QueryTrainInfo");
	}

}

通过火车车次编号查询火车车次详细信息servlet代码

package com.qf.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 com.qf.dao.TrainInfoDAO;
import com.qf.entity.TrainInfo;

/**
 * Servlet implementation class QueryTrainByNoServlet
 */
@WebServlet("/QueryTrainByNoServlet")
public class QueryTrainByNoServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset:utf-8");
		String train_no=request.getParameter("number");
		TrainInfoDAO trainInfoDao=new TrainInfoDAO();
		TrainInfo train=trainInfoDao.queryTrainInfo(train_no);
		request.setAttribute("train", train);
		request.getRequestDispatcher("trainInfo.jsp").forward(request, response);
	}

}

单击车次超链接跳转至指定车次的火车车次详情页面前端代码

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>




Insert title here



	
火车车次详情
车次 ${requestScope.train.train_no}
发车--到达 ${requestScope.train.start_station}--${requestScope.train.arrival_station}
发时--到时 ${requestScope.train.start_time}--${requestScope.train.arrival_time}
车型 ${requestScope.train.type}
运行时间(小时) ${requestScope.train.runtime}
里程(公里) ${requestScope.train.mile}
返回

你可能感兴趣的:(Web项目专项训练——火车车次信息管理系统代码分享)