1、首先要有一个JavaBean
package com.tnr.bean;
public class Car {
private int c_id;
private String c_name;
private int c_price;
public int getC_id() {
return c_id;
}
public void setC_id(int c_id) {
this.c_id = c_id;
}
public String getC_name() {
return c_name;
}
public void setC_name(String c_name) {
this.c_name = c_name;
}
public int getC_price() {
return c_price;
}
public void setC_price(int c_price) {
this.c_price = c_price;
}
}
2、需要一个Servlet(主要是用来获取参数以及页面跳转的方法)
package com.tnr.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.tnr.bean.Car;
import com.tnr.service.CarService;
@WebServlet({
"/carList",
"/carDoAdd",
"/carToAdd",
"/carToUpd",
"/carDoUpd",
"/carDoDel"
})
public class CarServlet extends HttpServlet{
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
req.setCharacterEncoding("UTF-8");
String path=req.getServletPath();
CarService service=new CarService();
if("/carList".equals(path)) {
List<Car> list=service.getCarList();
req.setAttribute("carList",list);
req.getRequestDispatcher("carList.jsp").forward(req, resp);
}else if("/carToAdd".equals(path)) {
req.getRequestDispatcher("carAdd.jsp").forward(req, resp);
}else if("/carDoAdd".equals(path)) {
Car car=new Car();
car.setC_id(Integer.parseInt(req.getParameter("c_id")));
car.setC_name(req.getParameter("c_name"));
car.setC_price(Integer.parseInt(req.getParameter("c_price")));
service.addNewCar(car);
resp.sendRedirect("carList");
}else if("/carToUpd".equals(path)) {
Car car=service.getCarById(req.getParameter("c_id"));
req.setAttribute("car",car);
req.getRequestDispatcher("carUpd.jsp").forward(req, resp);
}else if("/carDoUpd".equals(path)) {
Car car=new Car();
car.setC_id(Integer.parseInt(req.getParameter("c_id")));
car.setC_name(req.getParameter("c_name"));
car.setC_price(Integer.parseInt(req.getParameter("c_price")));
service.updCar(car);
resp.sendRedirect("carList");
}else if("/carDoDel".equals(path)) {
String c_id=req.getParameter("c_id");
service.delCar(c_id);
resp.sendRedirect("carList");
}
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
this.doGet(req, resp);
}
}
3、数据库的连接DBUtils
package com.tnr.utils;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class DBUtils {
private static final String driver="com.mysql.jdbc.Driver";
private static final String url="jdbc:mysql://localhost:3306/cars";
private static final String user="root";
private static final String pwd="root";
static {
try {
Class.forName(driver);
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
Connection con=null;
PreparedStatement pstm=null;
ResultSet rs=null;
//链接数据库
public static Connection getConnection() throws SQLException {
return DriverManager.getConnection(url, user, pwd);
}
//关闭数据库
public static void close(Connection con,PreparedStatement pstm,ResultSet rs) {
try {
if(rs!=null) {
rs.close();
}
} catch (SQLException e) {
throw new RuntimeException(e);
}
try {
if(pstm!=null) {
pstm.close();
}
} catch (SQLException e) {
throw new RuntimeException(e);
}
try {
if(con!=null && !con.isClosed()) {
con.close();
}
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
}
4、需要一个简单的跳转页面
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<a href="carList">跳转</a>
</body>
</html>
5、需要一个主界面(主要就是对象的集合以及显示对象的列表)
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8" import="java.util.*,com.tnr.bean.*"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<%List<Car> list=(List<Car>)request.getAttribute("carList"); %>
<a href="carToAdd">新增</a>
<table>
<tr>
<td>车辆的编号</td>
<td>车辆的名称</td>
<td>车辆的价格</td>
<td>操作</td>
</tr>
<%
for(Car cars:list){
%>
<tr>
<td><%=cars.getC_id() %></td>
<td><%=cars.getC_name() %></td>
<td><%=cars.getC_price() %></td>
<td>
<a href="carToUpd?c_id=<%=cars.getC_id()%>">修改</a>
<a href="carDoDel?c_id=<%=cars.getC_id()%>">删除</a>
</td>
</tr>
<%
}
%>
</table>
</body>
</html>
6、接下来就是简单增改的操作
//增加的页面
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<form action="carDoAdd" method="post">
<div>
车辆的编号:
<input type="number" name="c_id"/>
</div>
<div>
车辆的名称:
<input type="text" name="c_name"/>
</div>
<div>
车辆的价格:
<input type="number" name="c_price"/>
</div>
<div>
<input type="submit" value="提交"/>
</div>
</form>
</body>
</html>
//修改的页面
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8" import="com.tnr.bean.*"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<%
Car car=(Car)request.getAttribute("car");
%>
<form action="carDoUpd" method="post">
<div>
车辆的编号:
<input type="number" readonly="readonly" name="c_id" value="<%=car.getC_id()%>"/>
</div>
<div>
车辆的名称:
<input type="text" value="<%=car.getC_name() %>" name="c_name"/>
</div>
<div>
车辆的价格:
<input type="number" value="<%=car.getC_price() %>" name="c_price"/>
</div>
<div>
<input type="submit" value="提交"/>
</div>
</form>
</body>
</html>