1

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

enter image description here

Mysqldata Data

enter image description here

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

 %>
  • Try without `toJSONString()` and see if you get `json` as response or not. – Swati Aug 02 '19 at 05:22
  • if i try like that out.print(list); it is says errors cannot find the symbols and there is no ajax respones sir – design kobi Aug 02 '19 at 05:26
  • Here change this `list.put(obj);` to `list.add(obj);` ,also did you add `jar` file ? – Swati Aug 02 '19 at 05:28
  • which jar file sir need to add – design kobi Aug 02 '19 at 05:30
  • check [here](https://stackoverflow.com/questions/13155200/org-json-simple-cannot-be-resolved) or [this](http://www.java2s.com/Code/Jar/j/Downloadjsonsimple11jar.htm) you can download it – Swati Aug 02 '19 at 05:32
  • sir. i added the jar. file no error on the program when i run no data passing through ajax response i see error on console. i added above please check sir – design kobi Aug 02 '19 at 05:46
  • Remove this `<% Class.forName("com.mysql.jdbc.Driver"); %>` you have declare that `2` times in your code , also `PWC6199: Generated servlet error: ';' expected` it is on which line ? – Swati Aug 02 '19 at 05:55
  • ya removed line sir . i think may be the problem of this line out.print(list.toJSONString()); indicate the error of list – design kobi Aug 02 '19 at 06:02
  • out.print(list.add(obj)); if i written like this it pass the error cannot find the symbol – design kobi Aug 02 '19 at 06:22
  • [cannot find the symbol](https://stackoverflow.com/questions/25706216/what-does-a-cannot-find-symbol-compilation-error-mean) check this , it might help you to solve that , i think the problem here is you have not imported `jar` correctly . – Swati Aug 02 '19 at 06:37
  • i downlaod all jar files to the library json-simple-1.1.jar – design kobi Aug 02 '19 at 06:43
  • and added sir. but i don't why it is not working – design kobi Aug 02 '19 at 07:08
  • sir json-simple-1.1.jar library. i imported sir. can you give you email id i send the code to you – design kobi Aug 02 '19 at 07:11
  • sir now it is working output passing [{"name":"ramlangam","bal":"3077"}] like this json thn how to pass the values into relvent textboxs – design kobi Aug 02 '19 at 07:15
  • sir there how to pass this json [{"name":"ramlangam","bal":"3077"}] data to relavent textboxs i tried like this $("#name").val(msg[0].name); but not working – design kobi Aug 02 '19 at 07:32

1 Answers1

0

You need to parse json in ajax success function i.e :

<script language="javascript">
   function ClickMe_Click(){
    $.ajax({

        type: "post",
        url: "details.jsp",
        data: {"a": $("#accno").val()},
        success: function(msg) {
    //parsing json response
            var obj = JSON.parse(msg); 
              alert(obj[0].bal);

                 //setting value in input field

                  $("#bal").val(obj[0].bal);
                 $("#name").val(obj[0].name);
        },
        error:function (xhr, ajaxOptions, thrownError){
            alert(xhr.status);
            alert(thrownError);
        }  
    });
    return false;
}
</script>
Swati
  • 28,069
  • 4
  • 21
  • 41