Board index » jbuilder » how display an anonymous querydataset with struts

how display an anonymous querydataset with struts


2003-10-23 01:45:31 AM
jbuilder11
Hi all,
I have a problem. The following is a snippet of code that retrieve e querydataset form a database.
I would try to display the data matrix resulting from query without know prior the name of columns or the number of them, to have a general approach to the task.
Database testDb = new Database();
QueryDataSet queryTable = new QueryDataSet();
private String queryString ="select * from book";
private String tableName="Not_Setted";
private Column[] columns = null;
private ArrayList[] data ;
public void executeQuery(String queryString) {
this.queryTable.setQuery(new com.borland.dx.sql.dataset.QueryDescriptor(this.testDb, this.queryString, null, true, Load.ALL));
this.queryTable.executeQuery();
this.setTableName(queryTable.getTableName());
this.setColumns(this.queryTable.getColumns());
//put data int ArrayList[]
while (queryTable.next()) {
for (int i = 0; i < colonne.length; i++) {
data[i].add(queryTable.format(i));
}
}
}
Now the problem is how to iterate into the above arraylist[]
with struts tag iterate or something like that.
I don't understand how to do this.
What I found over the internet is very confusing and I am new to struts.
Is there anyone that can send me an help?
Thank
Alex
 
 

Re:how display an anonymous querydataset with struts

alessandro bonvicini wrote:
Quote

Hi all,


Now the problem is how to iterate into the above arraylist[]
with struts tag iterate or something like that.
I don't understand how to do this.
What I found over the internet is very confusing and I am new to struts.
Is there anyone that can send me an help?
Thank
Alex
Try something like this:
package testcoltypes;
import java.awt.*;
import java.awt.event.*;
import com.borland.dx.dataset.*;
import com.borland.dx.sql.dataset.*;
public class DM implements DataModule {
static private DM myDM;
private Database db = new Database();
private QueryDataSet qry2 = new QueryDataSet();
public static DM getDataModule() {
if (myDM == null) {
myDM = new DM();
}
return myDM;
}
public DM() {
try {
jbInit();
}
catch(Exception e) {
e.printStackTrace();
}
}
private void jbInit() throws Exception {
qry2.setQuery(new com.borland.dx.sql.dataset.QueryDescriptor(database1,
"SELECT MENUS.IDMENU,MENUS.MENUNAME,MENUS.MENUTEXT FROM MENUS", null, true,
Load.ALL));
users.setQuery(new com.borland.dx.sql.dataset.QueryDescriptor(database1,
"SELECT
USERS.IDUSER,USERS.IDROLE,USERS.USERID,USERS.PASSWRD,USERS.USER_NAME,USERS.IDDISTRICT,USERS.ID"
+
"EMPLOYEE,USERS.IDCONTRACTOR,USERS.HOSPITALID FROM USERS", null, true,
Load.ALL));
db.setConnection(new
com.borland.dx.sql.dataset.ConnectionDescriptor("jdbc:firebirdsql:localhost/3050:/opt/example/employees.gdb",
"sysdba", "masterkey", false, "org.firebirdsql.jdbc.FBDriver"));
}
public Database getDatabase1() {
return db;
}
public java.util.Vector getColumns(){
if(qry2.open()==false) {
qry2.open();
}
java.util.Vector vCols= new java.util.Vector();
for (int i=0; i<qry2.getColumnCount(); i++){
vCols.addElement(qry2.getColumn(i).getColumnName());
}
return vCols;
}
public java.util.Vector getRows() {
int colCount=qry2.getColumnCount();
int addTr=0;
java.util.Vector vRows= new java.util.Vector();
DataRow dr= new DataRow(menus);
String[] s=dr.getColumnNames(colCount);
int iRow=1;
for (int i=0; i<qry2.getRowCount(); ++i) {
for (int y=0; y <s.length; ++y) {
Variant v= new Variant();
vRows.addElement("<td style='vertical-align: top;'>");
qry2.getVariant(s[y],i,v);
if(v.getType()==Variant.INT) {
vRows.addElement(""+v.getInt());
}
else if(v.getType()==Variant.FLOAT) {
vRows.addElement(""+v.getFloat());
}
else if (v.getType()==Variant.DOUBLE) {
vRows.addElement(""+v.getDouble());
}
else if(v.getType()==Variant.DATE) {
vRows.addElement(""+v.getDate().toString());
}
else if(v.getType()==Variant.LONG) {
vRows.addElement(""+v.getLong());
}
else if(v.getType()==Variant.STRING) {
vRows.addElement(v.getString());
}
vRows.add("</td>");
if(addTr==colCount-1) {
addTr=0; //start over
vRows.addElement("</tr>");
vRows.addElement("<tr>");
}
else {
++addTr;
}
}
}
return vRows;
}
public com.borland.dx.sql.dataset.QueryDataSet getQry2() {
return qry2;
}
}
A Test.jsp to return data sample
<head>
<title>
Test
</title>
</head>
<body>
<h1>
JBuilder Generated JSP
</h1>
<% Vector vCols=dm.getColumns();
Vector vRows=dm.getRows();
Iterator ic= vCols.iterator();
Iterator ir=vRows.iterator();
%>
<table cellpadding="2" cellspacing="2" border="1"
style="text-align: left; width: 90%; margin-left: auto; margin-right:
auto;">
<tbody>
<tr>
<% while (ic.hasNext()) { %>
<td
style="vertical-align: top; background-color: rgb(102, 102, 102); color:
rgb(255, 255, 255); text-decoration: underline;"><font
size="+1"><%=ic.next()%><br>
</font></td>
<% }%></tr>
<tr>
<!-- now fill in the rows -->
<% while(ir.hasNext()) { %>
<%=ir.next()%>
<% } %>
</tr>
</tbody>
</table>
</body>
</html>
 

