Hello,
When we are retrieving data(dynamically) using java servlets the resultset holds query result.
ResultSet rs = st.executeQuery(query);
Can any one explain How to put ResulSet data into jsgrid
Please provide a description or no one will be able to help you.
jsgrid works on array of items.
Probably you should create your array of object (typically an ArrayList of javabeans), populate it looping your ResultSet, convert it into a json string (using a lib like Gson) and send back the string in your front-end.
@maurorulli maurorulli
Thank you for your replay,
This is my servlet page, Here i converted Resultset data into json array format and i sended it to Jsp page
Servlet code:
public class Reportgen extends HttpServlet{
public void service(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
String s1 = request.getParameter("t1");
String s2 = request.getParameter("t2");
try {
JsonObject jsonResponse = new JsonObject();
JsonArray data = new JsonArray();
PrintWriter out1 = response.getWriter();
Class.forName("com.informix.jdbc.IfxDriver");
Connection con = DriverManager
.getConnection(
"DB_URL", "Username", Password");
Statement st = con.createStatement();
String query = "select * from xyz_tbl where xyz_generated_date between '"+s1+"' and '"+s2+"'";
System.out.println("MY QUERY=" + query);
ResultSet rs = st.executeQuery(query);
while (rs.next()) {
JsonArray row = new JsonArray();
row.add(new JsonPrimitive(rs.getString("xyz_patient_id")));
row.add(new JsonPrimitive(rs.getString("xyz_patient_name")));
row.add(new JsonPrimitive(rs.getString("xyz_gender_id")));
row.add(new JsonPrimitive(rs.getString("xyz_mobile")));
data.add(row);
}
jsonResponse.add("ResponseData", data);
response.setContentType("application/json");
response.setCharacterEncoding("UTF-8");
request.setAttribute("Json", jsonResponse);
request.getRequestDispatcher("/Repdisplay.jsp").forward(request, response);
response.getWriter().print(jsonResponse.toString());
} catch (Exception e2) {
e2.printStackTrace();
}
finally {
out.close();
}
}
}
Jsp code:
<% out.println(request.getAttribute("Json")); %>
in jsp page the data is printing like this
["P1600003","abc","100","1111111111"],["TP16000002","xyz","1001","222222222"],["P6000003","pqr","101","4444444444"],["TP16000004","stu","1001","3333333333"]
please help me how to print this data into js grid, what are the steps i have to approach.
Two points you have to focus to:
You got an array of array of primitives. You should get an array of objects. Try to define the var row as JsonObject instead of JsonArray.
In JSP assign your output to a JavaScrit var that you'll use in JSgrid:
<script>
var db = <% out.println(request.getAttribute("Json")); %>;
</script>
OR, if your environment supports EL:
<script>
var db = ${requestScope.Json};
</script>