package cn.FinancingProduct.servlet;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Date;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.alibaba.fastjson.JSON;
import cn.FinancingProduct.entity.FinancingProduct;
import cn.FinancingProduct.service.FinancingProductService;
import cn.FinancingProduct.service.Impl.FinancingProductServiceImpl;
public class FinancingProductServlet extends HttpServlet{
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doPost(request,response);
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.setCharacterEncoding("UTF-8");
response.setCharacterEncoding("UTF-8");
response.setContentType("text/html;charset=UTF-8");
String opr = request.getParameter("opr");
PrintWriter out = response.getWriter();
FinancingProductService service = new FinancingProductServiceImpl();
//查询全部
if(opr.equals("list")){
List list = null;
try {
list =service.getAll();
} catch (SQLException e) {
list = new ArrayList();
e.printStackTrace();
}
request.getSession().setAttribute("list", list);
response.sendRedirect("index.jsp");
}else if(opr.equals("Servlet")){
String pid =request.getParameter("id");
String risk = request.getParameter("risk");
ListproductList=null;
try {
productList=service.selectAll(pid,Integer.parseInt(risk));
} catch (Exception e) {
e.printStackTrace();
productList=new ArrayList();
}
//用异步实现 输出
request.getSession().setAttribute("list",productList);
response.sendRedirect("index.jsp");
}else if(opr.equals("validate")){
//1.接收ajax请求提交来的pid参数
String pid=request.getParameter("pid");
int count=0;
try {
//2.调用查询数据库
count=service.selectCount(pid);
} catch (SQLException e) {
e.printStackTrace();
}
//3.根据数据输出JSON格式的结果字符串
boolean isUsed=false;
if(count>0){
isUsed=true;
}
out.print("{"isUsed":""+isUsed+""}");
out.close();
}else if(opr.equals("add")){
//1.获取参数
String pid=request.getParameter("pid");
String risk=request.getParameter("risk");
String income=request.getParameter("income");
String saleStarting=request.getParameter("saleStarting");
String saleEnd=request.getParameter("saleEnd");
String end=request.getParameter("end");
//2.封装成实体对象
FinancingProduct product = new FinancingProduct();
product.setId(pid);
product.setRisk(Integer.parseInt(risk));
product.setIncome(income);
product.setSaleStarting(Date.valueOf(saleStarting));
product.setSaleEnd(Date.valueOf(saleEnd));
product.setEnd(Date.valueOf(end));
//3.调用Service执行插入
int rows=0;
try {
rows=service.save(product);
} catch (Exception e) {
e.printStackTrace();
}
if(rows>0){
out.print("");
}
}
}
}
免责声明:本网站部分内容由用户自行上传,若侵犯了您的权益,请联系我们处理,谢谢!联系QQ:2760375052