@ -1500,7 +1502,7 @@ BEGIN
@@ -1500,7 +1502,7 @@ BEGIN
SELECT first_name, last_name, birth_date
INTO out_first_name, out_last_name, out_birth_date
FROM t_actor where id = in_id;
END;</programlisting>As you can see there are four parameters. One is an in
END;]]></programlisting>As you can see there are four parameters. One is an in
parameter "in_id" containing the id of the Actor we are looking up. The
remaining parameters are out parameters and they will be used to return
the data read from the table.</para>
@ -1510,7 +1512,7 @@ END;</programlisting>As you can see there are four parameters. One is an in
@@ -1510,7 +1512,7 @@ END;</programlisting>As you can see there are four parameters. One is an in
subclass and we declare it in the initialization method. For this
example, all we need to specify is the name of the procedure.</para>
<para><programlistinglanguage="java">public class JdbcActorDao implements ActorDao {
<para><programlistinglanguage="java"><![CDATA[public class JdbcActorDao implements ActorDao {
private SimpleJdbcTemplate simpleJdbcTemplate;
private SimpleJdbcCall procReadActor;
@ -1534,7 +1536,7 @@ END;</programlisting>As you can see there are four parameters. One is an in
@@ -1534,7 +1536,7 @@ END;</programlisting>As you can see there are four parameters. One is an in
}
// ... additional methods
}</programlisting>The execution of the call involves creating an
}]]></programlisting>The execution of the call involves creating an
<classname>SqlParameterSource</classname> containing the in parameter.
It's important to match the name of the parameter declared in the stored
procedure. The case doesn't have to match since we use metadata to
@ -1565,7 +1567,7 @@ END;</programlisting>As you can see there are four parameters. One is an in
@@ -1565,7 +1567,7 @@ END;</programlisting>As you can see there are four parameters. One is an in
<classname>commons-collections.jar</classname> on your classpath for
this to work. Here is an example of this configuration:</para>
<para><programlistinglanguage="java">public class JdbcActorDao implements ActorDao {
<para><programlistinglanguage="java"><![CDATA[public class JdbcActorDao implements ActorDao {
private SimpleJdbcCall procReadActor;
public void setDataSource(DataSource dataSource) {
@ -1578,8 +1580,8 @@ END;</programlisting>As you can see there are four parameters. One is an in
@@ -1578,8 +1580,8 @@ END;</programlisting>As you can see there are four parameters. One is an in
// ... additional methods
}</programlisting>By doing this, you don't have to worry about the case used
for the names of your returned out parameters.</para>
}]]></programlisting>By doing this, you don't have to worry about the case
used for the names of your returned out parameters.</para>
</section>
<sectionid="jdbc-simple-jdbc-call-2">
@ -1606,7 +1608,7 @@ END;</programlisting>As you can see there are four parameters. One is an in
@@ -1606,7 +1608,7 @@ END;</programlisting>As you can see there are four parameters. One is an in
<para>This is what a fully declared procedure call declaration of our
earlier example would look like:</para>
<para><programlistinglanguage="java">public class JdbcActorDao implements ActorDao {
<para><programlistinglanguage="java"><![CDATA[public class JdbcActorDao implements ActorDao {
private SimpleJdbcCall procReadActor;
public void setDataSource(DataSource dataSource) {
@ -1627,7 +1629,7 @@ END;</programlisting>As you can see there are four parameters. One is an in
@@ -1627,7 +1629,7 @@ END;</programlisting>As you can see there are four parameters. One is an in
// ... additional methods
}</programlisting>The execution and end results are the same, we are just
}]]></programlisting>The execution and end results are the same, we are just
specifying all the details explicitly rather than relying on metadata.
This will be necessary if the database we use is not part of the
supported databases. Currently we support metadata lookup of stored
@ -1647,8 +1649,8 @@ END;</programlisting>As you can see there are four parameters. One is an in
@@ -1647,8 +1649,8 @@ END;</programlisting>As you can see there are four parameters. One is an in
<classname>java.sql.Types</classname> constants. We have already seen
declarations like:</para>
<para><programlistinglanguage="java"> new SqlParameter("in_id", Types.NUMERIC),
new SqlOutParameter("out_first_name", Types.VARCHAR),</programlisting></para>
<para><programlistinglanguage="java"><![CDATA[ new SqlParameter("in_id", Types.NUMERIC),
new SqlOutParameter("out_first_name", Types.VARCHAR),]]></programlisting></para>
<para>The first line with the <classname>SqlParameter</classname>
declares an in parameter. In parameters can be used for both stored
@ -1668,7 +1670,7 @@ END;</programlisting>As you can see there are four parameters. One is an in
@@ -1668,7 +1670,7 @@ END;</programlisting>As you can see there are four parameters. One is an in
input values. This is different from the
<classname>StoredProcedure</classname> class which for backwards
compatibility reasons allows input values to be provided for
parameters declared as <classname>SqlOutParameter</classname>.</para>
parameters declared as <classname>SqlOutParameter</classname>.</para>
</note>
<para>In addition to the name and the SQL type you can specify
@ -1699,7 +1701,7 @@ END;</programlisting>As you can see there are four parameters. One is an in
@@ -1699,7 +1701,7 @@ END;</programlisting>As you can see there are four parameters. One is an in
that returns an actor's full name. Here is the MySQL source for this
function:</para>
<para><programlisting>CREATE FUNCTION get_actor_name (in_id INTEGER)
<para><programlisting><![CDATA[CREATE FUNCTION get_actor_name (in_id INTEGER)
RETURNS VARCHAR(200) READS SQL DATA
BEGIN
DECLARE out_name VARCHAR(200);
@ -1707,13 +1709,13 @@ BEGIN
@@ -1707,13 +1709,13 @@ BEGIN
INTO out_name
FROM t_actor where id = in_id;
RETURN out_name;
END;</programlisting></para>
END;]]></programlisting></para>
<para>To call this function we again create a
<classname>SimpleJdbcCall</classname> in the initialization
method.</para>
<para><programlistinglanguage="java">public class JdbcActorDao implements ActorDao {
<para><programlistinglanguage="java"><![CDATA[public class JdbcActorDao implements ActorDao {
SELECT a.id, a.first_name, a.last_name, a.birth_date FROM t_actor a;
END;</programlisting>In order to call this procedure we need to declare the
END;]]></programlisting>In order to call this procedure we need to declare the
<classname>RowMapper</classname> to be used. Since the class we want to
map to follows the JavaBean rules, we can use a
<classname>ParameterizedBeanPropertyRowMapper</classname> that is
created by passing in the required class to map to in the
<classname>newInstance</classname> method.</para>
<para><programlistinglanguage="java">public class JdbcActorDao implements ActorDao {
<para><programlistinglanguage="java"><![CDATA[public class JdbcActorDao implements ActorDao {
private SimpleJdbcTemplate simpleJdbcTemplate;
private SimpleJdbcCall procReadAllActors;
@ -1787,12 +1789,12 @@ END;</programlisting>In order to call this procedure we need to declare the
@@ -1787,12 +1789,12 @@ END;</programlisting>In order to call this procedure we need to declare the
}
public List getActorsList() {
Map m = procReadAllActors.execute(new HashMap<String, Object>(0));
Map m = procReadAllActors.execute(new HashMap<String,Object>(0));
return (List) m.get("actors");
}
// ... additional methods
}</programlisting>The execute call passes in an empty Map since this call
}]]></programlisting>The execute call passes in an empty Map since this call
doesn't take any parameters. The list of Actors is then retrieved from
the results map and returned to the caller.</para>
</section>
@ -1855,7 +1857,7 @@ END;</programlisting>In order to call this procedure we need to declare the
@@ -1855,7 +1857,7 @@ END;</programlisting>In order to call this procedure we need to declare the
customer relation to an instance of the <classname>Customer</classname>
class.</para>
<programlistinglanguage="java">private class CustomerMappingQuery extends MappingSqlQuery {
<programlistinglanguage="java"><![CDATA[private class CustomerMappingQuery extends MappingSqlQuery {
public CustomerMappingQuery(DataSource ds) {
super(ds, "SELECT id, name FROM customer WHERE id = ?");
@ -1869,7 +1871,7 @@ END;</programlisting>In order to call this procedure we need to declare the
@@ -1869,7 +1871,7 @@ END;</programlisting>In order to call this procedure we need to declare the
cust.setName(rs.getString("name"));
return cust;
}
}</programlisting>
}]]></programlisting>
<para>We provide a constructor for this customer query that takes the
<interfacename>DataSource</interfacename> as the only parameter. In this
@ -1886,18 +1888,18 @@ END;</programlisting>In order to call this procedure we need to declare the
@@ -1886,18 +1888,18 @@ END;</programlisting>In order to call this procedure we need to declare the
have been defined we call the <literal>compile()</literal> method so the
statement can be prepared and later be executed.</para>
CustomerMappingQuery custQry = new CustomerMappingQuery(dataSource);
Object[] parms = new Object[1];
parms[0] = id;
List customers = custQry.execute(parms);
if (customers.size() > 0) {
if (customers.size() > 0) {
return (Customer) customers.get(0);
}
else {
return null;
}
}</programlisting>
}]]></programlisting>
<para>The method in this example retrieves the customer with the id that
is passed in as the only parameter. After creating an instance of the
@ -1975,8 +1977,8 @@ public class UpdateCreditRating extends SqlUpdate {
@@ -1975,8 +1977,8 @@ public class UpdateCreditRating extends SqlUpdate {
SQL type is specified using the <classname>java.sql.Types</classname>
constants. We have already seen declarations like:</para>
<para><programlistinglanguage="java"> new SqlParameter("in_id", Types.NUMERIC),
new SqlOutParameter("out_first_name", Types.VARCHAR),</programlisting></para>
<para><programlistinglanguage="java"><![CDATA[ new SqlParameter("in_id", Types.NUMERIC),
new SqlOutParameter("out_first_name", Types.VARCHAR),]]></programlisting></para>
<para>The first line with the <classname>SqlParameter</classname>
declares an in parameter. In parameters can be used for both stored
@ -1991,11 +1993,11 @@ public class UpdateCreditRating extends SqlUpdate {
@@ -1991,11 +1993,11 @@ public class UpdateCreditRating extends SqlUpdate {
that also return a value</para>
<para><note>
<para>Parameters declared as <classname>SqlParameter</classname>
and <classname>SqlInOutParameter</classname> will always be used to
<para>Parameters declared as <classname>SqlParameter</classname> and
<classname>SqlInOutParameter</classname> will always be used to
provide input values. In addition to this any parameter declared as
<classname>SqlOutParameter</classname> where an non-null input value
is provided will also be used as an input paraneter.</para>
is provided will also be used as an input paraneter.</para>
</note></para>
<para>In addition to the name and the SQL type you can specify
@ -2113,7 +2115,7 @@ public class TitlesAndGenresStoredProcedure extends StoredProcedure {
@@ -2113,7 +2115,7 @@ public class TitlesAndGenresStoredProcedure extends StoredProcedure {
<classname>Title</classname> domain object for each row in the supplied
@ -2127,14 +2129,14 @@ public final class TitleMapper implements RowMapper {
@@ -2127,14 +2129,14 @@ public final class TitleMapper implements RowMapper {
title.setName(rs.getString("name"));
return title;
}
}</programlisting>
}]]></programlisting>
<para>Secondly, the <classname>GenreMapper</classname> class, which
again simply maps a <interfacename>ResultSet</interfacename> to a
<classname>Genre</classname> domain object for each row in the supplied
@ -2180,7 +2182,7 @@ public class TitlesAfterDateStoredProcedure extends StoredProcedure {
@@ -2180,7 +2182,7 @@ public class TitlesAfterDateStoredProcedure extends StoredProcedure {
inputs.put(CUTOFF_DATE_PARAM, cutoffDate);
return super.execute(inputs);
}
}</programlisting>
}]]></programlisting>
</section>
<sectionid="jdbc-SqlFunction">
@ -2211,11 +2213,11 @@ public class TitlesAfterDateStoredProcedure extends StoredProcedure {
@@ -2211,11 +2213,11 @@ public class TitlesAfterDateStoredProcedure extends StoredProcedure {
the appropriate run method repeatedly to execute the function. Here is
an example of retrieving the count of rows from a table:</para>
<programlistinglanguage="java">public int countRows() {
<programlistinglanguage="java"><![CDATA[public int countRows() {
SqlFunction sf = new SqlFunction(dataSource, "select count(*) from mytable");
sf.compile();
return sf.run();
}</programlisting>
}]]></programlisting>
</section>
</section>
@ -2346,7 +2348,7 @@ public class TitlesAfterDateStoredProcedure extends StoredProcedure {
@@ -2346,7 +2348,7 @@ public class TitlesAfterDateStoredProcedure extends StoredProcedure {
<areacoords="13"id="jdbc.lobhandler.setBlob"/>
</areaspec>
<programlistinglanguage="java">final File blobIn = new File("spring2004.jpg");
<programlistinglanguage="java"><![CDATA[final File blobIn = new File("spring2004.jpg");
final InputStream blobIs = new FileInputStream(blobIn);
final File clobIn = new File("large.txt");
final InputStream clobIs = new FileInputStream(clobIn);