我的一个例子:
/**
*类反射的一个简单使用的例子
* @param sql String
* @param className String,相对应得JavaBean/FormBean类的名字
* @return ArrayList,以类className为一条记录的结果集,完成ResultSet对象向ArrayList对象为集合的className对象的转化
*/
public ArrayList Select(String sql, String className)
{
ArrayList paraList = new ArrayList();
PreparedStatement stmt = null;
ResultSet rs = null;
Connection dbConnection = null; try
{
getStaticArray();
dbConnection = DAOUtils.getDBConnection(JNDINames.CSSS_DATASOURCE, this);
stmt = dbConnection.prepareStatement(sql);
rs = stmt.executeQuery();
String recordValue = "";
Object c1 = null;
paraList = new ArrayList();
ResultSetMetaData rsmd = rs.getMetaData();
int columnCount = rsmd.getColumnCount();
while (rs.next())
{
c1 = Class.forName(className).newInstance();
for (int i = 1; i <= columnCount; i++)
{
if (rs.getString(rsmd.getColumnName(i)) != null)
{
recordValue = rs.getString(rsmd.getColumnName(i));
}
else
{
recordValue = "";
}
Method m = c1.getClass().getMethod(getSetMethodName(rsmd.
getColumnName(i)), new Class[]
{recordValue.getClass()});
m.invoke(c1, new Object[]
{recordValue});
}
paraList.add(c1);
}
}
catch (SQLException ex)
{}
catch (ClassNotFoundException e)
{}
catch (NoSuchMethodException e)
{}
catch (InvocationTargetException e)
{}
catch (IllegalAccessException e)
{}
catch (InstantiationException e)
{}
finally
{
DAOUtils.closeResultSet(rs, this);
DAOUtils.closeStatement(stmt, this);
DAOUtils.closeConnection(dbConnection, this);
}
return paraList;
}
/**
*类反射的一个简单使用的例子
* @param sql String
* @param className String,相对应得JavaBean/FormBean类的名字
* @return ArrayList,以类className为一条记录的结果集,完成ResultSet对象向ArrayList对象为集合的className对象的转化
*/
public ArrayList Select(String sql, String className)
{
ArrayList paraList = new ArrayList();
PreparedStatement stmt = null;
ResultSet rs = null;
Connection dbConnection = null; try
{
getStaticArray();
dbConnection = DAOUtils.getDBConnection(JNDINames.CSSS_DATASOURCE, this);
stmt = dbConnection.prepareStatement(sql);
rs = stmt.executeQuery();
String recordValue = "";
Object c1 = null;
paraList = new ArrayList();
ResultSetMetaData rsmd = rs.getMetaData();
int columnCount = rsmd.getColumnCount();
while (rs.next())
{
c1 = Class.forName(className).newInstance();
for (int i = 1; i <= columnCount; i++)
{
if (rs.getString(rsmd.getColumnName(i)) != null)
{
recordValue = rs.getString(rsmd.getColumnName(i));
}
else
{
recordValue = "";
}
Method m = c1.getClass().getMethod(getSetMethodName(rsmd.
getColumnName(i)), new Class[]
{recordValue.getClass()});
m.invoke(c1, new Object[]
{recordValue});
}
paraList.add(c1);
}
}
catch (SQLException ex)
{}
catch (ClassNotFoundException e)
{}
catch (NoSuchMethodException e)
{}
catch (InvocationTargetException e)
{}
catch (IllegalAccessException e)
{}
catch (InstantiationException e)
{}
finally
{
DAOUtils.closeResultSet(rs, this);
DAOUtils.closeStatement(stmt, this);
DAOUtils.closeConnection(dbConnection, this);
}
return paraList;
}
Class c = yourclass.getClass();2.从Class信息中取出定义号的构造函数
Constructor[] cons = c.getConstructors();3.调用某个构造函数得到对象
Object o = cons[i].newInstance(Object[] initargs) ;4.强转