Thread:
 This works both for reading or writing with Inform... 
 ivaylodd   16 May 2008, 04:48 
 Re: This works both for reading or writing with... 
 Surya Mithra   22 Sep 2008, 06:44 

Comment
Prev. thread 
 Next thread
 
Prev. posting 
 Next posting
From: ivaylodd (16 May 2008, 04:48) Replies: 1, Views: 729
Subject: This works both for reading or writing with Informix 10...
=========================================================================
public class StringClobType implements UserType {

    private static final int READ_BUFFER_SIZE = 4096;

    public Object assemble(Serializable cached, Object owner) {
	return null;
    }

    public Object deepCopy(Object value) {
	if (value == null) {
	    return null;
	}
	return new String((String) value);
    }

    public Serializable disassemble(Object value) {
	return null;
    }

    public boolean equals(Object x, Object y) {
	return (x == y) || ((x != null) && (y != null) && (x.equals(y)));
    }

    public int hashCode(Object x) {
	return x.hashCode();
    }

    public boolean isMutable() {
	return false;
    }

    public Object nullSafeGet(ResultSet rs, String[] names, Object owner)
	    throws SQLException {
	Clob clob = rs.getClob(names[0]);

	Reader reader = clob.getCharacterStream();
	if (reader == null) {
	    return null;
	}

	StringBuffer sb = new StringBuffer();
	try {
	    char[] charbuf = new char[READ_BUFFER_SIZE];
	    for (int i = reader.read(charbuf); i > 0; i = reader.read(charbuf)) {
		sb.append(charbuf, 0, i);
	    }
	} catch (IOException e) {
	    throw new SQLException(e.getMessage());
	}
	return sb.toString();
    }

    public void nullSafeSet(PreparedStatement st, Object value, int index)
	    throws SQLException {
	if (value != null) {
	    StringReader r = new StringReader((String) value);
	    st.setCharacterStream(index, r, ((String) value).length());
	} else {
	    st.setNull(index, sqlTypes()[0]);
	}
    }

    public Object replace(Object original, Object target, Object owner) {
	return null;
    }

    public Class returnedClass() {
	return String.class;
    }

    public int[] sqlTypes() {
	return new int[] { Types.CLOB };
    }
=========================================================================

Note that in the nullSafeGet() method, the following code:

Reader reader = rs.getCharacterStream(names[0]);

doesn't work with Informix and throws an SQLException:
java.sql.SQLException: Can't convert tonull

Instead, I changed it to:

Clob clob = rs.getClob(names[0]);
Reader reader = clob.getCharacterStream();

and now it works.
Prev. thread 
 Next thread
 
Prev. posting 
 Next posting
© Copyright 2006, Red Hat Middleware, LLC. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc. [Privacy Policy]