KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > test > cmp2 > lob > GetSetObjectParameter


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.test.cmp2.lob;
23
24 import java.sql.PreparedStatement JavaDoc;
25 import java.sql.SQLException JavaDoc;
26 import java.sql.Types JavaDoc;
27 import java.sql.ResultSet JavaDoc;
28 import java.sql.Blob JavaDoc;
29 import java.io.IOException JavaDoc;
30
31 import org.jboss.ejb.plugins.cmp.jdbc.JDBCParameterSetter;
32 import org.jboss.ejb.plugins.cmp.jdbc.JDBCResultSetReader;
33 import org.jboss.logging.Logger;
34 import org.jboss.invocation.MarshalledValue;
35
36 /**
37  @author Scott.Stark@jboss.org
38  @version $Revision: 37393 $
39  */

40 public class GetSetObjectParameter
41    implements JDBCParameterSetter, JDBCResultSetReader
42 {
43    public Object JavaDoc get(ResultSet JavaDoc rs, int index, Class JavaDoc destination, Logger log) throws SQLException JavaDoc
44    {
45       Object JavaDoc value = rs.getObject(index);
46       log.debug("get: i=" + index + ", destination="+destination+", value=" + value);
47       Object JavaDoc result = value;
48       if(rs.wasNull())
49       {
50          result = null;
51       }
52       else if( value instanceof MarshalledValue )
53       {
54          MarshalledValue mv = (MarshalledValue) value;
55          try
56          {
57             result = mv.get();
58          }
59          catch (Exception JavaDoc e)
60          {
61             SQLException JavaDoc sqe = new SQLException JavaDoc("Unable to extract MarshalledValue");
62             sqe.initCause(e);
63             throw sqe;
64          }
65       }
66       return result;
67    }
68
69    public void set(PreparedStatement JavaDoc ps, int index, int jdbcType, Object JavaDoc value,
70       Logger log) throws SQLException JavaDoc
71    {
72       log.debug("set: i=" + index + ", jdbcType="+jdbcType+", value=" + value);
73       if( value instanceof MarshalledValue )
74       {
75          MarshalledValue mv = (MarshalledValue) value;
76          try
77          {
78             value = mv.get();
79          }
80          catch (Exception JavaDoc e)
81          {
82             SQLException JavaDoc sqe = new SQLException JavaDoc("Unable to extract MarshalledValue");
83             sqe.initCause(e);
84             throw sqe;
85          }
86       }
87
88       switch( jdbcType )
89       {
90          case Types.BLOB:
91          {
92             ps.setObject(index, value, jdbcType);
93             break;
94          }
95          case Types.OTHER:
96             ps.setObject(index, value);
97             break;
98          default:
99             throw new SQLException JavaDoc("Unsupported jdbcType: "+jdbcType);
100       }
101    }
102 }
103
Popular Tags