java - Populate Array from Oracle Database -
i trying create db connection , populate array results. need populating "destinationitem[]" sql below.
//destinationbean.java // manual array works need populated db using below query , 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 take db connection logic , populate "destinationitem[]" array above , 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 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); } }
here's snippet should need:
list<destinationitem> destinationslist = new arraylist<destinationitem>(); while (rs.next()) { destinationslist.add(new destinationitem( rs.getstring("destination_id"), rs.getstring("commdef_id"))); } rs.close(); // if need convert list array, can this: destinationitem[] destinationsarray = destinationslist.toarray( new destinationitem[destinationslist.size()]);
some considerations:
it easiest use dynamic data structure such list
retrieving data resultset
, because way don't need care number of rows returned db before iteration.
it considered best practise never return null
methods return list
s (like getcustomer()
method). if no data found, return empty list
. make client code simpler when null
checks not needed.
you should close connection con
, preparedstatement ps
resources open in method. common idiom have structure in data-accessing methods:
connection con = null; preparedstatement ps = null; try { // initialize , use 'con' , 'ps' } { if (ps != null) { ps.close(); } if (con != null) { con.close(); } }
if on java 7 or above, can same in more elegant way try-with-resources statement.
finally, instead of catch (exception e)
, should consider either throwing possible (checked) sqlexception
method, or if not want change method signature, wrapping sqlexception
runtimeexception
. in general, should not catch exception
s cannot (or not) handle in method. in these cases, it's better let problem bubble client code instead of silently ignoring it.
Comments
Post a Comment