如何分离从数据库中检索到的json对象

时间:2015-09-04 10:13:37

标签: ajax json database drop-down-menu

我试图分离从数据库中重新获得的json对象,我需要将值作为单个对象传递给ajax。帮帮我 我的代码是

这是我的servlet页面     


JSONObject obj = new JSONObject();

    String sql1=null;

    String check=request.getParameter("javabasics");
    System.out.println("check"+check);
    if(check.equals("java") )
    {
        String sql="select javabasics from topics ";
        sql1=sql;
    } else
    {

        System.out.println("error");
        sql1="select advancejava from topics ";
    }


    try
    {
         //sql1=sql;
        java.sql.Connection con=ConnectionProvider.getConnection();
        /*String sql="select javabasics from topics ";*/
        PreparedStatement pst=con.prepareStatement(sql1);
        ResultSet rs=  pst.executeQuery(sql1);
        while(rs.next())
        {

               obj.put(rs.getString(1),rs.getString("javabasics"));







        }

          StringWriter out = new StringWriter();
          obj.writeJSONString(out);

          String json = out.toString();
          response.getWriter().write(json);
          System.out.print(json);
         rs.close();
         pst.close();
         con.close();

    } catch(Exception e)
    {
        e.printStackTrace();
    }




}

0 个答案:

没有答案