问题描述
我正在使用以下内容将检索到的值添加到类中.所有值都将添加到类的属性中,但我使用的是集合(在类中具有类的对象),并且在输出上没有显示任何内容.
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这样的工作:
将结果集行转换为Javabean.该实现使用反射和BeanInfo类将列名与Bean属性名称匹配.属性与基于几个因素的列匹配:
- 课程具有与列相同名称的可写属性.名称比较是不敏感的.
- 使用resultset.get*方法,可以将列类型转换为属性的SET方法参数类型.如果转换失败(即.
原始bean属性设置为默认值.数字字段设置为0,布尔值设置为false.返回SQL null时,将对象bean属性设置为null.这与结果集获得*方法是相同的行为.
可能是地址不是可写的财产.请检查一下.
其他推荐答案
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); }
然后:
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(); } }
只需复制此代码调用方法copyfromresultset(class,resultset) 通过两个perameters首先是类名称,第二个是结果集.
我确定这是正确的
问题描述
I am using the following to add retrieved values to the class. all values will be added to attributes of the class but I am using compisition ( have an object of class in the class) and it does not show anything on output.
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;
Address class is as following:
public class Address { ..... private String block; .... public String getBlock() { return block; } public void setBlock(String block) { this.block = block; } .... }
推荐答案
The BeanProcessor.toBean works like this:
Convert a ResultSet row into a JavaBean. This implementation uses reflection and BeanInfo classes to match column names to bean property names. Properties are matched to columns based on several factors:
- The class has a writable property with the same name as a column. The name comparison is case insensitive.
- The column type can be converted to the property's set method parameter type with a ResultSet.get* method. If the conversion fails (ie. the property was an int and the column was a Timestamp) an SQLException is thrown.
Primitive bean properties are set to their defaults when SQL NULL is returned from the ResultSet. Numeric fields are set to 0 and booleans are set to false. Object bean properties are set to null when SQL NULL is returned. This is the same behavior as the ResultSet get* methods.
May be the address is not a writable property. Pls do check it.
其他推荐答案
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); }
then:
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(); } }
just copy paste this code call method copyFromResultSet(class, ResultSet ) pass two perameters first is class name and second is resultset.
i am sure this is working properlly