package servlet;
import Util.DBUtil;
import service.Service;
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 java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
@WebServlet("/AddServlet")
public class AddServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
Service service = new Service();
@Override
protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//设置编译格式
request.setCharacterEncoding("UTF-8");
response.setContentType("text/html;charset=UTF-8");
//接收数据
String date = request.getParameter("date");
String zi = request.getParameter("zi");
String zong = request.getParameter("zong");
String days = request.getParameter("days");
String Mdays = request.getParameter("Mdays");
PrintWriter write = response.getWriter();
PreparedStatement preparedStatement = null;
Connection connection = null;
try {
connection = DBUtil.getConnection();
String sql = "insert into daka(date,zi,zong,days,Mdays) values(?,?,?,?,?)";
preparedStatement = null;
preparedStatement = connection.prepareStatement(sql);
preparedStatement.setString(1, date);
preparedStatement.setString(2, zi);
preparedStatement.setString(3, zong);
preparedStatement.setString(4, date);
preparedStatement.setString(5, Mdays);
preparedStatement.executeUpdate();
} catch (ClassNotFoundException e) {
throw new RuntimeException(e);
} catch (SQLException e) {
throw new RuntimeException(e);
} finally {
DBUtil.close(preparedStatement);
DBUtil.close(connection);
}
request.getRequestDispatcher("liulan.jsp").forward(request,response);
}
private boolean isNumeric(String str) {
for (int i = str.length();--i>=0;){
if (!Character.isDigit(str.charAt(i))){
return false;
}
}
return true;
}
}
package service;
import bean.Dbean;
import dao.Dao;
import java.sql.SQLException;
import java.util.List;
public class Service {
Dao tDao=new Dao();
public boolean add(Dbean ten) throws SQLException, ClassNotFoundException {
boolean f = false;
if(!tDao.name(ten.getDate()))
{
tDao.add(ten);
f=true;
}
return f;
}
public boolean del(String title) throws SQLException, ClassNotFoundException {
tDao.delete(title);
return true;
}
public Dbean getbytitle(String title) throws SQLException, ClassNotFoundException {
return tDao.getbytitle(title);
}
public List<Dbean> list() throws SQLException, ClassNotFoundException {
return tDao.list();
}
}