• AJAX增删查


    数据库

    CREATE DATABASE crmweb;

    CREATE TABLE `customerstatus` (
    `id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT '序号',
    `customerstatus` varchar(100) DEFAULT NULL COMMENT '客户状态',
    `customerdescription` varchar(100) DEFAULT NULL COMMENT '客户描述',
    PRIMARY KEY (`id`)
    ) ENGINE=InnoDB AUTO_INCREMENT=6 DEFAULT CHARSET=utf8;

    C3P0

    <?xml version="1.0" encoding="UTF-8"?>
    <c3p0-config>
    <named-config name="webs001"> <!-- 自定义配置 -->
    <!-- mysql驱动 -->
    <property name="driverClass">com.mysql.jdbc.Driver</property>
    <!-- 数据库连接 -->

    <property name="jdbcUrl">jdbc:mysql://localhost:3306/crmweb?characterEncoding=utf-8</property>

    <!-- 数据库用户名 -->
    <property name="user">root</property>
    <!-- 数据库密码 -->
    <property name="password">mysql</property>
    <!-- 初始化连接池大小 -->
    <property name="initialPoolSize">10</property>
    <!-- 最大空闲时间(秒) - 若为0则永不丢弃 -->
    <property name="maxIdleTime">30</property>
    <!--每40秒检查所有连接池中的空闲连接。Default: 0 -->
    <property name="idleConnectionTestPeriod">40</property>
    <!-- 连接池中保留的最大连接数 -->
    <property name="maxPoolSize">100</property>
    <!-- 最小保留连接数 -->
    <property name="minPoolSize">10</property>
    <!-- 最大连接数 -->
    <property name="maxStatements">200</property>
    </named-config>
    </c3p0-config>

    WEB.xml

    <!-- Customerstatus查询 -->

    <servlet-mapping>
    <servlet-name>Customerstatus</servlet-name>
    <url-pattern>/Customerstatus/ajxsel</url-pattern>
    </servlet-mapping>

    <!-- Customerstatus增加 -->

    <servlet-mapping>
    <servlet-name>Customerstatus</servlet-name>
    <url-pattern>/Customerstatus/ajxadd</url-pattern>
    </servlet-mapping>

    <!-- Customerstatus删除 -->
    <servlet-mapping>
    <servlet-name>Customerstatus</servlet-name>
    <url-pattern>/Customerstatus/ajxdel</url-pattern>
    </servlet-mapping>

    Customerstatus.jsp页面


    <script type="text/javascript" src="js/jquery-3.2.1.min.js" ></script>

    <div class="a">
    <label>请输入客户状态:</label>
    <input type="text" class="inp" />
    <input type="button" value="查询" class="inp2"/>
    <a href="customerstatustoadd.jsp">
    <input type="button" value="添加" class="inp3"/>
    </a>
    </div>
    <div class="d"></div>
    <div class="b">
    <table>
    <tr style="background: #cdeaf9;"><th class="widt">序号</th><th class="width1">客户状态</th><th class="width">状态描述</th><th class="wid">基本操作</th></tr>
    <tbody id="td">

    </tbody>

    <%-- <c:forEach var="v" items="${list }" varStatus="status" >
    <tr><td class="widt">${status.index+1}</td>
    <td class="width1">${v.customerstatus }</td>
    <td class="width">${v.customerdescription }</td>

    <td class="wid">
    <a href="Customerstatus/delete?id=${v.id }">
    <img src="images/del.gif"/>
    删除</a>
    </td>
    </tr>
    </c:forEach> --%>
    </table>

    </div>
    <div class="c">
    <span class="if">共有3条记录,当前第1/1页</span>
    <img class="img1" src="images/first.gif" />
    <img src="images/back.gif" />
    <img src="images/next.gif" />
    <img src="images/last.gif" />
    <font>转到第
    <input type="text" class="else"/>
    页</font>
    <img src="images/go.gif" />
    </div>

    <script type="text/javascript">
    $(".inp2").click(function(){
    var s=$(".inp").val();
    $("tr").each(function(i,x){
    if($(x).children().eq(1).text().indexOf(s)<0){
    $(x).hide();
    }else{$(x).show();}
    });
    });
    $(function(){
    $("#td").html("");
    sele();
    })
    function sele(){
    $.ajax({
    type:"post",
    url:"Customerstatus/ajxsel",
    data:"",
    dataType:"json",
    success:function(data){
    var html="";
    $.each(data,function(i,v){
    html+="<tr><td>"+v.id+"</td><td>"+v.customerstatus+"</td><td>"+v.customerdescription+"</td><td><img src='images/del.gif'/><span onclick='dele("+v.id+")'>删除</span></td></tr>"
    })
    $("#td").html(html);
    }
    })
    }
    function dele(v){
    var r=confirm("是否确认删除?");
    if(!r){return;}
    $.ajax({
    type:"post",
    url:"Customerstatus/ajxdel",
    data:{id:v},
    dataType:"json",
    success:function(data){
    if(data.msg){
    sele();
    }
    }
    })
    }
    </script>

    Customerstatustoadd.jsp页面


    <script type="text/javascript" src="js/jquery-3.2.1.min.js" ></script>

    <body>
    <form id="asdasf">
    <table>
    <tr><td>
    <span>请输入客户类型的详细信息</span>
    </td></tr>
    <tr><td>
    <font>客户状态名称:</font>
    <input type="text" name="name" class="a" />
    </td></tr>
    <tr><td>
    <font>客户状态描述:</font>
    <input type="text" name="description" class="a" />
    </td></tr>
    <tr><td>
    <input type="button" value="提交" class="b" id="but"/>

    <input type="button" value="返回" class="b"/>
    </td></tr>
    </table>

    </form>
    </body>
    <script type="text/javascript">
    $("#but").click(function(){
    $.ajax({
    type:"post",
    url:"Customerstatus/ajxadd",
    data:$("#asdasf").serialize(),
    dateType:"json",
    success: function(data){
    window.location.href="customerstatus.jsp"
    }
    })
    });

    </script>

    Customerstatus.java

    protected void service(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {

    Connection connection = null;
    String url = request.getServletPath();

    if (url.contains("Customerstatus/ajxsel")) {
    try {
    connection = DButil.getConn();
    String sql = "SELECT * FROM customerstatus";
    PreparedStatement p = connection.prepareStatement(sql);
    ResultSet set = p.executeQuery();
    List<ProCustomerstatus> list = new ArrayList<ProCustomerstatus>();
    while(set.next()){
    ProCustomerstatus v = new ProCustomerstatus();
    v.setId(set.getInt("id"));
    v.setCustomerstatus(set.getString("customerstatus"));
    v.setCustomerdescription(set.getString("customerdescription"));
    list.add(v);
    }
    p.close();
    DButil.cClose(connection);
    System.out.println(JSONArray.fromObject(list));
    response.setCharacterEncoding("utf-8");
    response.getWriter().write(JSONArray.fromObject(list).toString());
    } catch (SQLException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }

    }
    else if (url.contains("Customerstatus/ajxdel")) {
    int id = Integer.parseInt(request.getParameter("id"));
    String sql = "DELETE FROM customerstatus WHERE id=?";
    try {
    connection = DButil.getConn();
    PreparedStatement preparedStatement = connection.prepareStatement(sql);
    preparedStatement.setInt(1, id);
    preparedStatement.execute();
    preparedStatement.close();DButil.cClose(connection);
    boolean msg = true;
    String json = "{msg:"+msg+"}";
    System.out.println(JSONObject.fromObject(json));
    response.getWriter().write(JSONObject.fromObject(json).toString());
    } catch (SQLException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }

    }
    else if (url.contains("Customerstatus/ajxadd")) {
    request.setCharacterEncoding("utf-8");
    String name = request.getParameter("name");
    String description =request.getParameter("description");
    response.setContentType("text/html;charset=utf-8");
    try {
    connection = DButil.getConn();
    String sql = "INSERT INTO customerstatus(customerstatus,customerdescription)VALUES(?,?)";
    PreparedStatement p = connection.prepareStatement(sql);
    p.setString(1, name);
    p.setString(2, description);
    p.execute();
    p.close();
    DButil.cClose(connection);
    boolean b = true;
    String json = "{b:"+b+"}";
    System.out.println(JSONObject.fromObject(json).toString());
    } catch (SQLException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    }

    }

  • 相关阅读:
    更新内核遇到 requested datatype primary not available
    在tmux缓冲区中搜索
    逻辑卷lvm消失问题.
    linux中的常用的proxy 代理方式
    windows下powershell的包管理工具
    一次docker镜像的迁移
    MAC OS 10.15 挂载ntfs文件系统并设置自动挂载RW模式.
    在linux中设置优先使用ipv4,而不是ipv6
    [搬家]新域名 akagi201.org
    基于Three.js的全景---photo-sphere-viewer
  • 原文地址:https://www.cnblogs.com/love1/p/7692953.html
Copyright © 2020-2023  润新知