我是新手,所以我没有多少经验。 我的问题很简单,我想在我的表中用ajax加载数据 - 但我不知道我该怎么做。 我现在有这个代码(ajaxServlet):
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
int row=0;
JSONArray back = new JSONArray();
JSONObject obj = new JSONObject();
try{
Class.forName("com.jdbc.mysql.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql//localhost/expenses","***","****");
PreparedStatement stmt = con.prepareStatement("Select * from expenses");
ResultSet rs = stmt.executeQuery();
while(rs.next())
{
JSONArray array = new JSONArray();
array.add(rs.getString("UID"));
array.add(rs.getString("Last Name");
array.add(rs.getString("First Name"));
back.add(array);
row++;
}
PrintWriter out = response.getWriter();
response.setContentType("application/json");
obj.put("size", row); // Here is located the problem i think...what must i return from here?
obj.put("example", back);
out.println(obj);
}catch(Exception e)
{
e.printStackTrace();
}
}
在HTML方面,代码与documentaion
中的代码完全相同只有ajaxUrl
看起来像这样:
ajaxUrl : "ajaxServlet?page={page}&size={size}&{sortList:col}",
所以,请你告诉我,我必须改变或做什么,这是有效的。
非常感谢