KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > jdbc > core > simple > SimpleJdbcTemplate


1 /*
2  * Copyright 2002-2005 the original author or authors.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.springframework.jdbc.core.simple;
18
19 import java.util.List JavaDoc;
20 import java.util.Map JavaDoc;
21
22 import javax.sql.DataSource JavaDoc;
23
24 import org.springframework.dao.DataAccessException;
25 import org.springframework.jdbc.core.JdbcOperations;
26 import org.springframework.jdbc.core.JdbcTemplate;
27
28 /**
29  * Java 5 and above wrapper for the classic Spring JdbcTemplate,
30  * taking advantage of varargs and autoboxing, and exposing only the
31  * most commonly required operations to simplify JdbcTemplate usage.
32  *
33  * <p>Use the <code>getJdbcOperations()</code> method if you need to invoke
34  * less commonly used methods. This includes any methods specifying SQL types,
35  * methods using less commonly used callbacks such as RowCallbackHandler,
36  * updates with PreparedStatementSetters, rather than arg lists, stored
37  * procedures and batch operations.
38  *
39  * @author Rod Johnson
40  * @author Rob Harrop
41  * @author Juergen Hoeller
42  * @since 2.0
43  * @see ParameterizedRowMapper
44  * @see SimpleJdbcDaoSupport
45  * @see org.springframework.jdbc.core.JdbcTemplate
46  */

47 public class SimpleJdbcTemplate implements SimpleJdbcOperations {
48     
49     /** The JdbcTemplate we are wrapping */
50     private final JdbcOperations classicJdbcTemplate;
51
52
53     /**
54      * Create a new SimpleJdbcTemplate for the given DataSource.
55      * <p>Creates a classic Spring JdbcTemplate and wraps it.
56      * @param dataSource the JDBC DataSource to access
57      */

58     public SimpleJdbcTemplate(DataSource JavaDoc dataSource) {
59         this.classicJdbcTemplate = new JdbcTemplate(dataSource);
60     }
61
62     /**
63      * Create a new SimpleJdbcTemplate for the given classic Spring JdbcTemplate.
64      * @param classicJdbcTemplate the classic Spring JdbcTemplate to wrap
65      */

66     public SimpleJdbcTemplate(JdbcOperations classicJdbcTemplate) {
67         this.classicJdbcTemplate = classicJdbcTemplate;
68     }
69
70     /**
71      * Expose the classic Spring JdbcTemplate to allow invocation of
72      * less commonly used methods.
73      */

74     public JdbcOperations getJdbcOperations() {
75         return this.classicJdbcTemplate;
76     }
77     
78     
79     public int queryForInt(String JavaDoc sql, Object JavaDoc... args) throws DataAccessException {
80         return args == null || args.length == 0 ?
81                     getJdbcOperations().queryForInt(sql) :
82                     getJdbcOperations().queryForInt(sql, args);
83     }
84     
85     public long queryForLong(String JavaDoc sql, Object JavaDoc... args) throws DataAccessException {
86         return (args == null || args.length == 0) ?
87                     getJdbcOperations().queryForLong(sql) :
88                     getJdbcOperations().queryForLong(sql, args);
89     }
90
91     @SuppressWarnings JavaDoc("unchecked")
92     public <T> T queryForObject(String JavaDoc sql, Class JavaDoc<T> requiredType, Object JavaDoc... args) throws DataAccessException {
93         return (T)((args == null || args.length == 0) ?
94                 getJdbcOperations().queryForObject(sql, requiredType) :
95                 getJdbcOperations().queryForObject(sql, args, requiredType));
96     }
97
98     @SuppressWarnings JavaDoc("unchecked")
99     public <T> T queryForObject(String JavaDoc sql, ParameterizedRowMapper<T> rm, Object JavaDoc... args) throws DataAccessException {
100         return (T)((args == null || args.length == 0)?
101                 getJdbcOperations().queryForObject(sql, rm):
102                 getJdbcOperations().queryForObject(sql, args, rm));
103     }
104     
105     @SuppressWarnings JavaDoc("unchecked")
106     public <T> List JavaDoc<T> query(String JavaDoc sql, ParameterizedRowMapper<T> rm, Object JavaDoc... args) throws DataAccessException {
107         return (List JavaDoc<T>)((args == null || args.length == 0) ?
108                 getJdbcOperations().query(sql, rm) :
109                 getJdbcOperations().query(sql, args, rm));
110     }
111
112     @SuppressWarnings JavaDoc("unchecked")
113     public Map JavaDoc<String JavaDoc, Object JavaDoc> queryForMap(String JavaDoc sql, Object JavaDoc... args) throws DataAccessException {
114         return (args == null || args.length == 0) ?
115                 getJdbcOperations().queryForMap(sql) :
116                 getJdbcOperations().queryForMap(sql, args);
117     }
118
119     @SuppressWarnings JavaDoc("unchecked")
120     public List JavaDoc<Map JavaDoc<String JavaDoc, Object JavaDoc>> queryForList(String JavaDoc sql, Object JavaDoc... args) throws DataAccessException {
121         return (args == null || args.length == 0) ?
122                 getJdbcOperations().queryForList(sql) :
123                 getJdbcOperations().queryForList(sql, args);
124     }
125
126     public int update(String JavaDoc sql, Object JavaDoc ... args) throws DataAccessException {
127         return (args == null || args.length == 0) ?
128                 getJdbcOperations().update(sql) :
129                 getJdbcOperations().update(sql, args);
130     }
131
132 }
133
Popular Tags