jTable jquery不删除数据库中的记录

时间:2014-08-06 04:35:18

标签: java jquery jsp jquery-jtable

我在jsp页面使用jTable jquery,我可以添加新记录和 客户端 - 服务器上的编辑记录中的数据非常好。我也可以在客户端删除记录,但不删除服务器上的记录(数据库)。这是我现在使用的代码

<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Setup and Load Data to jTable using Servlets and JSP</title>
<!-- Include one of jTable styles. -->
<link href="css/themes/metro/blue/jtable.css" rel="stylesheet" type="text/css" />
<link href="css/jquery-ui-1.10.3.custom.css" rel="stylesheet" type="text/css" />
<!-- Include jTable script file. -->
<script src="js/jquery-1.8.2.js" type="text/javascript"></script>
<script src="js/jquery-ui-1.10.3.custom.js" type="text/javascript"></script>
<script src="js/jquery.jtable.js" type="text/javascript"></script>
<script type="text/javascript">
    $(document).ready(function () {
        $('#ReasonCodeContainer').jtable({
            title: 'Table of Reason Code',
            actions: {
                listAction: 'ReasonCode?action=list',
                createAction:'ReasonCode?action=create',
                updateAction: 'ReasonCode?action=update',
                deleteAction: 'ReasonCode?action=delete'
            },
            fields: {
                reason_code: {
                    title: 'Reason Code',
                    width: '30%',
                    clientOnly: false
                },
                reason_desc: {
                    title: 'Reason Desc',
                    width: '40%',
                    clientOnly: false
                }                
            }
        });   
        $('#ReasonCodeContainer').jtable('load');
    });
</script>
</head>
<body>
<div style="width:60%;margin-right:20%;margin-left:20%;text-align:center;">
<h1>Add Reason Code</h1>
<div id="ReasonCodeContainer"></div>
</div>
</body>
</html>

servlet中的代码:

if (action.equals("delete")) { //Delete record                   
                try {             
                        ReasonCodeBean record = new ReasonCodeBean();
                        daorscode.deleteReasonCode(record);
                        //Convert Java Object to Json       
                        String json = "{\"Result\":\"OK\"}";

                        //Return Json in the format required by jTable plugin
                        response.getWriter().print(json);
                } catch (Exception ex) {
                    String error = "{\"Result\":\"ERROR\",\"Message\":" + ex.getStackTrace().toString() + "}";
                    response.getWriter().print(error);
                }
            }

public void deleteReasonCode(ReasonCodeBean record) throws ParseException {
    try {
        PreparedStatement preparedStatement = connection
                .prepareStatement("DELETE FROM reason_code WHERE reason_code=? AND reason_desc=? ");
        // Parameters start with 1
        preparedStatement.setInt(1, record.getReason_code());
        preparedStatement.setString(2, record.getReason_desc());
        preparedStatement.executeUpdate();
    } catch (SQLException e) {
        e.printStackTrace();
    }
}

1 个答案:

答案 0 :(得分:2)

if (action.equals("delete")) { //Delete record                   
            try {   
    //first get your record id from the request parameter
      Integer recordId=Integer.parseInt(request.getParameter("reason_code")
                                                                    .toString());

                    ReasonCodeBean record = new ReasonCodeBean();
                    //set recordId in in record object
                    record.setReasonCode(recordId);
                    //also make changes in delete method(remove where condition for description)
                    daorscode.deleteReasonCode(record);
                    //Convert Java Object to Json       
                    String json = "{\"Result\":\"OK\"}";

                    //Return Json in the format required by jTable plugin
                    response.getWriter().print(json);
            } catch (Exception ex) {
                String error = "{\"Result\":\"ERROR\",\"Message\":" + ex.getStackTrace().toString() + "}";
                response.getWriter().print(error);
            }
        }