gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 05:34:23 25 4
gpt4 key购买 nike

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

<%@ 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();
}
}

最佳答案

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

关于java - jTable jquery 不删除数据库中的记录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25152296/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com