KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > versant > core > jdbc > sql > conv > BytesConverter


1
2 /*
3  * Copyright (c) 1998 - 2005 Versant Corporation
4  * All rights reserved. This program and the accompanying materials
5  * are made available under the terms of the Eclipse Public License v1.0
6  * which accompanies this distribution, and is available at
7  * http://www.eclipse.org/legal/epl-v10.html
8  *
9  * Contributors:
10  * Versant Corporation - initial API and implementation
11  */

12 package com.versant.core.jdbc.sql.conv;
13
14 import com.versant.core.jdbc.JdbcConverter;
15 import com.versant.core.jdbc.JdbcConverterFactory;
16 import com.versant.core.jdbc.JdbcTypeRegistry;
17 import com.versant.core.jdbc.metadata.JdbcColumn;
18
19 import javax.jdo.JDOFatalDataStoreException; //todo: appears only in throws clause
20
import java.sql.PreparedStatement JavaDoc;
21 import java.sql.SQLException JavaDoc;
22 import java.sql.ResultSet JavaDoc;
23
24 /**
25  * This converter converts byte[] to and from SQL. It assumes the data is
26  * accessable using ResultSet.getBytes and PreparedStatement.setBytes.
27  */

28 public class BytesConverter extends JdbcConverterBase {
29
30     public static class Factory extends NoArgJdbcConverterFactory {
31
32         private BytesConverter converter;
33
34         /**
35          * Create a converter for col using props as parameters. Return null if
36          * no converter is required.
37          */

38         public JdbcConverter createJdbcConverter(JdbcColumn col, Object JavaDoc args,
39                 JdbcTypeRegistry jdbcTypeRegistry) {
40             if (converter == null) converter = new BytesConverter();
41             return converter;
42         }
43
44     }
45
46     /**
47      * Get the value of col from rs at position index.
48      * @exception SQLException on SQL errors
49      * @exception JDOFatalDataStoreException if the ResultSet value is invalid
50      */

51     public Object JavaDoc get(ResultSet JavaDoc rs, int index, JdbcColumn col)
52             throws SQLException JavaDoc, JDOFatalDataStoreException {
53         return rs.getBytes(index);
54     }
55
56     /**
57      * Set parameter index on ps to value (for col).
58      * @exception SQLException on SQL errors
59      * @exception JDOFatalDataStoreException if value is invalid
60      */

61     public void set(PreparedStatement JavaDoc ps, int index, JdbcColumn col, Object JavaDoc value)
62             throws SQLException JavaDoc, JDOFatalDataStoreException {
63         if (value == null) {
64             ps.setNull(index, col.jdbcType);
65         } else {
66             ps.setBytes(index, (byte[])value);
67         }
68     }
69
70     /**
71      * Get the type of our expected value objects (e.g. java.util.Locale
72      * for a converter for Locale's).
73      */

74     public Class JavaDoc getValueType() {
75         return byte[].class;
76     }
77
78 }
79
Popular Tags