• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

using and passing a multidimensional array to jsp

 
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 47
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
There's a way to do it better - find it. -Edison. A better tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic