Author: dennisl Date: Sun Mar 16 15:49:34 2008 New Revision: 637681 URL: http://svn.apache.org/viewvc?rev=637681&view=rev Log: [DBUTILS-38] example documentation page, update query.
- Add examples on how to insert and update data. - Add an example on how to use the BeanListHandler to fetch a List of beans. Modified: commons/proper/dbutils/trunk/xdocs/examples.xml Modified: commons/proper/dbutils/trunk/xdocs/examples.xml URL: http://svn.apache.org/viewvc/commons/proper/dbutils/trunk/xdocs/examples.xml?rev=637681&r1=637680&r2=637681&view=diff ============================================================================== --- commons/proper/dbutils/trunk/xdocs/examples.xml (original) +++ commons/proper/dbutils/trunk/xdocs/examples.xml Sun Mar 16 15:49:34 2008 @@ -99,6 +99,34 @@ } ]]> </source> + +<p> + You can not only fetch data from the database - you can also insert or update + data. The following example will first insert a person into the database and + after that change the person's height. +</p> + +<source> +QueryRunner run = new QueryRunner( dataSource ); +try +{ + // Create an object array to hold the values to insert + Object[] insertParams = {"John Doe", new Double( 1.82 )}; + // Execute the SQL update statement and return the number of + // inserts that were made + int inserts = run.update( "INSERT INTO Person (name,height) VALUES (?,?)", + insertParams ); + + // Now it's time to rise to the occation... + Object[] updateParams = {new Double( 2.05 ), "John Doe"}; + int updates = run.update( "UPDATE Person SET height=? WHERE name=?", + updateParams ); +} +catch(SQLException sqle) { + // Handle it +} +</source> + </section> @@ -115,6 +143,11 @@ another that converts all rows in the <code>ResultSet</code>. </p> +<p> + We'll start with an example using the <code>BeanHandler</code> to fetch one + row from the <code>ResultSet</code> and turn it into a JavaBean. +</p> + <source> QueryRunner run = new QueryRunner(dataSource); @@ -126,6 +159,23 @@ // return the results in a new Person object generated by the BeanHandler. Person p = (Person) run.query( "SELECT * FROM Person WHERE name=?", "John Doe", h); +</source> + +<p> + This time we will use the BeanListHandler to fetch all rows from the + <code>ResultSet</code> and turn them into a <code>List</code> of JavaBeans. +</p> + +<source> +QueryRunner run = new QueryRunner(dataSource); + +// Use the BeanListHandler implementation to convert all +// ResultSet rows into a List of Person JavaBeans. +ResultSetHandler h = new BeanListHandler(Person.class); + +// Execute the SQL statement and return the results in a List of +// Person objects generated by the BeanListHandler. +List persons = (List) run.query("SELECT * FROM Person", h); </source> </section>