0

データベースからデータを取得してjson形式で出力するデータベース接続を備えたjspページを作成しました。そして今、その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 に答える