i need to view records in to the Datatable using jsp json. data is displayed to datatable successfully. my problem is only one row data is displaying.the last row of database record is repecting 4 times.i attached image below what was shown on the datatable and what are the data inside mysql database.
Record Shown one the datatable look like this.data duplicated
Mysqldata Data
Datatable
function get_all()
{
$('#tbl-projects').dataTable().fnDestroy();
$.ajax({
url : "all_project.jsp",
type : "GET",
dataType : "JSON",
success:function (data){
console.log(data);
$('#tbl-projects').dataTable({
"aaData": data,
"scrollX": true,
"aoColumns":
[
{"sTitle": "Name", "mData": "name"},
{"sTitle": "Course", "mData": "course"},
{"sTitle": "Fee", "mData": "fee"},
]
});
},
error: function (xhr) {
console.log('Request Status: ' + xhr.status );
console.log('Status Text: ' + xhr.statusText );
console.log(xhr.responseText);
var text = $($.parseHTML(xhr.responseText)).filter('.trace-message').text();
}
});
} details.jsp
<%@page import="org.json.simple.JSONArray"%>
<%@page import="org.json.simple.JSONObject"%>
<%@page import="org.json.simple.parser.JSONParser"%>
<%@page import="org.json.simple.parser.ParseException"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@page import="java.sql.*" %>
<%
JSONObject obj = new JSONObject();
JSONArray list = new JSONArray();
Connection con;
PreparedStatement pst;
ResultSet rs;
Class.forName("com.mysql.jdbc.Driver");
con = DriverManager.getConnection("jdbc:mysql://localhost/studcrud", "root","");
String query="select * from records";
Statement stmt=con.createStatement();
rs=stmt.executeQuery(query);
while(rs.next())
{
String id = rs.getString("id");
String name = rs.getString("name");
String course = rs.getString("course");
String fee = rs.getString("fee");
obj.put("name", name);
obj.put("course", course);
obj.put("fee", fee);
obj.put("id", id);
list.add(obj);
}
out.print(list.toJSONString());
out.flush();
%>