a little help about datatable...

HI guys,

i'm a new user of JSF and now i'm descovering great possibilities offered by datatable.

I read the article on

http://balusc.xs4all.nl/srv/dev-j2p-dat.html#RetrieveAndStoreData,

i've found it excellent but there is a point that isn't me clear...

I've to show in a datatable a mysql table

utenti(firstName,lastName,city)

which its relative values..

i've developed a User class with the tree values as private variables and getter and setter methods...

publicclass Userimplements Serializable{

privatestaticfinallong serialVersionUID = 1;

private String firstName;

private String lastName;

private String city;

public User( String firstName, String lastName,String city

){

this.firstName = firstName;

this.lastName = lastName;

this.city = city;

}

public String getFirstName(){

return firstName;

}

publicvoid setFirstName(String firstName){

this.firstName =firstName;

}

public String getLastName(){

return lastName;

}

publicvoid setLastName(String lastName){

this.lastName = lastName;

}

public String getCity(){

return city;

}

publicvoid setCity(String city){

this.city = city;

}

}

and this is UsersTable

package giu;

import java.sql.Connection;

import java.sql.PreparedStatement;

import java.sql.ResultSet;

import java.util.List;

import javax.naming.Context;

import javax.naming.InitialContext;

import javax.sql.DataSource;

publicclass UsersTable{

private List myTableList;

publicvoid loadMyTableList(){

try{

Context ctx =new InitialContext();

if (ctx ==null){

thrownew Exception("Boom - No Context");

}

Context envCtx = (Context) ctx.lookup("java:comp/env");

DataSource ds = (DataSource) ctx.lookup("java:comp/env/MysqlJNDI");//"java:comp/env/jdbc/nomedb"

if (ds !=null){

Connection conn = ds.getConnection();

if (conn !=null){

PreparedStatement pst =null;

String query="SELECT * FROM utenti ;";

pst = conn.prepareStatement(query);

ResultSet res=pst.executeQuery();

pst.close();

conn.close();

setMyTableList(res.getOutputAsList());

}

}

}catch (Exception e){

e.printStackTrace();

}

}

public List getMyTableList(){

loadMyTableList();// Reload after every request.

return myTableList;

}

// Setters -

publicvoid setMyTableList(List myTableList){

this.myTableList = myTableList;

}

}

i have an error because i can't use getOutpusAsList on ResultSet res...

can you help me?

Please,i'm a bit confused on datatable,i'm trying to understand something....

[5715 byte] By [giubata] at [2007-10-3 1:13:07]
# 1
Try to inverse these lines....pst = conn.prepareStatement(query);ResultSet res=pst.executeQuery();setMyTableList(res.getOutputAsList());pst.close();conn.close();
WilliamBecka at 2007-7-14 18:10:11 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 2
thanks...but nothing,the problem is(as i've seen from Java Api) that getOutputAsList isn't defined for a Resultset object...how can i convert the result of a query in something on which i can use getOutputAsList?thanks
giubata at 2007-7-14 18:10:11 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 3

// Do your Hibernate/JDBC thing, this is just a basic example.

YourLoadQuery yourLoadQuery = new YourLoadQuery(MyTable.class);

YourDAOInvoker.execute(yourLoadQuery);

setMyTableList(yourLoadQuery.getOutputAsList());

There stands "this is just a basic example". Write your own DAO class to get the table rows collected as wrapper objects in a List. This should look like:

List rows = new ArrayList();

while (result.next()) {

MyRow myRow = new MyRow(); // this is the wrapper class

row.setField1(result.getString("field1"));

row.setField2(result.getString("field2"));

row.setField3(result.getString("field3"));

rows.add(row);

}

return rows;

I use Hibernate by the way. YourLoadQuery(MyTable.class) gets and interpets the "layout" of each row using the mapping file mytable.hbm.xml and the getOutputAsList() processes the rows using the mapping file and the provided MyTable.class and returns a List of MyTable objects which represents the rows.

BalusCa at 2007-7-14 18:10:11 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...