Hello everyone,
I am populating a dropdown list in my webapp, from a MySQL database table. There are around 160,000 records in the table. When I start my webapp and when populating the content of the drop-down, it takes incredible amount of time, and eventually the browser crashes. I am not sure if this is normal for 160k records, or I maybe doing something incorrect?
HEre's what I have done. **Note: If I choose to load a small amount of records, say 100, then there's no problem at all.
My servlet:
protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setAttribute("ClientInfo", (ArrayList<ClientInfo>)getClientResult());
request.getRequestDispatcher("/myJSP.jsp").forward(request, response);
}
public ArrayList<ClientInfo> getClientResult() {
Connection conn = null;
Statement st = null;
ResultSet rs = null;
ArrayList<ClientInfo> ClientInfoList = new ArrayList<ClientInfo>();
try {
conn = cltUtils.getMySQLConnection();
st = conn.createStatement();
rs = st.executeQuery("SELECT client_id, client_name FROM client_info where client_title != ''");
while (rs.next()) {
String client_id = rs.getString("client_id").trim();
String client_name = rs.getString("client_name");
ClientInfo ci = new ClientInfo(client_id, client_name);
ClientInfoList.add(ci);
}
} catch (Exception ex) {
ex.printStackTrace();
} finally {
try { if (rs != null) rs.close(); } catch (SQLException e) { e.printStackTrace(); }
try { if (st != null) st.close(); } catch (SQLException e) { e.printStackTrace(); }
try { if (conn != null) conn.close(); } catch (SQLException e) { e.printStackTrace(); }
}
return ClientInfoList;
}
Here's my JSP portion, where it reads the data and populates it.
<% ArrayList<ClientInfo> cil=(ArrayList<ClientInfo>)request.getAttribute("ClientInfo"); %>
<select id="client" class="form-control">
<%
for (int i=0; i < cil.size(); i++) {
ClientInfo ci = (ClientInfo)cil.get(i);
%>
<option value="<%=bi.getClientId() %>"><%=ci.getClientName() %></option>
<%} %>
</select>
Is this an optimal way of doing this? or there's a better way to improve the performance?
Appreciate input/feedbacks. Thanks in advance.