Re:how display an anonymous querydataset with struts

Hi all,
I am extremely grateful for your support and I really appreciate it.
But, I would like to know if there is a solution using standard struts tags, I am not sure that embedding java sources in pages
is a good solution, I would like to use a tag library.
What do you think about this? what docs can I study to solve this question?
Thak in advance
Alex
pNichols < XXXX@XXXXX.COM >wrote:
Quote
alessandro bonvicini wrote:

>
>Hi all,
>
>
>Now the problem is how to iterate into the above arraylist[]
>with struts tag iterate or something like that.
>I don't understand how to do this.
>What I found over the internet is very confusing and I am new to struts.
>Is there anyone that can send me an help?
>Thank
>Alex


Try something like this:

package testcoltypes;

import java.awt.*;
import java.awt.event.*;
import com.borland.dx.dataset.*;
import com.borland.dx.sql.dataset.*;

public class DM implements DataModule {
static private DM myDM;
private Database db = new Database();
private QueryDataSet qry2 = new QueryDataSet();
public static DM getDataModule() {
if (myDM == null) {
myDM = new DM();
}
return myDM;
}

public DM() {
try {
jbInit();
}
catch(Exception e) {
e.printStackTrace();
}
}
private void jbInit() throws Exception {
qry2.setQuery(new com.borland.dx.sql.dataset.QueryDescriptor(database1,
"SELECT MENUS.IDMENU,MENUS.MENUNAME,MENUS.MENUTEXT FROM MENUS", null, true,
Load.ALL));
users.setQuery(new com.borland.dx.sql.dataset.QueryDescriptor(database1,
"SELECT
USERS.IDUSER,USERS.IDROLE,USERS.USERID,USERS.PASSWRD,USERS.USER_NAME,USERS.IDDISTRICT,USERS.ID"
+
"EMPLOYEE,USERS.IDCONTRACTOR,USERS.HOSPITALID FROM USERS", null, true,
Load.ALL));
db.setConnection(new
com.borland.dx.sql.dataset.ConnectionDescriptor("jdbc:firebirdsql:localhost/3050:/opt/example/employees.gdb",
"sysdba", "masterkey", false, "org.firebirdsql.jdbc.FBDriver"));
}
public Database getDatabase1() {
return db;
}

public java.util.Vector getColumns(){
if(qry2.open()==false) {
qry2.open();
}
java.util.Vector vCols= new java.util.Vector();

for (int i=0; i<qry2.getColumnCount(); i++){
vCols.addElement(qry2.getColumn(i).getColumnName());
}
return vCols;
}

public java.util.Vector getRows() {
int colCount=qry2.getColumnCount();
int addTr=0;
java.util.Vector vRows= new java.util.Vector();
DataRow dr= new DataRow(menus);
String[] s=dr.getColumnNames(colCount);
int iRow=1;



for (int i=0; i<qry2.getRowCount(); ++i) {
for (int y=0; y <s.length; ++y) {
Variant v= new Variant();
vRows.addElement("<td style='vertical-align: top;'>");
qry2.getVariant(s[y],i,v);
if(v.getType()==Variant.INT) {
vRows.addElement(""+v.getInt());
}
else if(v.getType()==Variant.FLOAT) {
vRows.addElement(""+v.getFloat());
}
else if (v.getType()==Variant.DOUBLE) {
vRows.addElement(""+v.getDouble());
}
else if(v.getType()==Variant.DATE) {
vRows.addElement(""+v.getDate().toString());
}
else if(v.getType()==Variant.LONG) {
vRows.addElement(""+v.getLong());
}
else if(v.getType()==Variant.STRING) {
vRows.addElement(v.getString());
}
vRows.add("</td>");
if(addTr==colCount-1) {
addTr=0; //start over
vRows.addElement("</tr>");
vRows.addElement("<tr>");
}
else {
++addTr;
}


}
}
return vRows;
}
public com.borland.dx.sql.dataset.QueryDataSet getQry2() {
return qry2;
}

}


