Search code examples
javajqueryjspjquery-jtable

jTable jquery Not Delete record in Database


I use a jTable jquery for jsp page, I can Add new record and Edit data in record very well on client-server. Also I can delete record at the client, but not remove record on the server (database). Here is the code I am using right now

<%@ 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>

Code in 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();
    }
}

Solution

  • 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);
                }
            }