KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > ejb > plugins > cmp > jdbc > JDBCParameterSetter


1 /*
2 * JBoss, Home of Professional Open Source
3 * Copyright 2005, JBoss Inc., and individual contributors as indicated
4 * by the @authors tag. See the copyright.txt in the distribution for a
5 * full listing of individual contributors.
6 *
7 * This is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU Lesser General Public License as
9 * published by the Free Software Foundation; either version 2.1 of
10 * the License, or (at your option) any later version.
11 *
12 * This software is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this software; if not, write to the Free
19 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21 */

22 package org.jboss.ejb.plugins.cmp.jdbc;
23
24 import org.jboss.logging.Logger;
25
26 import java.sql.PreparedStatement JavaDoc;
27 import java.sql.SQLException JavaDoc;
28 import java.io.StringReader JavaDoc;
29 import java.io.ByteArrayInputStream JavaDoc;
30 import java.math.BigDecimal JavaDoc;
31
32 /**
33  * Implementations of this interface are used to set java.sql.PreparedStatement parameters.
34  *
35  * @author <a HREF="mailto:alex@jboss.org">Alexey Loubyansky</a>
36  * @version <tt>$Revision: 37459 $</tt>
37  */

38 public interface JDBCParameterSetter
39 {
40    /**
41     * Sets a parameter of a specific JDBC type.
42     * @param ps the java.sql.PreparedStatement to set parameter on
43     * @param index the index of the parameter
44     * @param jdbcType the JDBC type of the parameter as defined by java.sql.Types
45     * @param value parameter value
46     * @param log the logger
47     * @throws SQLException
48     */

49    void set(PreparedStatement JavaDoc ps, int index, int jdbcType, Object JavaDoc value, Logger log) throws SQLException JavaDoc;
50
51    abstract class JDBCAbstractParameterSetter implements JDBCParameterSetter
52    {
53       public void set(PreparedStatement JavaDoc ps, int index, int jdbcType, Object JavaDoc value, Logger log)
54          throws SQLException JavaDoc
55       {
56          if(log.isTraceEnabled())
57          {
58             log.trace("param: " +
59                "i=" + index + ", " +
60                "type=" + JDBCUtil.getJDBCTypeName(jdbcType) + ", " +
61                "value=" + ((value == null) ? "NULL" : value));
62          }
63
64          if(value == null)
65          {
66             ps.setNull(index, jdbcType);
67          }
68          else
69          {
70             value = JDBCUtil.coerceToSQLType(jdbcType, value);
71             setNotNull(ps, index, jdbcType, value, log);
72          }
73       }
74
75       protected abstract void setNotNull(PreparedStatement JavaDoc ps, int index, int jdbcType, Object JavaDoc value, Logger log)
76          throws SQLException JavaDoc;
77    }
78
79    /**
80     * Types.CLOB, Types.LONGVARCHAR.
81     */

82    JDBCParameterSetter CLOB = new JDBCAbstractParameterSetter()
83    {
84       protected void setNotNull(PreparedStatement JavaDoc ps, int index, int jdbcType, Object JavaDoc value, Logger log)
85          throws SQLException JavaDoc
86       {
87          String JavaDoc string = value.toString();
88          ps.setCharacterStream(index, new StringReader JavaDoc(string), string.length());
89       }
90    };
91
92    /**
93     * Types.BINARY, Types.VARBINARY.
94     */

95    JDBCParameterSetter BINARY = new JDBCAbstractParameterSetter()
96    {
97       protected void setNotNull(PreparedStatement JavaDoc ps, int index, int jdbcType, Object JavaDoc value, Logger log)
98          throws SQLException JavaDoc
99       {
100          byte[] bytes = JDBCUtil.convertObjectToByteArray(value);
101          ps.setBytes(index, bytes);
102       }
103    };
104
105    /**
106     * Types.BLOB, Types.LONGVARBINARY.
107     */

108    JDBCParameterSetter BLOB = new JDBCAbstractParameterSetter()
109    {
110       protected void setNotNull(PreparedStatement JavaDoc ps, int index, int jdbcType, Object JavaDoc value, Logger log)
111          throws SQLException JavaDoc
112       {
113          byte[] bytes = JDBCUtil.convertObjectToByteArray(value);
114          ps.setBinaryStream(index, new ByteArrayInputStream JavaDoc(bytes), bytes.length);
115       }
116    };
117
118    /**
119     * Types.DECIMAL, Types.NUMERIC
120     */

121    JDBCParameterSetter NUMERIC = new JDBCAbstractParameterSetter()
122    {
123       protected void setNotNull(PreparedStatement JavaDoc ps, int index, int jdbcType, Object JavaDoc value, Logger log)
124          throws SQLException JavaDoc
125       {
126          if(value instanceof BigDecimal JavaDoc)
127          {
128             ps.setBigDecimal(index, (BigDecimal JavaDoc)value);
129          }
130          else
131          {
132             ps.setObject(index, value, jdbcType, 0);
133          }
134       }
135    };
136
137    /**
138     * Types.JAVA_OBJECT, Types.OTHER, Types.STRUCT
139     */

140    JDBCParameterSetter OBJECT = new JDBCAbstractParameterSetter()
141    {
142       protected void setNotNull(PreparedStatement JavaDoc ps, int index, int jdbcType, Object JavaDoc value, Logger log)
143          throws SQLException JavaDoc
144       {
145          ps.setObject(index, value, jdbcType);
146       }
147    };
148 }
149
Popular Tags