Java 如何将结果集复制到对象中?
我使用以下命令将检索到的值添加到类中。所有值都将添加到类的属性中,但我使用compision(类中有一个class对象),并且它不会在输出上显示任何内容Java 如何将结果集复制到对象中?,java,database,javabeans,resultset,Java,Database,Javabeans,Resultset,我使用以下命令将检索到的值添加到类中。所有值都将添加到类的属性中,但我使用compision(类中有一个class对象),并且它不会在输出上显示任何内容 class employee { .... private Address address = new Address(); ..... } ... Employee emp = new Employee(); try { ps = con.prepareStatement("select * fr
class employee
{
....
private Address address = new Address();
.....
}
...
Employee emp = new Employee();
try {
ps = con.prepareStatement("select * from employee,address "
+ "WHERE employee.username = ? AND "
+ "employee.ADD_ID = address.ID");
ps.setString(1, username);
ResultSet r = ps.executeQuery();
if (r.next()) {
BeanProcessor bp = new BeanProcessor();
emp = bp.toBean(r,Employee.class);
System.out.println("blockkkk:"+emp.getAddress().getBlock());
//output of above line is blockkkk:null
}
con.close();
ps.close();
} catch (SQLException e) {
System.err.println(e.getMessage());
}
return emp;
地址类别如下:
public class Address {
.....
private String block;
....
public String getBlock() {
return block;
}
public void setBlock(String block) {
this.block = block;
}
....
}
BeanProcessor.toBean
的工作原理如下:
将ResultSet行转换为JavaBean。此实现使用反射和BeanInfo类将列名与bean属性名匹配。属性根据几个因素与列匹配:
- 该类具有与列同名的可写属性。名称比较不区分大小写
- 可以使用ResultSet.get*方法将列类型转换为属性的set方法参数类型。如果转换失败(即,属性为int,列为时间戳),将抛出SQLException
public static Object copyFromResultSet(Class clazz, ResultSet resultSet)
{
ArrayList objectArrayList = new ArrayList(1);
try
{
Object object = clazz.newInstance();
objectArrayList.add(object);
copyFromResultSet(objectArrayList, resultSet);
}
catch (Exception e)
{
e.printStackTrace();
}
return objectArrayList.get(0);
}
然后:
公共静态void copyFromResultSet(ArrayList对象ArrayList,ResultSet ResultSet)
{
ArrayList ArrayList=null;
尝试
{
if(objectArrayList!=null)
{
int objectArrayList_len=objectArrayList.size();
int objectArrayList_index=0;
java.beans.BeanInfo toBeanInfo[]=新的java.beans.BeanInfo[objectArrayList_len];
向量objectMethodVector[]=新向量[objectArrayList_len];
向量objectTypeVector[]=新向量[objectArrayList_len];
int totalMethod[]=新的int[objectArrayList_len];
int[][]索引=新的int[objectArrayList_len][];
for(objectArrayList_index=0;objectArrayList_index0&&resultSet!=null)
{
方法=null;
类型[]=null;
int cols=0;
字符串colName=null;
for(objectArrayList_index=0;objectArrayList_index 对于(int i=1;i NPE指向何处?当BeanProcessor
创建emp对象时,它很有可能不会通过在Employee
对象中创建新实例来初始化Address
对象。请检查列类型是否可以转换为属性的set method参数使用ResultSet.get方法键入。*我对外键处理有点困惑。每个列都映射到与列同名的setter方法。请检查ResultSet中的列名。
public static void copyFromResultSet(ArrayList<Object> objectArrayList, ResultSet resultSet)
{
ArrayList arrayList = null;
try
{
if (objectArrayList != null)
{
int objectArrayList_len = objectArrayList.size();
int objectArrayList_index = 0;
java.beans.BeanInfo toBeanInfo[] = new java.beans.BeanInfo[objectArrayList_len];
Vector<Method> objectMethodVector[] = new Vector[objectArrayList_len];
Vector<Type> objectTypeVector[] = new Vector[objectArrayList_len];
int totalMethod[] = new int[objectArrayList_len];
int[][] indexes = new int[objectArrayList_len][];
for (objectArrayList_index = 0; objectArrayList_index < objectArrayList_len; objectArrayList_index++)
{
toBeanInfo[objectArrayList_index] = java.beans.Introspector.getBeanInfo(objectArrayList.get(objectArrayList_index).getClass());
}
if (objectArrayList_len > 0 && resultSet != null)
{
Method method = null;
Type type[] = null;
int cols = 0;
String colName = null;
for (objectArrayList_index = 0; objectArrayList_index < objectArrayList_len; objectArrayList_index++)
{
//toBeanInfo[objectArrayList_index]=java.beans.Introspector.getBeanInfo(objectArrayList.get(objectArrayList_index).getClass());
java.beans.PropertyDescriptor toPropertyDescriptor[] = toBeanInfo[objectArrayList_index].getPropertyDescriptors();
int toPropertyDescriptor_length = toPropertyDescriptor.length;
method = null;
type = null;
ResultSetMetaData resultSetMetaData = resultSet.getMetaData();
cols = resultSetMetaData.getColumnCount();
colName = null;
Vector<Method> methodVector = new Vector(cols);
Vector<Type> typeVector = new Vector(cols);
indexes[objectArrayList_index] = new int[cols];
totalMethod[objectArrayList_index] = -1;
for (int i = 1; i <= cols; i++)
{
colName = resultSetMetaData.getColumnName(i);
for (int j = 0; j < toPropertyDescriptor_length; j++)
{
if (toPropertyDescriptor[j].getName().equalsIgnoreCase(colName))
{
totalMethod[objectArrayList_index]++;
method = toPropertyDescriptor[j].getWriteMethod();
type = method.getGenericParameterTypes();
methodVector.add(method);
typeVector.add(type[0]);
indexes[objectArrayList_index][totalMethod[objectArrayList_index]] = i;
break;
}
}
}
objectMethodVector[objectArrayList_index] = (methodVector);
objectTypeVector[objectArrayList_index] = (typeVector);
}
if (resultSet.next())
{
arrayList = new ArrayList();
for (objectArrayList_index = 0; objectArrayList_index < objectArrayList_len; objectArrayList_index++)
{
for (int i = 0; i <= totalMethod[objectArrayList_index]; i++)
{
//System.out.println(objectMethodVector[objectArrayList_index].get(i));
objectMethodVector[objectArrayList_index].get(i).invoke(objectArrayList.get(objectArrayList_index), getObject(indexes[objectArrayList_index][i], objectTypeVector[objectArrayList_index].get(i), resultSet));
}
arrayList.add(objectArrayList.get(objectArrayList_index));
}
}
while (resultSet.next())
{
for (objectArrayList_index = 0; objectArrayList_index < objectArrayList_len; objectArrayList_index++)
{
for (int i = 0; i <= totalMethod[objectArrayList_index]; i++)
{
objectMethodVector[objectArrayList_index].get(i).invoke(objectArrayList.get(objectArrayList_index), getObject(indexes[objectArrayList_index][i], objectTypeVector[objectArrayList_index].get(i), resultSet));
}
arrayList.add(objectArrayList.get(objectArrayList_index));
}
}
}
}
}
catch (Exception e)
{
e.printStackTrace();
}
}