Return-Path: Delivered-To: apmail-jakarta-commons-user-archive@www.apache.org Received: (qmail 42848 invoked from network); 12 Dec 2003 17:06:54 -0000 Received: from daedalus.apache.org (HELO mail.apache.org) (208.185.179.12) by minotaur-2.apache.org with SMTP; 12 Dec 2003 17:06:54 -0000 Received: (qmail 60469 invoked by uid 500); 12 Dec 2003 17:06:24 -0000 Delivered-To: apmail-jakarta-commons-user-archive@jakarta.apache.org Received: (qmail 60415 invoked by uid 500); 12 Dec 2003 17:06:23 -0000 Mailing-List: contact commons-user-help@jakarta.apache.org; run by ezmlm Precedence: bulk List-Unsubscribe: List-Subscribe: List-Help: List-Post: List-Id: "Jakarta Commons Users List" Reply-To: "Jakarta Commons Users List" Delivered-To: mailing list commons-user@jakarta.apache.org Received: (qmail 60355 invoked from network); 12 Dec 2003 17:06:23 -0000 Received: from unknown (HELO web60810.mail.yahoo.com) (216.155.196.73) by daedalus.apache.org with SMTP; 12 Dec 2003 17:06:23 -0000 Message-ID: <20031212151733.8074.qmail@web60810.mail.yahoo.com> Received: from [66.80.49.7] by web60810.mail.yahoo.com via HTTP; Fri, 12 Dec 2003 07:17:33 PST Date: Fri, 12 Dec 2003 07:17:33 -0800 (PST) From: David Graham Subject: Re: DBCP & DBUtils To: Jakarta Commons Users List In-Reply-To: <3FD937C4.6060504@comcast.net> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii X-Spam-Rating: daedalus.apache.org 1.6.2 0/1000/N X-Spam-Rating: minotaur-2.apache.org 1.6.2 0/1000/N > 2. DBUtils - These sound real neat and I'd really like to use them, but > I can't find any decent documentation to get a beginner started. The > example page (http://jakarta.apache.org/commons/dbutils/examples.html) > may be enough to remind a developer how things work, but it's rough for > me because it leaves alot of questions and guessing. I'll look at adding more examples soon but this is a community effort so feel free to open a bugzilla enhancement ticket with a patch in cvs diff -u format on the examples. Questions for different commons components should be sent in separate emails with the component name prefixed on the subject line like [dbutils] or [dbcp] so that people can filter messages accordingly. David > > I've used the JOCLPoolingDriverExample.java from DBCP as a basis for my > playing with the Utils. Following the directions on the example page, I > wasn't able to get it to work. Nor do I really understand the way its > supposed to work. > > I gather the QueryRunner is designed to execute an sql query and give > you an object you define instead of a ResultSet. The example nor the > apis really tell you what the ResultSetHandler is doing. And what kind > of bean are you supposed to setup? I mean, if you do a SELECT * on some > table you can get alot of rows back, but with the QueryRunner you only > get a single object back. And I'm guessing you define that object > yourself. But, is that bean supposed to represent a row of the table > returned from your query? If it is, how do you access all of the rows. > Obviously, I'm confused and couldn't find anything to help. The only way > > I got it to work was as follows: > > import java.sql.DriverManager; > import java.sql.Connection; > import java.sql.Statement; > import java.sql.ResultSet; > import java.sql.SQLException; > import java.util.ArrayList; > import java.util.Iterator; > > import org.apache.commons.dbutils.QueryRunner; > import org.apache.commons.dbutils.ResultSetHandler; > import org.apache.commons.dbutils.handlers.BeanHandler; > > public class JOCLPoolingDriverExample { > > public static void main(String[] args) { > > Connection conn = null; > Statement stmt = null; > ResultSet rset = null; > > try { > System.out.println("Creating connection."); > conn = > DriverManager.getConnection("jdbc:apache:commons:dbcp:/db"); > > QueryRunner runner = new QueryRunner(); > ResultSetHandler h = new BeanHandler(OrderBean.class) { > public Object handle(ResultSet rs) throws SQLException { > > //OrderBean orderBean = new OrderBean(); > ArrayList orders = new ArrayList(); > > while(rs.next()) > { > OrderBean orderBean = new OrderBean(); > orderBean.setOrderId(rs.getString(1)); > orderBean.setTotalAmount(rs.getString(2)); > orders.add(orderBean); > } > > return orders; > } > }; > > ArrayList p = > (ArrayList)runner.query(conn, "SELECT ORDER_ID, > TOTAL_AMT FROM ORDERS", h); > > Iterator iter = p.iterator(); > while(iter.hasNext()) > { > System.out.println((OrderBean)iter.next()); > } > } catch(SQLException e) { > e.printStackTrace(); > } finally { > try { rset.close(); } catch(Exception e) { } > try { stmt.close(); } catch(Exception e) { } > try { conn.close(); } catch(Exception e) { } > } > } > } > > > I doubt this is at all correct as its really not doing anything for me. > It's just as much work. From the example, it infers that the > ResultSetHandler class will work some magic in dumping your query into > an Object. Can anyone help? > > Thanks, > Mike Zatko > > > --------------------------------------------------------------------- > To unsubscribe, e-mail: commons-user-unsubscribe@jakarta.apache.org > For additional commands, e-mail: commons-user-help@jakarta.apache.org > __________________________________ Do you Yahoo!? Free Pop-Up Blocker - Get it now http://companion.yahoo.com/ --------------------------------------------------------------------- To unsubscribe, e-mail: commons-user-unsubscribe@jakarta.apache.org For additional commands, e-mail: commons-user-help@jakarta.apache.org