Skip to Main Content

Java Programming

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Populate Array from Database!

f4e45476-6814-4a89-89fe-879179d6a309Apr 14 2015 — edited Apr 15 2015

I have this working manually, but please need help creating this from a DB Connection and populating the Array with the results.

I just need help with populating "DestinationItem[]" from the SQL below

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

I need to take this DB Connection Logic and populate the "DestinationItem[]" Array above and I need help.

//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() -->" + e.getMessage());

            return (null);

        }

    }

Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on May 13 2015
Added on Apr 14 2015
8 comments
1,274 views