我试图从数据库连接创建此数据,并使用结果填充数组。我只需要帮助从下面的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