理财产品信息管理系统(使用ajax实现)

直接上代码:

pojo:

ProductManager:
productManager
package pojo;

public class ProductManager {


    private String id;
    private int risk;
    private String income;
    private String saleStarting;
    private String saleEnd;

    @Override
    public String toString() {
        return "productManager{" +
                "id='" + id + '\'' +
                ", risk=" + risk +
                ", income='" + income + '\'' +
                ", saleStarting='" + saleStarting + '\'' +
                ", saleEnd='" + saleEnd + '\'' +
                ", end='" + end + '\'' +
                '}';
    }

    private String end;

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public int getRisk() {
        return risk;
    }

    public void setRisk(int risk) {
        this.risk = risk;
    }

    public String getIncome() {
        return income;
    }

    public void setIncome(String income) {
        this.income = income;
    }

    public String getSaleStarting() {
        return saleStarting;
    }

    public void setSaleStarting(String saleStarting) {
        this.saleStarting = saleStarting;
    }

    public String getSaleEnd() {
        return saleEnd;
    }

    public void setSaleEnd(String saleEnd) {
        this.saleEnd = saleEnd;
    }

    public String getEnd() {
        return end;
    }

    public void setEnd(String end) {
        this.end = end;
    }
}

Dao层:

BaseDao等JDBC省略

ProductManagerDao:
productManagerDao
package dao;

import pojo.ProductManager;

import java.sql.SQLException;
import java.util.List;

public interface ProductManagerDao {
    /**
     * 查询所有信息
     */
    List<ProductManager> getProductManagerList() throws SQLException;

    /**
     * 模糊查询 信息 通过ID
     */
    List<ProductManager> selectGetProductManagerList(String id) throws SQLException;

    /**
     *  查询信息 通过风险评级
     */
    List<ProductManager> selectR(int risk) throws SQLException;

    /**
     * 添加信息
     */
    int add(ProductManager productManager);
}

 

ProductManagerDaoImpl:
productManagerDaoImpl

 

service层:

ProductManagerDaoService:
productManagerDaoService
package service;

import pojo.ProductManager;

import java.sql.SQLException;
import java.util.List;

public interface ProductManagerDaoService {
    /**
     * 查询所有信息
     */
    List<ProductManager> getProductManagerList();

    /**
     * 模糊查询 信息
     */

    List<ProductManager> selectGetProductManagerList(String id);

    /**
     *  查询信息 通过风险评级
     */
    List<ProductManager> selectR(int risk);

    /**
     * 添加信息
     */
    int add(ProductManager productManager);

}

 

ProductManagerDaoServiceImpl:
productManagerDaoServiceImpl

 

servlet:

 

 

add:

addServlet
package servlet;

import pojo.ProductManager;
import service.ProductManagerDaoService;
import service.ProductManagerDaoServiceImpl;

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;

@WebServlet("/add")
public class Add extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("请求是否进入...");
        resp.setContentType("text/html;charset=UTF-8");

        ProductManager productManager = new ProductManager();
        ProductManagerDaoService productManagerDaoService = new ProductManagerDaoServiceImpl();
        String id = req.getParameter("id");
        String select_value = req.getParameter("select_value");
        Integer integer = new Integer(select_value);
        String income = req.getParameter("income");
        String saleStarting = req.getParameter("saleStarting");
        String saleEnd = req.getParameter("saleEnd");
        String end = req.getParameter("end");
        productManager.setId(id);
        productManager.setRisk(integer);
        productManager.setIncome(income);
        productManager.setSaleStarting(saleStarting);
        productManager.setSaleEnd(saleEnd);
        productManager.setEnd(end);
        int result = productManagerDaoService.add(productManager);

        if(result>0){

            System.out.println("添加成功!");
        }else{
            System.out.println("添加失败!");
        }

    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
       this.doGet(req,resp);
    }
}

ProductManagerServlet:
productManagerServlet:
package servlet;

import com.google.gson.Gson;
import pojo.ProductManager;
import service.ProductManagerDaoService;
import service.ProductManagerDaoServiceImpl;

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.util.List;

