I have an application where I have a servlet connect to a db and perform sql commands, put the sql results in a multidimensional array, and then pass the multidimensional array to a jsp page to display. However, when I try to do this, something is going wrong, but I'm not sure what it is because I'm not getting any kind of error message. I have pinpointed to where the error is occuring and its when I try to set the values of the array, at this line:
resultsArray[v][s] = rs2.getString(s+1);
Here is the servlet:
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;
import java.sql.*;
public class ProjectDetailProcess3 extends HttpServlet {
private static final String CONTENT_TYPE = "text/html";
ConnectionPool connPool = null;
public void init(ServletConfig config) throws ServletException {
super.init(config);
String jdbcDriver = "sun.jdbc.odbc.JdbcOdbcDriver";
String dbURL = "jdbc
dbc:ProjectsDb";
try {
connPool = new ConnectionPool(jdbcDriver, dbURL, "projects", "projects");
connPool.setInitialConnections(5);
connPool.setIncrementalConnections(5);
connPool.setMaxConnections(15);
connPool.setTestTable("Projects");
connPool.createPool();
}catch (Exception e) {
System.out.println("Error " +e);
}
}
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String securityAccess = request.getParameter("pmsn");
int projectId = Integer.parseInt(request.getParameter("project"));
int projectDetailId = Integer.parseInt(request.getParameter("projDetail"));
Connection dbConn = null;
try{
dbConn = connPool.getConnection();
Statement stmt = dbConn.createStatement();
int numberOfRows = 0;
String getRowCount = "Select count(*) from projects";
ResultSet rs = stmt.executeQuery(getRowCount);
while (rs.next()) {
numberOfRows = Integer.parseInt(rs.getString(1));
}
rs.close();
String getDetails1 = "Select * from projects_detail";
ResultSet rs2 = stmt.executeQuery(getDetails1);
int numberOfColumns = rs2.getMetaData().getColumnCount();
//out.println("#"+numberOfColumns);
String[][] resultsArray = new String[numberOfRows][numberOfColumns];
while (rs2.next()) {
for (int v=0; v<=numberOfRows-1;v++) {
for (int s=0;s<=numberOfColumns-1;s++) {
resultsArray[v][s] = rs2.getString(s+1);
}
}
}
HttpSession session = request.getSession();
request.setAttribute("array", resultsArray);
try{
RequestDispatcher d = request.getRequestDispatcher("/jsp/Test.jsp");
d.include(request, response);
}catch (ServletException sx) {
}
}catch (Exception e) {
//out.println(e.getMessage());
}finally{
connPool.returnConnection(dbConn);
}
}
public void destroy() {
}
}