题目:火车车次信息管理 |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
一、语言和环境 |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
A、实现语言 |
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Java |
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
B、环境要求 |
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
JDK1.7、Eclipse、Tomcat7.*、mysql |
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
二、功能要求 |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
使用JSP+Servlet实现火车车次信息管理功能,mysql作为后台数据库,功能包括车次信息浏览、火车车次信息详情和车次信息删除两大功能。具体要求如下: (1)打开火车车次信息管理首页,以列表方式显示所有车次信息,页面列表要求实现隔行变色效果,如图-1所示。
图-1 (2)单击“删除”链接可以进行火车车次信息删除,如果没有复选框被选中,当单击“删除”链接时需要提示“请选择车次!”信息,如图-2所示。
图-2 (3)如果选中复选框,单击“删除”链接,执行删除车次信息操作,并根据删除结果给出相应的信息提示,如图-3所示。
图-3 点击确定后,跳转至车次信息列表页面,并显示更新后的车次列表信息,如图-4所示。 图-4
(4)单击车次超链接,则跳转至指定车次的火车车次详情页面,如图-5所示。
图-5 |
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
三、 |
数据库设计 |
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
数据库表名称及要求: |
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
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;
}
}
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();
}
}
}
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;
}
}
NZjavaee1901Train
QueryTrainInfo
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);
}
}
<%@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
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");
}
}
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}
返回