我有以下代码从数据库中检索数据:
List<LocationBean> Locations = new ArrayList<LocationBean>();
/*
* The following logic iterates through the database entries
* */
for(int i = 1; i<=10; ++i){
String query ="SELECT * FROM Energyinfo.Info WHERE id_number = " +i;
ResultSet rs = stmt.executeQuery(query);
while(rs.next()){
int id = rs.getInt("id_number");
int kw = rs.getInt("kilowatts_used");
double lat = rs.getDouble("Latitude");
double lon = rs.getDouble("Longitude");
LocationBean temp = new LocationBean(id, kw, lat, lon);
Locations.add(temp);
}}
String jsonData = new Gson().toJson(Locations);
System.out.println(jsonData);
response.setContentType("application/json");
response.setCharacterEncoding("UTF-8");
response.getWriter().write(jsonData);
我不确定这是否已正确设置为在前端检索。但是,我想在JSP中使用AJAX来检索此JSON对象以在屏幕上显示。我希望这是动态的,因为数据库数据库中的数据可能会发生变化。