我試圖從數據庫連接創建此數據,並使用結果填充數組。我只需要幫助從下面的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);
}
}
你的問題到底是什麼? – Mureinik
填充 「DestinationItem []」 使用,我提供 目前我已填充了 「DestinationItem []」 手動與數據庫的連接信息數據庫: 「私人DestinationItem [] destinationResults =新DestinationItem [] { 新DestinationItem ( 「58285」, 「道奇大捷龍」), 新DestinationItem( 「57605」, 「道奇SX 2.0」), 新DestinationItem( 「58265」, 「克萊斯勒300旅行車」) }; 「 但我需要從「從BLAH.destination中選擇destination_id,commdef_id」填充它,我不知道該怎麼做。 – djcajun