Parsing Vector into a Table

Hi all,

Firstly, an advanced thankyou for anyone willing to help.

I'm writing a JSP to display a table from a database, but the bean used passes back a vector (consisiting of more vectors) to the JSP and I have no idea how to parse through said vectors so I can output it to a table.

Here's my code so far:

The (segment of) bean:

public Vector<Vector><Object>> getAllStock()

throws SQLException

{

Vector<Object> row =null;

Vector<Vector><Object>> rows =

new Vector<Vector><Object>>();

connectToDb();

try

{

results = statement.executeQuery(

"SELECT * FROM Stock");

while (results.next())

{

row =new Vector<Object>();

row.add(results.getString(1));

row.add(results.getString(2));

row.add(results.getInt(3));

row.add(results.getInt(4));

row.add(results.getFloat(5));

rows.add(row);

}

}

catch (SQLException sqlEx)

{

//Error message

}

disconnectFromDb();

return rows;

}

And then the JSP:

<HTML>

<HEAD>

<TITLE>The tableName Database Table </TITLE>

</HEAD>

<BODY>

<H1>Displaying full stock list </H1>

<%

Vector resultset = stockacc.getAllStock();

%>

<TABLE BORDER="1">

<TR>

<TH>Stock Code</TH>

<TH>Stock Desc</TH>

<TH>Current Level</TH>

<TH>Reorder Level</TH>

<TH>Price</TH>

</TR>

<%while(resultset.next()){ %>

<TR>

<TD> <%= resultset.getString(1) %></td>

<TD> <%= resultset.getString(2) %></TD>

<TD> <%= resultset.getString(3) %></TD>

<TD> <%= resultset.getString(4) %></TD>

<TD> <%= resultset.getString(5) %></TD>

</TR>

<%} %>

</TABLE>

</BODY>

</HTML>

All help and advice greatly appreciated!

[2877 byte] By [pedanta] at [2007-11-27 2:10:14]
# 1

See http://java.sun.com/j2se/1.5.0/docs/api/ for the API. You can look up Vector on the bottom left corner and see what functions you can call from it.

The procedure would be this, though:

<% Vector table = getVectorOfRows...%>

<!-- Set up header for table -->

<% //get an iterator for rows to make it easy to walk through

Iterator rows = table.iterator();

while (rows.hasNext()) {

//get the list of all columns (one row), from the iterator. Make sure it is a Vector

Vector currentRow = (Vector)rows.next();

//get an iterator for the columns

Iterator columns = currentRow.iterator();

%>

<!-- Build a table using <% columns.next(); %>, or <% currentRow.get(index) %> to get value for each column -->

stevejlukea at 2007-7-12 2:01:49 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 2

Vector resultset = stockacc.getAllStock();

while (results.next())

Your variable resultset is a Vector not a ResultSet.

You should use a for loop and retrieve the elements with using elementAt

http://java.sun.com/j2se/1.4.2/docs/api/java/util/Vector.html#elementAt(int)

I don't recommend you using Vector of Vector.

You should create a StockDetail class with getters and setters.

You may store those StockDetails in a Vector (or someone would suggest you to use an ArrayList).

for result set has next

getString, getInt to get the data

create a StockDetail using setters or constructor

Add this StockDetail object to a Vector or ArrayList

end for

// in your jsp

for vector size (or ArrayList)

get the StockDetail element

using getters to get individual data

end for

rym82a at 2007-7-12 2:01:50 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 3

well so gud to hear that you are following the right approach...

if i were you i could have used a vector of Maps(Vector<Map><String,Object>>) or Vector of a custom built bean Vector<ResultSetBean> which could be very easy to resolve using a custom tags like JSTL,struts(logic & bean) tags..

RahulSharnaa at 2007-7-12 2:01:50 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 4

Many thanks for all the timely replies!

I'm trying to implement your solution (stevejluke), and here's what I've got so far:

<HTML>

<%@ page import="java.util.*" %>

<%@ page import="java.net.*" %>

<jsp:useBean id="stockacc" class="stockBeans.StockAccess" />

<HEAD>

<TITLE>The tableName Database Table </TITLE>

</HEAD>

<BODY>

<H1>The tableName Database Table </H1>

<%

Vector table = (Vector)stockacc.getAllStock();

Iterator rows = table.iterator();

Vector currentRow = (Vector)rows.next();

Iterator columns = currentRow.iterator();

%>

<%= columns.next() %>

<TABLE BORDER="1">

<TR>

<TH>Stock Code</TH>

<TH>Stock Desc</TH>

<TH>Current Level</TH>

<TH>Reorder Level</TH>

<TH>Price</TH>

</TR>

<TR>

</TR>

</TABLE>

</BODY>

</HTML>

Bearing in mind I'm just trying to see if anythings making it through, hence I'm just using <%= columns.next() %> for now.

However, I don't think the vector (of vectors; not too confusing!) is being created when I do:

Vector table = (Vector)stockacc.getAllStock();

As I'm getting a NoSuchElement exception being thrown.

Can anyone advise?

Thanks again.

pedanta at 2007-7-12 2:01:50 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 5
Vector of Vectors in here.Vector < Vector <Object>> rows =new Vector < Vector <Object>>();
rym82a at 2007-7-12 2:01:50 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 6
- deleted -Message was edited by: rym82
rym82a at 2007-7-12 2:01:50 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 7

So it should read:

<%

Vector<Vector<Object>> table = new Vector<Vector><Object>>();

table =stockacc.getAllStock();

Iterator rows = table.iterator();

Vector currentRow = (Vector)rows.next();

Iterator columns = currentRow.iterator();

%>

<%= currentRow.get(1) %>

?

Many thanks!

pedanta at 2007-7-12 2:01:50 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 8
Anyone any ideas? Still stuck on trying to get it working :(
pedanta at 2007-7-12 2:01:50 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 9

One last bump before my deadline passes.

It's definitely a problem with my first line, that is,

Vector<Vector><Object>> results = (Vector<Vector><Object>>)stockacc.getAllStock();

As when doing a simple <%= results.isEmpty() %> , it always comes back true.

I've tried several variations but none seem to work (that is, I always have an empty results vector). Am I doing something monumentally stupid?

pedanta at 2007-7-12 2:01:50 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 10

public Vector<Vector><Object>> getAllStock()

throws SQLException

{

Vector<Object> row = null;

Vector<Vector><Object>> rows =

new Vector<Vector><Object>>();

connectToDb();

try

{

results = statement.executeQuery(

"SELECT * FROM Stock");

while (results.next())

{

row = new Vector<Object>();

row.add(results.getString(1));

row.add(results.getString(2));

row.add(results.getInt(3));

row.add(results.getInt(4));

row.add(results.getFloat(5));

rows.add(row);

}

}

catch (SQLException sqlEx)

{

throw new SQLException("Error retrieving stock list!!");

}

disconnectFromDb();

return rows;

}

pedanta at 2007-7-12 2:01:50 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...