1 package com.randomnoun.common.spring;
2
3
4
5
6
7 import java.io.*;
8 import java.sql.*;
9
10 import org.springframework.dao.TypeMismatchDataAccessException;
11 import org.springframework.jdbc.core.*;
12
13 import com.randomnoun.common.StreamUtil;
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39 public class StringRowMapper implements RowMapper<String>
40 {
41
42
43
44
45
46
47
48
49
50
51
52
53 public String mapRow(ResultSet resultSet, int rowNumber)
54 throws SQLException
55 {
56 Object value;
57
58 value = resultSet.getObject(1);
59
60 if (value == null) {
61
62 } else if (value.getClass().getName().equals("oracle.sql.CLOB")) {
63
64
65 throw new UnsupportedOperationException("Unexpected CLOB");
66 } else if (value instanceof java.sql.Clob) {
67 Clob clob = (Clob) value;
68 ByteArrayOutputStream baos = new ByteArrayOutputStream();
69 try {
70 StreamUtil.copyStream(clob.getAsciiStream(), baos, 1024);
71 } catch (IOException ioe) {
72 throw (SQLException) new SQLException("IO error transferring CLOB").initCause(ioe);
73 }
74 value = baos.toString();
75 } else if (!(value instanceof String)) {
76 throw new TypeMismatchDataAccessException(
77 "Expecting String/CLOB in column 1 of query (found " +
78 value.getClass().getName() + ")");
79 }
80
81 return (String)value;
82 }
83 }