繁体   English   中英

如何使用JSONArray从数据库中检索项目

[英]How to retrieve items from database using JSONArray

我正在尝试使用JSONArray从数据库检索数据。 我的代码运行良好,但是它仅将数据库中的最后一项显示为数据库中的项数。

我的代码是:

    Gson gson = new Gson();
    JsonObject myObj = new JsonObject();
    JSONObject Obj = new JSONObject();
    Connection conn = null; 
    try {
        conn=prepareConnection();
    } catch (ClassNotFoundException e1) {
        System.out.println( "Error --> " + displayErrorForWeb(e1));
        e1.printStackTrace();
    } catch (SQLException e1) {
        System.out.println( "Error --> " + displayErrorForWeb(e1));
        e1.printStackTrace();
    }

    try {
        JsonElement commentObj;

        StringBuilder sb1=new StringBuilder(1024);
        sb1.append("insert into ").append(uname.trim()).append("vcomments values(?,?,?,?)");
        String sql1=sb1.toString();
        PreparedStatement stmt1 = conn.prepareStatement(sql1);
         stmt1.setString(1,uname);
         stmt1.setString(2,message);
         stmt1.setInt(3,itemId);
         stmt1.setInt(4,albumId);
         int i=stmt1.executeUpdate();

        if(i!=1){
        myObj.addProperty("success", false);
    }
    else {
        myObj.addProperty("success", true);
    }
    PreparedStatement stmt = null;    
    String sql = null;

     try {     

        StringBuilder sb=new StringBuilder(1024);
        sb.append("select * from ").append(uname.trim()).append("vcomments").append(" where itemid=").append(itemId).append(" and albumid=").append(albumId);
        sql=sb.toString();
        stmt = conn.prepareStatement(sql);
        ResultSet rs = stmt.executeQuery();
        ArrayList<JSONObject> CommArray=new ArrayList<JSONObject>();

         while(rs.next()){
            Obj.put("uname",rs.getString("uname").trim());
            Obj.put("comment",rs.getString("comments").trim());
            CommArray.add(Obj);

            }    
         JSONArray arrayObj=JSONArray.fromObject(CommArray);
         commentObj=gson.toJsonTree(arrayObj);
         myObj.add("commentInfo", commentObj);
         out.println(myObj.toString());
         rs.close();                                                              
         stmt.close();                                                            
         stmt = null;                                                             


         conn.close();                                                            
         conn = null;                                                  

     }                                                              
     catch(Exception e){System.out.println( "Error --> " + displayErrorForWeb(e));}                     

收到的回复:

 {"success":true,"commentInfo":[{"uname":"shyam","comment":"erxdg"},{"uname":"shyam","comment":"erxdg"},{"uname":"shyam","comment":"erxdg"},{"uname":"shyam","comment":"erxdg"}]}

该数据库包含两个项目。 最后一项是erxdg Servlet仅将响应发送回最后一个项目。

请任何人帮我修复它.....谢谢...........

您必须为每个结果集创建一个新的JSONObject 在循环中创建一个,并将其添加到数组中。 否则,您只是替换旧对象的字段,并在结果中添加对该对象的另一个引用。

while(rs.next()){
    //new line:
    JSONObject Obj = new JSONObject();
    // original part looks fine:
    Obj.put("uname",rs.getString("uname").trim());
    Obj.put("comment",rs.getString("comments").trim());
    CommArray.add(Obj);
}   

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM