2D array using table tags
1 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   Dave_Teesley
Posted On:   Thursday, March 15, 2007 11:23 AM

Hello, I am trying to write a JavaBean that connects to a database (my database is called 'test') and executes "Select" type SQL statements (from a string called 'sql') and then stores the results in a 2D array. I would also like the array to be stored in such a way that it contains HTML table tags so that when I call the function from another page, the result will be a table that represents the information contained in the 2D array. I have managed to connect to the database ok, but am having lots of problems with constructing the tabular 2D array. Is anyone able to help, or preferrably show some examples of what I need to do? Many Thanks!    More>>

Hello,

I am trying to write a JavaBean that connects to a database (my database is called 'test') and executes "Select" type SQL statements (from a string called 'sql') and then stores the results in a 2D array.



I would also like the array to be stored in such a way that it contains HTML table tags so that when I call the function from another page, the result will be a table that represents the information contained in the 2D array.



I have managed to connect to the database ok, but am having lots of problems with constructing the tabular 2D array. Is anyone able to help, or preferrably show some examples of what I need to do?



Many Thanks!

   <<Less

Re: 2D array using table tags

Posted By:   Robert_Lybarger  
Posted On:   Thursday, March 15, 2007 10:44 PM

I'd probably try something along this line: create another java bean that you pass the resultset object into and that you can use to get formatted html table data elements back out of. Something along this line comes to mind:

import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.Vector;

public class ResultSetHTMLTableData {

int columnCount = 0;
int rowCount = 0;
Vector columnNames;
Vector> rowValues;

public ResultSetHTMLTableData() {
columnNames = new Vector();
rowValues = new Vector>();
}

public void setResultSet(ResultSet rs) throws SQLException {
ResultSetMetaData meta = rs.getMetaData();
columnCount = meta.getColumnCount();
for (int i=1 ; i<=columnCount ; i++) {
columnNames.add(meta.getColumnName(i));
}
while (rs.next()) {
rowCount++;
Vector row = new Vector();
for (int i=1 ; i<=columnCount ; i++) {
row.add(rs.getObject(i));
}
rowValues.add(row);
}
}

public int getRowCount() {
return rowCount;
}

public int getColumnCount() {
return columnCount;
}

public String getTableHeaderHTML() {
if (getColumnCount() <= 0) {
return "";
}
StringBuffer buf = new StringBuffer();
buf.append("");
for (int i=0 ; i buf.append("" + columnNames.get(i) + "");
}
buf.append("");
return buf.toString();
}

public String getTableRowHTML(int rowNumber) {
if (rowNumber < 0 || rowNumber >= getRowCount()) {
return "";
}
if (getColumnCount() <= 0) {
return "";
}
StringBuffer buf = new StringBuffer();
buf.append("");
Vector rowData = rowValues.get(rowNumber);
for (int i=0 ; i buf.append("" + rowData.get(i).toString() + "");
}
buf.append("");
return buf.toString();
}
}

You'll probably have to modify to suit your own needs, but might give you a direction to think in. Note/Warning: result set (and related) index columns at 1, not 0.