[prev in list] [next in list] [prev in thread] [next in thread]
List: struts-user
Subject: Transfering data from a result set using reflection and metadata
From: Sivakumar Santharam <ambalam () gmail ! com>
Date: 2005-02-28 18:34:21
Message-ID: loom.20050228T193326-524 () post ! gmane ! org
[Download RAW message or body]
I am trying to use reflection and metadata to transfer data from a result set
into a bean. I just followed the instructions provided at
http://husted.com/struts/catalog.html. Eventhough the result set returns a
row, the values are not set in the bean after the BeanUtils.populate(bean,
properties) method is called. Are you guys able to seeing something that I am
missing? Here is the code sample:
-- bean --
public UserBean{
String firstName;
public void setFirstName(String string) {
firstName = string;
}
public String getFirstName() {
return firstName;
}
}
--- DAO ---
public myDAO{
public UserBean getUser(String userid){
UserBean user = new UserBean();
String SQL = "SELECT FIRST_NAME FROM USER WHERE USERID='abc'";
try{
conn = getConnection();
pstmt = conn.prepareStmt(SQL);
pstmt.setString(1,userid);
rs = pstmt.executeQuery();
populate(user, rs);
}catch(Exception e){}
}
public static void populate(Object bean,ResultSet resultSet) throws
SQLException {
// Build a list of relevant column properties from this resultSet
HashMap properties = new HashMap();
// Acquire resultSet MetaData
ResultSetMetaData metaData = resultSet.getMetaData();
int cols = metaData.getColumnCount();
// Scroll to next record and pump into hashmap
if (resultSet.next()) for (int i=1; i<=cols ; i++) {
properties.put(metaData.getColumnName(i),
resultSet.getString(i));
}
// Set the corresponding properties of our bean
try {
BeanUtils.populate(bean, properties);
} catch (Exception e) {
throw new SQLException("BeanUtils.populate threw " + e.toString());
}
}
}
---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org
[prev in list] [next in list] [prev in thread] [next in thread]
Configure |
About |
News |
Add a list |
Sponsored by KoreLogic