JDBC - resultset is returning two instances of JSON

JDBC - resultset is returning two instances of JSON

macksigepmacksigep Posts: 6Questions: 3Answers: 0

The error I am getting is "Invalid JSON Response"; I know why. But I do not know why I am getting 2 different result sets, and also getting 2 instances of a JSON array.

This is my returned JSON

{"data":[{"runnerID":"1","runnerFName":"TestFirst","runnerLName":"TestLast","runnerAge":"0","classID":"1"}]}{"data":[{"runnerID":"1","runnerFName":"TestFirst","runnerLName":"TestLast","runnerAge":"0","classID":"1"},{"runnerID":"2","runnerFName":"TestFirstName2","runnerLName":"TestLastName2","runnerAge":"1","classID":"1"}]}

This is my JDBC connection:

List<Runner> runners = new ArrayList<Runner>();
        CallableStatement callStmt = null;
        ResultSet myRS = null;
        try (Connection conn = ConnectionManager.getConnection()) {
            // #1 connection
            // conn = DriverManager.getConnection(dbRUL, user, password);

            // #2 set stored procedure
            callStmt = conn.prepareCall("{call get_runners()}");
            // #3 execute the stored procedure

            callStmt.execute();
            myRS = callStmt.getResultSet();
            
            while (myRS.next()) {
                 String rid =  myRS.getString("runnerID");
                 //runners.add(runnerID);
                String rfn =  myRS.getString("runnerFName");
                //runners.add(runnerFName);
                String rln = myRS.getString("runnerLName");
                //runners.add(runnerFName + " " + runnerLName);
                String a = myRS.getString("age");
                //runners.add(runnerAge);
                 String cid = myRS.getString("classID");
                    //runners.add(runnerClass);
            Runner r = new Runner(rid, rfn, rln, a, cid);
            runners.add(r); 
            
                Gson gson = new Gson();
 
                DataTable table = new DataTable();
                table.setData(runners);
                String json = gson.toJson(table); 
                
                PrintWriter out = response.getWriter();
                out.print(json);
                out.flush();
                
            }

Answers

  • macksigepmacksigep Posts: 6Questions: 3Answers: 0

    My fix was that I had to move lines 29-37 out of the loop. It was creating a new instance of JSON and adding to the list.

This discussion has been closed.