@WebServlet("/ProductManagerServlet")
public class ProductManagerServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("text/html;charset=UTF-8");
        ProductManagerDaoService productManagerDaoService = new ProductManagerDaoServiceImpl();
        List<ProductManager> productManagerList = productManagerDaoService.getProductManagerList();
        Gson gson = new Gson();

        System.out.println(gson.toJson(productManagerList));
        resp.getWriter().println(gson.toJson(productManagerList));

    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doGet(req, resp);
    }
}

selectByID:
selectByID
package servlet;

import com.google.gson.Gson;
import pojo.ProductManager;
import service.ProductManagerDaoService;
import service.ProductManagerDaoServiceImpl;

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.util.List;

@WebServlet("/selectByID")
public class selectByID extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("text/html;charset=UTF-8");
        ProductManagerDaoService productManagerDaoService = new ProductManagerDaoServiceImpl();

        Gson gson = new Gson();
        String userCode = req.getParameter("userCode");
        List<ProductManager> productManagerList = productManagerDaoService.selectGetProductManagerList(userCode);

        resp.getWriter().println(gson.toJson(productManagerList));
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doGet(req,resp);
    }
}

selectByR:
selectByR
package servlet;

import com.google.gson.Gson;
import pojo.ProductManager;
import service.ProductManagerDaoService;
import service.ProductManagerDaoServiceImpl;

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.util.List;

@WebServlet("/selectByR")
public class selectByR extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        System.out.println("请求是否进入");
        resp.setContentType("text/html;charset=UTF-8");
        Gson gson = new Gson();
        ProductManagerDaoService productManagerDaoService = new ProductManagerDaoServiceImpl();
        String selected_val = req.getParameter("selected_val");
        Integer integer = new Integer(selected_val);

        List<ProductManager> list = productManagerDaoService.selectR(integer);
//        System.out.println(list);
        System.out.println(gson.toJson(list));
        resp.getWriter().println(gson.toJson(list));
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doGet(req, resp);
    }
}

 

Web方面:

test.jsp:

test.jsp
<%--
  Created by IntelliJ IDEA.
  User: admin
  Date: 2019/10/19
  Time: 15:17
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>测试</title>
    <script src="jquery.min.js"></script>
    <script>
        function clickBto1(){
            var userCode = $("#userCode").val();
            return userCode;
        }
        function clickBto2(){
            var value = document.getElementById("select_id").value;
            return value;

        }
        $(function () {
            $.ajax(
                {
                    "url": "ProductManagerServlet",//要提交的url路径
                    "type": "get",     // 发送的请求方法
                    "data": "userName=" + "1",  // 要发送到服务器的数据
                    "dataType": "json",   // 指定返回的数据格式
                    "success": callBack,    //响应成功后要执行的代码
                    "error": function () {  // 请求失败后要执行的代码
                        alert("用户名验证时出现错误,请稍后再试或与管理员联系!");
                    }
                });

            function callBack(data) {
                // 添加新数据之前先删除之前的数据
                // $("#tabl tr:not(:first)").remove();

                var trs = "<tr style='background-color: dimgray'><td>产品代码</td><td>风险评级</td><td>预期收益</td><td>发售起始日</td><td>发售截止日</td><td>产品到期日</td></tr>";

                $.each(data,function (i,val) {
                    if(i%2!=0){
                        trs += "<tr style='background-color: dimgray'><td>"
                            +data[i].id +"</td><td>"+data[i].risk
                            +"</td><td>"+data[i].income+"</td><td>"
                            +data[i].saleStarting+"</td><td>"+data[i].saleEnd
                            +"</td><td>"+data[i].end+"</td></tr>";
                    }else{
                        trs += "<tr><td>"
                            +data[i].id +"</td><td>"+data[i].risk
                            +"</td><td>"+data[i].income+"</td><td>"
                            +data[i].saleStarting+"</td><td>"+data[i].saleEnd
                            +"</td><td>"+data[i].end+"</td></tr>";
                    }

                })
                // document.getElementById("b").innerHTML = trs;
                $("#b").html(trs);
            }
            $("#select_id").click(function () {
                $("#bto").click(function () {
                    var selected_val = clickBto2();
                    $.ajax(
                        {
                            "url": "selectByR",//要提交的url路径
                            "type": "get",     // 发送的请求方法
                            "data": "selected_val=" + selected_val,  // 要发送到服务器的数据
                            "dataType": "json",   // 指定返回的数据格式
                            "success": callBack,
                            "error": function () {  // 请求失败后要执行的代码
                                alert("用户名验证时出现错误,请稍后再试或与管理员联系!");
                            }
                        })
                })
            })
            $("#userCode").click(function () {
                $("#bto").click(function () {
                    var userCode = clickBto1();
                    $.ajax(
                        {
                            "url": "selectByID",//要提交的url路径
                            "type": "get",     // 发送的请求方法
                            "data": "userCode=" + userCode,  // 要发送到服务器的数据
                            "dataType": "json",   // 指定返回的数据格式
                            "success":callBack,
                            "error": function () {  // 请求失败后要执行的代码
                                alert("用户名验证时出现错误,请稍后再试或与管理员联系!");
                            }
                        })
                })
            })

        })
    </script>
