0

我写了一个带有数据库连接的jsp页面,从数据库中获取数据并以json格式打印。现在我希望将 json 数据发送到 dojo 数据网格

有什么帮助吗?

这是我的jsp代码

 <%@ page import="javax.security.auth.Refreshable"%>
 <%@ page import="java.sql.*"%>
 <%@ page import="java.io.*"%>
 <%@ page import="org.json.simple.JSONObject"%>
 <%@ page import="org.json.simple.JSONArray"%>
 <%@page import="java.util.LinkedList"%>
 <%@page import="java.util.List"%>
 <%
  JSONObject responseObj = new JSONObject();
  List<JSONObject> data = new LinkedList<JSONObject>();
try {
    String dbms = "mysql"; 
    String serverName = "localhost";
    String portNumber = "3306";
    String userName = "root";
    String upassword = "";
    String databaseName = "testind";
    int datacount = 0;

    String connectionURL = "jdbc:" + dbms + "://" + serverName
            + ":" + portNumber + "/" + databaseName;
    Connection connection = null;
    Class.forName("com.mysql.jdbc.Driver");
    connection = DriverManager.getConnection(connectionURL,
            userName, upassword);
    System.out.println("connection::" + connection);

    String strQuery = "select * from user";
    System.out.println(strQuery);
    PreparedStatement st = connection.prepareStatement(strQuery);
    ResultSet rs = st.executeQuery();
    while(rs.next())
    {
        datacount++;
        JSONObject dataobject = new JSONObject();
        dataobject.put("sl",(String.valueOf(datacount)));
                    dataobject.put("name",rs.getString(1));
        dataobject.put("email",rs.getString(2));
        dataobject.put("mobi",rs.getString(3));
        dataobject.put("add",rs.getString(4));
        data.add(dataobject);
        System.out.println("name&email::"+rs.getString(1) +","+rs.getString(2));
    }
    responseObj.put("data",data);

    PrintWriter writer = response.getWriter();
    System.out.println("{\"identifier\":\"data\",\"label\":\"data\",\"items\":" +responseObj+"}");
    //writer.write("{\"identifier\":\"data\",\"label\":\"data\",\"items\":" +responseObj+"}");
    writer.write(" "+responseObj+"");
    writer.flush();
    writer.close();

} catch (Exception e) {
    System.out.println(e);
}`enter code here`
%>  

在这里,我以 json 格式从数据库中获取数据,如下所示:

{"data":[{"sl":"1","email":"srinath@gma","name":"shivasrinat","mobi":"9849692921","add":"manikonda"}, {"sl":"2","email":"srinath@gma","name":"shivasrinat","mobi":"9849692921","add":"manikonda"}]}

4

1 回答 1

0

删除 json 中的数据对象

例如,

                responseObj.put("data", data);
                PrintWriter writer = response.getWriter();
                System.out.println("{\"identifier\":\"data\",\"label\":\"data\",\"items\":" + responseObj + "}");
                writer.write("{\"identifier\":\"Id\",\"label\":\"Organisation\",\"items\":" + responseObj.remove("data") + "}");
                //writer.write(" " + responseObj + "");
                writer.flush();
                writer.close();
于 2016-06-06T04:51:28.430 回答