A Test.jsp to return data sample

<head>
<title>
Test
</title>
</head>
<body>
<h1>
JBuilder Generated JSP
</h1>
<% Vector vCols=dm.getColumns();
Vector vRows=dm.getRows();
Iterator ic= vCols.iterator();
Iterator ir=vRows.iterator();
%>
<table cellpadding="2" cellspacing="2" border="1"
style="text-align: left; width: 90%; margin-left: auto; margin-right:
auto;">
<tbody>
<tr>
<% while (ic.hasNext()) { %>

<td
style="vertical-align: top; background-color: rgb(102, 102, 102); color:
rgb(255, 255, 255); text-decoration: underline;"><font
size="+1"><%=ic.next()%><br>
</font></td>
<% }%></tr>
<tr>
<!-- now fill in the rows -->

<% while(ir.hasNext()) { %>

<%=ir.next()%>

<% } %>

</tr>
</tbody>
</table>
</body>
</html>

 

{smallsort}

Re:how display an anonymous querydataset with struts

alessandro bonvicini wrote:
Quote

Hi all,
I am extremely grateful for your support and I really appreciate it.

But, I would like to know if there is a solution using standard struts
tags, I am not sure that embedding java sources in pages is a good
solution, I would like to use a tag library.

What do you think about this? what docs can I study to solve this
question? Thak in advance
Alex


There is no taglib that I am aware of, that will allow you to use the
QueryDataSets from Borland, except thos ein the Enterprise version of
JBuuilder where you could use the Internet Express Tag libs. However, it
should be relatively easy to take the code sample I gave you and create a
taglib from it.
 

Re:how display an anonymous querydataset with struts

Hi
There is an excellent solution to this problem using jstl tags - see below
where RESULT is a javax.servlet.jsp.jstl.sql.Result object.
There is some good example code using this in O'Reilly book JavaServer
Pages.
<table>
<tr>
<c:forEach items="${RESULT.columnNames}" var="colName">
<th><c:out value="${colName}" /></th>
</c:forEach>
</tr>
<c:forEach items="${RESULT.rowsByIndex}" var="row">
<tr>
<c:forEach items="${row}" var="column">
<td><c:out value="${column}" /></td>
</c:forEach>
</tr>
</c:forEach>
</table>
db
"alessandro bonvicini" < XXXX@XXXXX.COM >wrote in message
Quote

Hi all,
I have a problem. The following is a snippet of code that retrieve e
querydataset form a database.
I would try to display the data matrix resulting from query without know
prior the name of columns or the number of them, to have a general approach
to the task.
Quote

Database testDb = new Database();
QueryDataSet queryTable = new QueryDataSet();
private String queryString ="select * from book";
private String tableName="Not_Setted";
private Column[] columns = null;
private ArrayList[] data ;



public void executeQuery(String queryString) {

this.queryTable.setQuery(new
com.borland.dx.sql.dataset.QueryDescriptor(this.testDb, this.queryString,
null, true, Load.ALL));
Quote

this.queryTable.executeQuery();

this.setTableName(queryTable.getTableName());

this.setColumns(this.queryTable.getColumns());


//put data int ArrayList[]

while (queryTable.next()) {

for (int i = 0; i < colonne.length; i++) {
data[i].add(queryTable.format(i));
}

}
}

Now the problem is how to iterate into the above arraylist[]
with struts tag iterate or something like that.
I don't understand how to do this.
What I found over the internet is very confusing and I am new to struts.
Is there anyone that can send me an help?
Thank
Alex