2015-04-14 37 views
0

我试图从数据库连接创建此数据,并使用结果填充数组。我只需要帮助从下面的SQL填充“DestinationItem []”。从Oracle数据库填充数组

//DestinationBean.java 
// Manual Array works but I need this to be populated from DB using the below Query and DB Connection info. 
    private DestinationItem[] destinationResults = new DestinationItem[]{ 
      new DestinationItem("58285", "Dodge Grand Caravan"), 
      new DestinationItem("57605", "Dodge SX 2.0"), 
      new DestinationItem("58265", "Chrysler 300 Touring") 
    }; 

    public DestinationItem[] getdestinationResults() { 
     return destinationResults; 
    } 

    public class DestinationItem { 
     String destid; 
     String commdefid; 

     public DestinationItem(String destid, String commdefid) { 
      this.destid = destid; 
      this.commdefid = commdefid; 
     } 
// Getter/Setter below 

// END 

我需要采取这种数据库连接逻辑和上面的“DestinationItem []”数组填充,我需要帮助。

//DBConnection 
public static ArrayList<CustomerBean> getCustomer() { 
    try { 
     Class.forName("oracle.jdbc.OracleDriver").newInstance(); 

     Connection con = DriverManager.getConnection("jdbc:oracle:thin:", "BLAH", "BLAH"); 
     PreparedStatement ps = con.prepareStatement("select destination_id, commdef_id from BLAH.destination"); 

     ArrayList<CustomerBean> al = new ArrayList<CustomerBean>(); 

     ResultSet rs = ps.executeQuery(); 

     boolean found = false; 

     while (rs.next()) { 
      CustomerBean e = new CustomerBean(); 
      e.setDestId(rs.getString("destination_id")); 
      e.setDestId(rs.getString("commdef_id")); 
      al.add(e); 
      found = true; 
     } 

     rs.close(); 
     if (found) { 
      return al; 
     } else { 
      return null; // no entires found 
     } 
    } catch (Exception e) { 
     System.out.println("Error In getCustomer() -->" + .getMessage()); 
     return (null); 
    } 
} 
+0

你的问题到底是什么? – Mureinik

+0

填充 “DestinationItem []” 使用,我提供 目前我已填充了 “DestinationItem []” 手动与数据库的连接信息数据库: “私人DestinationItem [] destinationResults =新DestinationItem [] { 新DestinationItem ( “58285”, “道奇大捷龙”), 新DestinationItem( “57605”, “道奇SX 2.0”), 新DestinationItem( “58265”, “克莱斯勒300旅行车”) }; “ 但我需要从“从BLAH.destination中选择destination_id,commdef_id”填充它,我不知道该怎么做。 – djcajun

回答

0

这里有一个片段,它应该做你需要的东西:

List<DestinationItem> destinationsList = new ArrayList<DestinationItem>(); 

while (rs.next()) { 
    destinationsList.add(new DestinationItem(
     rs.getString("destination_id"), rs.getString("commdef_id"))); 
} 
rs.close(); 

// if you need to convert the list into an array, you can do it like this: 
DestinationItem[] destinationsArray = destinationsList.toArray(
              new DestinationItem[destinationsList.size()]); 

一些注意事项:

这是最容易使用的动态数据结构,如ListResultSet检索数据,因为这样你不需要关心迭代之前从数据库返回的行数。

从返回List s的方法(如您的getCustomer()方法)决不会返回null被认为是最佳做法。如果没有找到数据,只需返回一个空的List。当不需要检查null时,这将使客户端代码更简单。

您应该关闭在该方法中打开的Connection conPreparedStatement ps资源。一个常见的成语是在数据访问方法,这样的结构:

Connection con = null; 
PreparedStatement ps = null; 
try { 
    // initialize and use 'con' and 'ps' 
} finally { 
    if (ps != null) { 
     ps.close(); 
    } 
    if (con != null) { 
     con.close(); 
    } 
} 

如果您对Java 7的或以上,你可以做同样的与try-with-resources statement更优雅的方式。

最后,而不是catch (Exception e),你应该考虑无论是从方法抛出一个可能的(检查)SQLException,或者如果你不想改变方法签名,包裹SQLExceptionRuntimeException。一般来说,你不应该捕获你不能(或不)处理你的方法的Exception。在这些情况下,最好让问题冒泡到客户端代码而不是默默地忽略它。

+0

谢谢soooooo - 我会尝试。我认为我需要将它转换为一个字符串数组,因为我有它的工作方式,但它抱怨说它是一个“List”而不是String Array。 – djcajun

+0

你可能意味着你需要一个'DestinationItem'数组(不是'String'数组)? –