Skip to content

Commit 9d1789e

Browse files
committed
Revised code examples for stored procedure type declarations
Issue: SPR-16811
1 parent 849b6cc commit 9d1789e

File tree

1 file changed

+32
-40
lines changed

1 file changed

+32
-40
lines changed

src/asciidoc/data-access.adoc

Lines changed: 32 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -2971,7 +2971,6 @@ in the primitive wrapper classes explicitly or using auto-boxing.
29712971
[subs="verbatim,quotes"]
29722972
----
29732973
import javax.sql.DataSource;
2974-
29752974
import org.springframework.jdbc.core.JdbcTemplate;
29762975
29772976
public class ExecuteAnUpdate {
@@ -4018,7 +4017,7 @@ data from the `t_actor` relation to an instance of the `Actor` class.
40184017
40194018
public ActorMappingQuery(DataSource ds) {
40204019
super(ds, "select id, first_name, last_name from t_actor where id = ?");
4021-
super.declareParameter(new SqlParameter("id", Types.INTEGER));
4020+
declareParameter(new SqlParameter("id", Types.INTEGER));
40224021
compile();
40234022
}
40244023
@@ -4039,7 +4038,7 @@ for this customer query takes the `DataSource` as the only parameter. In this
40394038
constructor you call the constructor on the superclass with the `DataSource` and the SQL
40404039
that should be executed to retrieve the rows for this query. This SQL will be used to
40414040
create a `PreparedStatement` so it may contain place holders for any parameters to be
4042-
passed in during execution.You must declare each parameter using the `declareParameter`
4041+
passed in during execution. You must declare each parameter using the `declareParameter`
40434042
method passing in an `SqlParameter`. The `SqlParameter` takes a name and the JDBC type
40444043
as defined in `java.sql.Types`. After you define all parameters, you call the
40454044
`compile()` method so the statement can be prepared and later executed. This class is
@@ -4092,9 +4091,7 @@ class since it can easily be parameterized by setting SQL and declaring paramete
40924091
[subs="verbatim"]
40934092
----
40944093
import java.sql.Types;
4095-
40964094
import javax.sql.DataSource;
4097-
40984095
import org.springframework.jdbc.core.SqlParameter;
40994096
import org.springframework.jdbc.object.SqlUpdate;
41004097
@@ -4173,9 +4170,7 @@ output parameter, in this case only one, using the parameter name as the key.
41734170
import java.util.Date;
41744171
import java.util.HashMap;
41754172
import java.util.Map;
4176-
41774173
import javax.sql.DataSource;
4178-
41794174
import org.springframework.beans.factory.annotation.Autowired;
41804175
import org.springframework.jdbc.core.SqlOutParameter;
41814176
import org.springframework.jdbc.object.StoredProcedure;
@@ -4222,14 +4217,13 @@ Oracle REF cursors).
42224217
[source,java,indent=0]
42234218
[subs="verbatim,quotes"]
42244219
----
4220+
import java.util.HashMap;
4221+
import java.util.Map;
4222+
import javax.sql.DataSource;
42254223
import oracle.jdbc.OracleTypes;
42264224
import org.springframework.jdbc.core.SqlOutParameter;
42274225
import org.springframework.jdbc.object.StoredProcedure;
42284226
4229-
import javax.sql.DataSource;
4230-
import java.util.HashMap;
4231-
import java.util.Map;
4232-
42334227
public class TitlesAndGenresStoredProcedure extends StoredProcedure {
42344228
42354229
private static final String SPROC_NAME = "AllTitlesAndGenres";
@@ -4259,12 +4253,10 @@ the supplied `ResultSet`:
42594253
[source,java,indent=0]
42604254
[subs="verbatim,quotes"]
42614255
----
4262-
import org.springframework.jdbc.core.RowMapper;
4263-
42644256
import java.sql.ResultSet;
42654257
import java.sql.SQLException;
4266-
42674258
import com.foo.domain.Title;
4259+
import org.springframework.jdbc.core.RowMapper;
42684260
42694261
public final class TitleMapper implements RowMapper<Title> {
42704262
@@ -4283,12 +4275,10 @@ the supplied `ResultSet`.
42834275
[source,java,indent=0]
42844276
[subs="verbatim,quotes"]
42854277
----
4286-
import org.springframework.jdbc.core.RowMapper;
4287-
42884278
import java.sql.ResultSet;
42894279
import java.sql.SQLException;
4290-
42914280
import com.foo.domain.Genre;
4281+
import org.springframework.jdbc.core.RowMapper;
42924282
42934283
public final class GenreMapper implements RowMapper<Genre> {
42944284
@@ -4306,17 +4296,15 @@ delegate to the superclass' untyped `execute(Map parameters)` method (which has
43064296
[source,java,indent=0]
43074297
[subs="verbatim,quotes"]
43084298
----
4309-
import oracle.jdbc.OracleTypes;
4310-
import org.springframework.jdbc.core.SqlOutParameter;
4311-
import org.springframework.jdbc.core.SqlParameter;
4312-
import org.springframework.jdbc.object.StoredProcedure;
4313-
4314-
import javax.sql.DataSource;
4315-
43164299
import java.sql.Types;
43174300
import java.util.Date;
43184301
import java.util.HashMap;
43194302
import java.util.Map;
4303+
import javax.sql.DataSource;
4304+
import oracle.jdbc.OracleTypes;
4305+
import org.springframework.jdbc.core.SqlOutParameter;
4306+
import org.springframework.jdbc.core.SqlParameter;
4307+
import org.springframework.jdbc.object.StoredProcedure;
43204308
43214309
public class TitlesAfterDateStoredProcedure extends StoredProcedure {
43224310
@@ -4411,6 +4399,7 @@ dependency injection.
44114399
final File clobIn = new File("large.txt");
44124400
final InputStream clobIs = new FileInputStream(clobIn);
44134401
final InputStreamReader clobReader = new InputStreamReader(clobIs);
4402+
44144403
jdbcTemplate.execute(
44154404
"INSERT INTO lob_table (id, a_clob, a_blob) VALUES (?, ?, ?)",
44164405
new AbstractLobCreatingPreparedStatementCallback(lobHandler) { # <1>
@@ -4421,6 +4410,7 @@ dependency injection.
44214410
}
44224411
}
44234412
);
4413+
44244414
blobIs.close();
44254415
clobReader.close();
44264416
----
@@ -4507,21 +4497,24 @@ declaration of an `SqlOutParameter`.
45074497
[source,java,indent=0]
45084498
[subs="verbatim,quotes"]
45094499
----
4510-
final TestItem = new TestItem(123L, "A test item",
4511-
new SimpleDateFormat("yyyy-M-d").parse("2010-12-31"));
4500+
public class TestItemStoredProcedure extends StoredProcedure {
45124501
4513-
declareParameter(new SqlOutParameter("item", OracleTypes.STRUCT, "ITEM_TYPE",
4514-
new SqlReturnType() {
4515-
public Object getTypeValue(CallableStatement cs, int colIndx, int sqlType, String typeName) throws SQLException {
4516-
STRUCT struct = (STRUCT) cs.getObject(colIndx);
4517-
Object[] attr = struct.getAttributes();
4518-
TestItem item = new TestItem();
4519-
item.setId(((Number) attr[0]).longValue());
4520-
item.setDescription((String) attr[1]);
4521-
item.setExpirationDate((java.util.Date) attr[2]);
4522-
return item;
4523-
}
4524-
}));
4502+
public TestItemStoredProcedure(DataSource dataSource) {
4503+
...
4504+
declareParameter(new SqlOutParameter("item", OracleTypes.STRUCT, "ITEM_TYPE",
4505+
new SqlReturnType() {
4506+
public Object getTypeValue(CallableStatement cs, int colIndx, int sqlType, String typeName) throws SQLException {
4507+
STRUCT struct = (STRUCT) cs.getObject(colIndx);
4508+
Object[] attr = struct.getAttributes();
4509+
TestItem item = new TestItem();
4510+
item.setId(((Number) attr[0]).longValue());
4511+
item.setDescription((String) attr[1]);
4512+
item.setExpirationDate((java.util.Date) attr[2]);
4513+
return item;
4514+
}
4515+
}));
4516+
...
4517+
}
45254518
----
45264519

45274520
You use the `SqlTypeValue` to pass in the value of a Java object like `TestItem` into a
@@ -4533,7 +4526,7 @@ the following example, or ``ArrayDescriptor``s.
45334526
[source,java,indent=0]
45344527
[subs="verbatim,quotes"]
45354528
----
4536-
final TestItem = new TestItem(123L, "A test item",
4529+
final TestItem testItem = new TestItem(123L, "A test item",
45374530
new SimpleDateFormat("yyyy-M-d").parse("2010-12-31"));
45384531
45394532
SqlTypeValue value = new AbstractSqlTypeValue() {
@@ -6544,7 +6537,6 @@ constructs a Spring application context, and calls these two methods.
65446537
import java.io.IOException;
65456538
import javax.xml.transform.stream.StreamResult;
65466539
import javax.xml.transform.stream.StreamSource;
6547-
65486540
import org.springframework.context.ApplicationContext;
65496541
import org.springframework.context.support.ClassPathXmlApplicationContext;
65506542
import org.springframework.oxm.Marshaller;

0 commit comments

Comments
 (0)