我编写了一个带有数据库连接的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格式从数据库获取数据如下:
{ “数据”:[{ “SL”: “1”, “电子邮件”: “srinath @ GMA”, “名称”: “shivasrinat”, “摩比”: “9849692921”, “添加”:“manikonda “},{” SL “:” 2" , “电子邮件”: “srinath @ GMA”, “名称”: “shivasrinat”, “摩比”: “9849692921”, “添加”: “manikonda”}]}
答案 0 :(得分:0)
你检查了这个link吗?可能这会对你有所帮助。实际上究竟是什么问题?在dojo-grid中加载json数据有什么问题吗?或者您想知道如何实施?
答案 1 :(得分:0)
例如,
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();