01: package vqwiki.db;
02:
03: import java.io.IOException;
04: import java.io.Reader;
05: import java.io.Writer;
06: import java.lang.reflect.InvocationTargetException;
07: import java.lang.reflect.Method;
08: import java.sql.Clob;
09: import java.sql.SQLException;
10:
11: /*
12: * Created on Aug 14, 2003
13: * @author Ernst Jan Plugge <<a href="mailto:rmc@dds.nl">rmc@dds.nl</a>>
14: */
15: public class OracleClobHelper {
16:
17: /**
18: *
19: */
20: public static String getClobValue(Clob clob) throws SQLException,
21: IOException {
22: if (clob == null)
23: return "";
24: StringBuffer sb = new StringBuffer();
25: char buffer[] = new char[4096];
26: Reader r = clob.getCharacterStream();
27: while (true) {
28: int n = r.read(buffer);
29: if (n == -1)
30: break;
31: sb.append(buffer, 0, n);
32: }
33: r.close();
34: return new String(sb);
35: }
36:
37: /**
38: *
39: */
40: public static void setClobValue(Clob clob, String value)
41: throws SQLException, IOException, ClassNotFoundException,
42: SecurityException, NoSuchMethodException,
43: IllegalArgumentException, IllegalAccessException,
44: InvocationTargetException {
45: // Use Reflection here to avoid a compile time dependency
46: // on the Oracle JDBC driver.
47: Class oracleClobClass = Class.forName("oracle.sql.CLOB");
48: Method m = oracleClobClass.getMethod(
49: "getCharacterOutputStream", new Class[] {});
50: Writer w = (Writer) m.invoke(clob, new Object[] {});
51: w.write(value);
52: w.close();
53: }
54: }
|