我正在尝试使用数据库中的数据绘制散点图。我正在从servlet检索数据,当我尝试打印时,它给了我一个JSON对象。但是,在我的jsp页面中,它给了我一个未定义的值或null。有人可以帮我这个忙吗?谢谢。
Servlet
try {
// Register JDBC driver
Class.forName("com.mysql.jdbc.Driver");
// Open a connection
Connection conn = (Connection) DriverManager.getConnection(DB_URL, USER, PASS);
// Execute SQL query
Statement stmt = (Statement) conn.createStatement();
String sql;
sql = "SELECT userName as userName, fpogx, fpogy\r\n"+
"FROM affecttutor.gazedata WHERE fpogv = 1;";
/*sql = "select exercise_working_on as exeWork, sum(no_of_errors) as countErr, idtopic\r\n" +
"from affecttutor.fexdb left join affecttutor.exercises on affecttutor.fexdb.exercise_working_on=affecttutor.exercises.idexercises\r\n" +
"where user_id LIKE '%student08%' && no_of_errors!=0 && exercise_working_on!=0 group by exeWork;";*/
JSONArray list = new JSONArray();
ResultSet rs = stmt.executeQuery(sql);
// Extract data from result set
while(rs.next()) {
//Retrieve by column name
JSONObject object=new JSONObject();
float fpogx = rs.getFloat("fpogx");
float fpogy = rs.getFloat("fpogy");
String userName = rs.getString("userName");
object.put("fpogx", fpogx);
object.put("fpogy", fpogy);
object.put("userName", userName);
list.put(object);
}
out.println(list);
System.out.println(list);
// Clean-up environment
// rs.close();
//stmt.close();
//conn.close();
} catch(SQLException se) {
//Handle errors for JDBC
se.printStackTrace();
} catch(Exception e) {
//Handle errors for Class.forName
e.printStackTrace();
}
JSP
d3.json("analysisDataServlet", function(error, data) {
console.log(data);
if (error) throw error;
JSON
[{"fpogy":0.0348300002515316,"fpogx":-2.7372400760650635,"userName":"student01"},{"fpogy":0.03328999876976013,"fpogx":-2.73757004737854,"userName":"student01"},{"fpogy":0.03189000114798546,"fpogx":-2.73786997795105,"userName":"student01"},{"fpogy":0.5096700191497803,"fpogx":0.4750800132751465,"userName":"student01"},{"fpogy":0.5096700191497803,"fpogx":0.4750800132751465,"userName":"student01"},{"fpogy":0.5096700191497803,"fpogx":0.4750800132751465,"userName":"student01"},{"fpogy":0.5096700191497803,"fpogx":0.4750800132751465,"userName":"student01"},{"fpogy":0.5096700191497803,"fpogx":0.4750800132751465,"userName":"student01"},{"fpogy":0.5012699961662292,"fpogx":0.4617300033569336,"userName":"student01"},{"fpogy":0.5012699961662292,"fpogx":0.4617300033569336,"userName":"student01"},{"fpogy":0.5012699961662292,"fpogx":0.4617300033569336,"userName":"student01"},{"fpogy":0.5012699961662292,"fpogx":0.4617300033569336,"userName":"student01"},{"fpogy":0.5012699961662292,"fpogx":0.4617300033569336,"userName":"student01"},{"fpogy":0.4968700110912323,"fpogx":0.4556199908256531,"userName":"student01"},{"fpogy":0.4968700110912323,"fpogx":0.4556199908256531,"userName":"student01"}]
答案 0 :(得分:0)
<%@page import="java.io.PrintWriter"%>
<%@page contentType="text/html; charset=UTF-8"%>
<%@page import="newpackage.ChartDataController,java.util.*"%>
<%
String json=ChartDataController.getChartData();
response.setContentType("application/json");
response.getWriter().write(json);
%>