//实体类
package entry;
import java.util.Date;
public class Animal {
private int aid;
private String aname;
private Date atime;
getXXX setXXX
}
//DAO
package DAO;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import util.JDBCUtil;
import entry.Animal;
/**
* 对数据库进行操作
* @author dell-
*
*/
public class AnimalDAO {
//添加动物信息
public void addAnimal(Animal animal){
//1建立连接
Connection conn= JDBCUtil.getConnection();
//2创建sql语句
String sql = "insert into animal (aid,aname,atime)values(?,?,?)";
//3创建sql执行对象
PreparedStatement ps =null;
try {
ps=conn.prepareStatement(sql);
ps.setInt(1, animal.getAid());
ps.setString(2, animal.getAname());
ps.setDate(3, new java.sql.Date(animal.getAtime().getTime()));
ps.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
}finally{
JDBCUtil.release(null,ps,conn);
}
}
//查询所有信息
public List<Animal> getAll(){
List<Animal> list = new ArrayList<Animal>();
//1连接数据库
Connection conn = JDBCUtil.getConnection();
//2拼装sql
String sql="select * from animal";
//3创建sql执行对象
PreparedStatement ps =null;
ResultSet rs = null;
try {
ps = conn.prepareStatement(sql);
rs=ps.executeQuery();
while(rs.next()){
Animal animal = new Animal();
animal.setAid(rs.getInt("aid"));
animal.setAname(rs.getString("aname"));
animal.setAtime(rs.getDate("atime"));
list.add(animal);
}
} catch (SQLException e) {
e.printStackTrace();
}finally{
JDBCUtil.release(rs, ps, conn);
}
return list;
}
//通过aid 删除动物信息
public void deleteAnimal(int aid){
//1建立数据库连接
Connection conn = JDBCUtil.getConnection();
//2拼装sql
String sql = "delete from animal where aid=?";
//3创建sql执行对象
PreparedStatement ps =null;
try {
ps = conn.prepareStatement(sql);
ps.setInt(1, aid);
ps.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
}finally{
JDBCUtil.release(null, ps, conn);
}
}
//通过aid修改动物信息
public void updateAnimal(Animal animal){
//1建立连接
Connection conn = JDBCUtil.getConnection();
//2拼装sql
String sql = "update animal set aname=?,atime=? where aid=?";
//3创建sql执行对象
PreparedStatement ps = null;
try {
ps = conn.prepareStatement(sql);
ps.setString(1, animal.getAname());
ps.setDate(2, new java.sql.Date(animal.getAtime().getTime()));
ps.setInt(3, animal.getAid());
ps.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
}finally{
JDBCUtil.release(null, ps, conn);
}
}
public Animal getAnimalByid(int aid){
//1链接数据库
Connection conn= JDBCUtil.getConnection();
//2创建sql语句
String sql = "select * from animal where aid=?";
//3创建sql执行对象
PreparedStatement ps = null;
ResultSet rs = null;
try {
ps = conn.prepareStatement(sql);
ps.setInt(1, aid);
rs = ps.executeQuery();
if(rs.next()){
Animal animal = new Animal();
animal.setAid(rs.getInt("aid"));
animal.setAname(rs.getString("aname"));
animal.setAtime(rs.getDate("atime"));
return animal;
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
JDBCUtil.release(rs, ps, conn);
}
return null;
}
}
//controller
package controller;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import DAO.AnimalDAO;
import entry.Animal;
public class AnimalSrevlet extends HttpServlet {
/**
* The doGet method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to get.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doPost(request, response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
response.setContentType("text/html;charset=utf-8");
String type = request.getParameter("type");
if(type==null||"".equals(type.trim())||"list".equals(type.trim())){
//执行获取所有小动物的操作
AnimalDAO animaldao = new AnimalDAO();
List<Animal> list = animaldao.getAll();
request.setAttribute("list", list);
request.getRequestDispatcher("/animal/list.jsp").forward(request, response);
}else if("add".equals(type.trim())) {
//执行添加动物操作
String aid = request.getParameter("id");
String aname = request.getParameter("name");
String atime = request.getParameter("times");
int id = Integer.parseInt(aid);
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Date time=null;
try {
time=sdf.parse(atime);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Animal animal = new Animal();
animal.setAid(id);
animal.setAname(aname);
animal.setAtime(time);
AnimalDAO animaldao = new AnimalDAO();
animaldao.addAnimal(animal);
response.sendRedirect("AnimalSrevlet");
}else if("delete".equals(type.trim())){
//执行删除动物操作
String id= request.getParameter("aid");
int aid = Integer.parseInt(id);
AnimalDAO animaldao = new AnimalDAO();
animaldao.deleteAnimal(aid);
response.sendRedirect("AnimalSrevlet");
}else if("get".equals(type.trim())){
//执行通过姓名(id)查找动物信息操作
String aid= request.getParameter("aid");
int id = Integer.parseInt(aid);
AnimalDAO animaldao= new AnimalDAO();
Animal animal = new Animal();
animal=animaldao.getAnimalByid(id);
request.setAttribute("animal", animal);
request.getRequestDispatcher("/animal/update.jsp").forward(request, response);
}else if("update".equals(type.trim())){
//执行修改动物信息的操作
String aid = request.getParameter("id");
String aname = request.getParameter("name");
String atime = request.getParameter("times");
int id = Integer.parseInt(aid);
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Date time=null;
try {
time=sdf.parse(atime);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Animal animal = new Animal();
animal.setAid(id);
animal.setAname(aname);
animal.setAtime(time);
AnimalDAO animaldao = new AnimalDAO();
animaldao.updateAnimal(animal);
response.sendRedirect("AnimalSrevlet");
}
}
}
//JSP
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
//animal/List.jsp
<body>
<a href="animal/add.jsp">添加信息</a>
<table border="1">
<tr>
<th>aid号</th>
<th>aname姓名</th>
<th>atime时间</th>
<th>操作</th>
</tr>
<c:forEach items="${list}" var="list">
<tr>
<td>${list.aid }</td>
<td>${list.aname }</td>
<td>${list.atime }</td>
<td>
<a href="servlet/AnimalSrevlet?type=delete&aid=${list.aid }"> 删除</a>
<a href="servlet/AnimalSrevlet?type=get&aid=${list.aid }">修改</a>
</td>
</tr>
</c:forEach>
</table>
</body>
//animal/add.jsp
<body>
<form action="servlet/AnimalSrevlet?type=add" method="post">
<table>
<tr>
<td>动物id <input type="text" name="id"/></td>
</tr>
<tr>
<td>动物名 <input type="text" name="name"/></td>
</tr>
<tr>
<td>时 间 <input type="text" name="times"/><font color="red">填写格式:yyyy-MM-dd</font></td>
</tr>
<tr>
<td colspan="1" align="right">
<input type="submit" value="添加"/>
<input type="submit" value="取消"/>
</td>
</tr>
</table>
</form>
</body>
//animal/update.jsp
<body>
<form action="servlet/AnimalSrevlet?type=update" method="post">
<table>
<tr>
<td>动物id<input type="text" name="id" value="${animal.aid }"/></td>
</tr>
<tr>
<td>动物姓名<input type="text" name="name" value="${animal.aname }"/></td>
</tr>
<tr>
<td>时间<input type="text" name="times" value="${animal.atime }"/><font color="red">填写格式:yyyy-MM-dd</font></td>
</tr>
<tr>
<td colspan="2" align="right">
<input type="submit" value="修改"/>
</td>
</tr>
</table>
</form>
</body>