Skip to content

Commit 2818051

Browse files
committed
Revised code examples for stored procedure type declarations
Issue: SPR-16811 (cherry picked from commit 765d18e)
1 parent a3bcdbe commit 2818051

File tree

1 file changed

+32
-40
lines changed

1 file changed

+32
-40
lines changed

src/docs/asciidoc/data-access.adoc

Lines changed: 32 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -2982,7 +2982,6 @@ in the primitive wrapper classes explicitly or using auto-boxing.
29822982
[subs="verbatim,quotes"]
29832983
----
29842984
import javax.sql.DataSource;
2985-
29862985
import org.springframework.jdbc.core.JdbcTemplate;
29872986
29882987
public class ExecuteAnUpdate {
@@ -4023,7 +4022,7 @@ data from the `t_actor` relation to an instance of the `Actor` class.
40234022
40244023
public ActorMappingQuery(DataSource ds) {
40254024
super(ds, "select id, first_name, last_name from t_actor where id = ?");
4026-
super.declareParameter(new SqlParameter("id", Types.INTEGER));
4025+
declareParameter(new SqlParameter("id", Types.INTEGER));
40274026
compile();
40284027
}
40294028
@@ -4044,7 +4043,7 @@ for this customer query takes the `DataSource` as the only parameter. In this
40444043
constructor you call the constructor on the superclass with the `DataSource` and the SQL
40454044
that should be executed to retrieve the rows for this query. This SQL will be used to
40464045
create a `PreparedStatement` so it may contain place holders for any parameters to be
4047-
passed in during execution.You must declare each parameter using the `declareParameter`
4046+
passed in during execution. You must declare each parameter using the `declareParameter`
40484047
method passing in an `SqlParameter`. The `SqlParameter` takes a name and the JDBC type
40494048
as defined in `java.sql.Types`. After you define all parameters, you call the
40504049
`compile()` method so the statement can be prepared and later executed. This class is
@@ -4097,9 +4096,7 @@ class since it can easily be parameterized by setting SQL and declaring paramete
40974096
[subs="verbatim"]
40984097
----
40994098
import java.sql.Types;
4100-
41014099
import javax.sql.DataSource;
4102-
41034100
import org.springframework.jdbc.core.SqlParameter;
41044101
import org.springframework.jdbc.object.SqlUpdate;
41054102
@@ -4178,9 +4175,7 @@ output parameter, in this case only one, using the parameter name as the key.
41784175
import java.util.Date;
41794176
import java.util.HashMap;
41804177
import java.util.Map;
4181-
41824178
import javax.sql.DataSource;
4183-
41844179
import org.springframework.beans.factory.annotation.Autowired;
41854180
import org.springframework.jdbc.core.SqlOutParameter;
41864181
import org.springframework.jdbc.object.StoredProcedure;
@@ -4227,14 +4222,13 @@ Oracle REF cursors).
42274222
[source,java,indent=0]
42284223
[subs="verbatim,quotes"]
42294224
----
4225+
import java.util.HashMap;
4226+
import java.util.Map;
4227+
import javax.sql.DataSource;
42304228
import oracle.jdbc.OracleTypes;
42314229
import org.springframework.jdbc.core.SqlOutParameter;
42324230
import org.springframework.jdbc.object.StoredProcedure;
42334231
4234-
import javax.sql.DataSource;
4235-
import java.util.HashMap;
4236-
import java.util.Map;
4237-
42384232
public class TitlesAndGenresStoredProcedure extends StoredProcedure {
42394233
42404234
private static final String SPROC_NAME = "AllTitlesAndGenres";
@@ -4264,12 +4258,10 @@ the supplied `ResultSet`:
42644258
[source,java,indent=0]
42654259
[subs="verbatim,quotes"]
42664260
----
4267-
import org.springframework.jdbc.core.RowMapper;
4268-
42694261
import java.sql.ResultSet;
42704262
import java.sql.SQLException;
4271-
42724263
import com.foo.domain.Title;
4264+
import org.springframework.jdbc.core.RowMapper;
42734265
42744266
public final class TitleMapper implements RowMapper<Title> {
42754267
@@ -4288,12 +4280,10 @@ the supplied `ResultSet`.
42884280
[source,java,indent=0]
42894281
[subs="verbatim,quotes"]
42904282
----
4291-
import org.springframework.jdbc.core.RowMapper;
4292-
42934283
import java.sql.ResultSet;
42944284
import java.sql.SQLException;
4295-
42964285
import com.foo.domain.Genre;
4286+
import org.springframework.jdbc.core.RowMapper;
42974287
42984288
public final class GenreMapper implements RowMapper<Genre> {
42994289
@@ -4311,17 +4301,15 @@ delegate to the superclass' untyped `execute(Map parameters)` method (which has
43114301
[source,java,indent=0]
43124302
[subs="verbatim,quotes"]
43134303
----
4314-
import oracle.jdbc.OracleTypes;
4315-
import org.springframework.jdbc.core.SqlOutParameter;
4316-
import org.springframework.jdbc.core.SqlParameter;
4317-
import org.springframework.jdbc.object.StoredProcedure;
4318-
4319-
import javax.sql.DataSource;
4320-
43214304
import java.sql.Types;
43224305
import java.util.Date;
43234306
import java.util.HashMap;
43244307
import java.util.Map;
4308+
import javax.sql.DataSource;
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;
43254313
43264314
public class TitlesAfterDateStoredProcedure extends StoredProcedure {
43274315
@@ -4416,6 +4404,7 @@ dependency injection.
44164404
final File clobIn = new File("large.txt");
44174405
final InputStream clobIs = new FileInputStream(clobIn);
44184406
final InputStreamReader clobReader = new InputStreamReader(clobIs);
4407+
44194408
jdbcTemplate.execute(
44204409
"INSERT INTO lob_table (id, a_clob, a_blob) VALUES (?, ?, ?)",
44214410
new AbstractLobCreatingPreparedStatementCallback(lobHandler) { # <1>
@@ -4426,6 +4415,7 @@ dependency injection.
44264415
}
44274416
}
44284417
);
4418+
44294419
blobIs.close();
44304420
clobReader.close();
44314421
----
@@ -4512,21 +4502,24 @@ declaration of an `SqlOutParameter`.
45124502
[source,java,indent=0]
45134503
[subs="verbatim,quotes"]
45144504
----
4515-
final TestItem = new TestItem(123L, "A test item",
4516-
new SimpleDateFormat("yyyy-M-d").parse("2010-12-31"));
4505+
public class TestItemStoredProcedure extends StoredProcedure {
45174506
4518-
declareParameter(new SqlOutParameter("item", OracleTypes.STRUCT, "ITEM_TYPE",
4519-
new SqlReturnType() {
4520-
public Object getTypeValue(CallableStatement cs, int colIndx, int sqlType, String typeName) throws SQLException {
4521-
STRUCT struct = (STRUCT) cs.getObject(colIndx);
4522-
Object[] attr = struct.getAttributes();
4523-
TestItem item = new TestItem();
4524-
item.setId(((Number) attr[0]).longValue());
4525-
item.setDescription((String) attr[1]);
4526-
item.setExpirationDate((java.util.Date) attr[2]);
4527-
return item;
4528-
}
4529-
}));
4507+
public TestItemStoredProcedure(DataSource dataSource) {
4508+
...
4509+
declareParameter(new SqlOutParameter("item", OracleTypes.STRUCT, "ITEM_TYPE",
4510+
new SqlReturnType() {
4511+
public Object getTypeValue(CallableStatement cs, int colIndx, int sqlType, String typeName) throws SQLException {
4512+
STRUCT struct = (STRUCT) cs.getObject(colIndx);
4513+
Object[] attr = struct.getAttributes();
4514+
TestItem item = new TestItem();
4515+
item.setId(((Number) attr[0]).longValue());
4516+
item.setDescription((String) attr[1]);
4517+
item.setExpirationDate((java.util.Date) attr[2]);
4518+
return item;
4519+
}
4520+
}));
4521+
...
4522+
}
45304523
----
45314524

45324525
You use the `SqlTypeValue` to pass in the value of a Java object like `TestItem` into a
@@ -4538,7 +4531,7 @@ the following example, or ``ArrayDescriptor``s.
45384531
[source,java,indent=0]
45394532
[subs="verbatim,quotes"]
45404533
----
4541-
final TestItem = new TestItem(123L, "A test item",
4534+
final TestItem testItem = new TestItem(123L, "A test item",
45424535
new SimpleDateFormat("yyyy-M-d").parse("2010-12-31"));
45434536
45444537
SqlTypeValue value = new AbstractSqlTypeValue() {
@@ -6257,7 +6250,6 @@ constructs a Spring application context, and calls these two methods.
62576250
import java.io.IOException;
62586251
import javax.xml.transform.stream.StreamResult;
62596252
import javax.xml.transform.stream.StreamSource;
6260-
62616253
import org.springframework.context.ApplicationContext;
62626254
import org.springframework.context.support.ClassPathXmlApplicationContext;
62636255
import org.springframework.oxm.Marshaller;

0 commit comments

Comments
 (0)