You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by Sivakumar Santharam <am...@gmail.com> on 2005/02/28 19:34:21 UTC

Transfering data from a result set using reflection and metadata

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