</head>
<body>

<table id="b"border="1"width="300px">
    产品代码: <input type="text"name="userCode"id="userCode"/> &nbsp;
    风险评级:
    <select id="select_id">
        <option value="0">--请选择--</option>
        <option value="1">  R1 </option>
        <option value="2">  R2 </option>
    </select>
    <button id="bto">查询</button> &nbsp;&nbsp;
    <a href="add.jsp">新增理财信息</a>
    <br><br>

</table>

</body>
</html>

add.jsp:

add.jsp
<%--
  Created by IntelliJ IDEA.
  User: admin
  Date: 2019/10/19
  Time: 15:51
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>添加页面</title>
    <script src="jquery.min.js"></script>
    <script>
        // function getSltValue() {
        //     // var value = document.getElementById("select_id").value;
        //     // alert(value);
        //     var serializeArray = $("#a").serializeArray();
        //     alert(serializeArray);
        //     return serializeArray;
        // }
        $(function () {


            $("#bto1").click(function () {

                var id = $("#id").val();
                var select_value = document.getElementById("select_id").value;
                var income = $("#income").val();
                var saleStarting = $("#saleStarting").val();
                var saleEnd = $("#saleEnd").val();
                var end = $("#end").val();
                alert(id+" "+select_value+" "+income+" "+saleStarting+" "+saleEnd+" "+end)

                $.ajax({

                    type: "Get",
                    dataType: "text",
                    url: "add",
                    data: {"id":id,"select_value":
                        select_value,"income":income,"saleStarting":saleStarting,"saleEnd":saleEnd,"end":end},
                    success: function(data){
                        // document.getElementById("b").innerHTML = data;
                        alert(data)

                    },
                    error: function(msg){
                        alert("请联系客户")
                    }
                })
            })

        })

    </script>
</head>
<body>

<form id="a">
    <table>
        <tr><td>
            <h3>新增理财信息</h3>
        </td></tr>
        <tr><td>

            产品代码: <input type="text"name="id"id="id"/> <br><br>
            风险评级:
            <select id="select_id">
                <option value="0">--请选择--</option>
                <option value="1">  R1 </option>
                <option value="2">  R2 </option>
            </select>
            <br><br>
            预期收益: <input type="text"name="income"id="income"/><br><br>
            发售起始日: <input type="date"name="saleStarting"id="saleStarting"/><br><br>
            发售截止日: <input type="date"name="saleEnd"id="saleEnd"/> <br><br>
            产品到期日: <input type="date"name="end"id="end"/><br><br>
            <button id="bto1">保存</button>
            <input type="button" value="返回" onclick="javascript:window.location.href ='test.jsp'">

        </td></tr>
    </table>
</form>
</body>
</html>

 

 

MYSQL方面:

 

 

实现效果:

 

posted @ 2019-10-21 16:43  AxeBurner  阅读(1493)  评论(3编辑  收藏  举报