wood burning stoves 2.0*
The moose likes Servlets and the fly likes using and passing a multidimensional array to jsp Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "using and passing a multidimensional array to jsp" Watch "using and passing a multidimensional array to jsp" New topic
Author

using and passing a multidimensional array to jsp

Jeff Sunder
Ranch Hand

Joined: Jun 26, 2001
Posts: 47
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() {
}
}
Jeff Sunder
Ranch Hand

Joined: Jun 26, 2001
Posts: 47
I think the problem is comng from the while loop, when I'm looping through the resultset. What I am trying to do is capture all of the data in a 2 dimension array. One dimension the rows the other dimension the columns. When I comment out the line of code where the array is getting assinged, it works. But I have tried to redo the while statement, but still I am not able to get it to work. Does any one have any ideas?
Thanks in advance.
Manjunath Reddy
Ranch Hand

Joined: Jul 26, 2001
Posts: 60
The logic seems alright to me...just check if the no of rows returned by query "select count(*) from projects" matches with the query "select * from project_details". It seems that u r assuming that the rows of the project table matches with that of the project_details...but they might not be so..
cheers,
mpr
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: using and passing a multidimensional array to jsp
 
Similar Threads
help with jsp
Multidimensional array
Multidimensional arrays
Multidimensional ArrayList
IllegalStateException error