2013-02-18 22 views
0

我正在使用以下內容將檢索到的值添加到類中。所有的值將被添加到類的屬性,但我正在使用compisition(在類中有一個類的對象),它不顯示任何輸出。如何將結果集複製到對象中?

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類是如下:

public class Address { 
    ..... 
    private String block; 
    .... 
     public String getBlock() { 
      return block; 
     } 

     public void setBlock(String block) { 
      this.block = block; 
     } 
    .... 
    } 
+0

NPE指向哪裏? – 2013-02-18 05:07:44

+2

很有可能當你的BeanProcessor創建** emp **對象時,它不會通過在你的Employee對象中爲它創建一個新的實例來初始化你的Address對象。請檢查 – SudoRahul 2013-02-18 05:07:46

回答

1

BeanProcessor.toBean是這樣的:

轉換一個結果行插入一個JavaBean。此實現使用反射和BeanInfo類將列名稱與bean屬性名稱進行匹配。屬性根據幾個因素與列匹配:

  • 該類具有一個與列名稱相同的可寫屬性。名稱比較不區分大小寫。
  • 使用ResultSet.get *方法可以將列類型轉換爲屬性的set方法參數類型。如果轉換失敗(即,該屬性是一個int並且該列是時間戳),則會拋出SQLException。

當從ResultSet返回SQL NULL時,原始Bean屬性設置爲其默認值。數字字段設置爲0,布爾值設置爲false。當返回SQL NULL時,對象bean屬性設置爲空。這與ResultSet get *方法的行爲相同。

可能是地址不是可寫屬性。請檢查一下。

+0

我已更新問題 – 2013-02-18 05:20:14

+0

*可以使用ResultSet.get *方法將列類型轉換爲屬性的set方法參數類型*我對外鍵處理有點困惑。每列都映射到與列名稱相同的setter方法。檢查結果集中的列名稱。 – 2013-02-18 07:24:38

0
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(類,結果集) 傳遞兩個perameters第一是類名和第二個是結果集。

我相信這是正常工作

相關問題