Java Code Examples for java.sql.PreparedStatement.setClob()

The following are Jave code examples for showing how to use setClob() of the java.sql.PreparedStatement class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.
Example 1
Project: lams   File: OracleLobHandler.java   Source Code and License Vote up 8 votes
@Override
public void setClobAsCharacterStream(
		PreparedStatement ps, int paramIndex, final Reader characterStream, int contentLength)
	throws SQLException {

	if (characterStream != null) {
		Clob clob = (Clob) createLob(ps, true, new LobCallback() {
			@Override
			public void populateLob(Object lob) throws Exception {
				Method methodToInvoke = lob.getClass().getMethod("getCharacterOutputStream", (Class[]) null);
				Writer writer = (Writer) methodToInvoke.invoke(lob, (Object[]) null);
				FileCopyUtils.copy(characterStream, writer);
			}
		});
		ps.setClob(paramIndex, clob);
		if (logger.isDebugEnabled()) {
			logger.debug("Set character stream for Oracle CLOB with length " + clob.length());
		}
	}
	else {
		ps.setClob(paramIndex, (Clob) null);
		logger.debug("Set Oracle CLOB to null");
	}
}
 
Example 2
Project: lams   File: TemporaryLobCreator.java   Source Code and License Vote up 7 votes
@Override
public void setClobAsString(PreparedStatement ps, int paramIndex, String content)
		throws SQLException {

	Clob clob = ps.getConnection().createClob();
	clob.setString(1, content);

	this.temporaryClobs.add(clob);
	ps.setClob(paramIndex, clob);

	if (logger.isDebugEnabled()) {
		logger.debug(content != null ? "Copied string into temporary CLOB with length " + content.length() :
				"Set CLOB to null");
	}
}
 
Example 3
Project: lams   File: TemporaryLobCreator.java   Source Code and License Vote up 7 votes
@Override
public void setClobAsAsciiStream(
		PreparedStatement ps, int paramIndex, InputStream asciiStream, int contentLength)
		throws SQLException {

	Clob clob = ps.getConnection().createClob();
	try {
		FileCopyUtils.copy(asciiStream, clob.setAsciiStream(1));
	}
	catch (IOException ex) {
		throw new DataAccessResourceFailureException("Could not copy into LOB stream", ex);
	}

	this.temporaryClobs.add(clob);
	ps.setClob(paramIndex, clob);

	if (logger.isDebugEnabled()) {
		logger.debug(asciiStream != null ?
				"Copied ASCII stream into temporary CLOB with length " + contentLength :
				"Set CLOB to null");
	}
}
 
Example 4
Project: lams   File: TemporaryLobCreator.java   Source Code and License Vote up 7 votes
@Override
public void setClobAsCharacterStream(
		PreparedStatement ps, int paramIndex, Reader characterStream, int contentLength)
		throws SQLException {

	Clob clob = ps.getConnection().createClob();
	try {
		FileCopyUtils.copy(characterStream, clob.setCharacterStream(1));
	}
	catch (IOException ex) {
		throw new DataAccessResourceFailureException("Could not copy into LOB stream", ex);
	}

	this.temporaryClobs.add(clob);
	ps.setClob(paramIndex, clob);

	if (logger.isDebugEnabled()) {
		logger.debug(characterStream != null ?
				"Copied character stream into temporary CLOB with length " + contentLength :
				"Set CLOB to null");
	}
}
 
Example 5
Project: lams   File: OracleLobHandler.java   Source Code and License Vote up 7 votes
@Override
public void setClobAsString(PreparedStatement ps, int paramIndex, final String content)
	throws SQLException {

	if (content != null) {
		Clob clob = (Clob) createLob(ps, true, new LobCallback() {
			@Override
			public void populateLob(Object lob) throws Exception {
				Method methodToInvoke = lob.getClass().getMethod("getCharacterOutputStream", (Class[]) null);
				Writer writer = (Writer) methodToInvoke.invoke(lob, (Object[]) null);
				FileCopyUtils.copy(content, writer);
			}
		});
		ps.setClob(paramIndex, clob);
		if (logger.isDebugEnabled()) {
			logger.debug("Set string for Oracle CLOB with length " + clob.length());
		}
	}
	else {
		ps.setClob(paramIndex, (Clob) null);
		logger.debug("Set Oracle CLOB to null");
	}
}
 
Example 6
Project: lams   File: OracleLobHandler.java   Source Code and License Vote up 7 votes
@Override
public void setClobAsAsciiStream(
		PreparedStatement ps, int paramIndex, final InputStream asciiStream, int contentLength)
	throws SQLException {

	if (asciiStream != null) {
		Clob clob = (Clob) createLob(ps, true, new LobCallback() {
			@Override
			public void populateLob(Object lob) throws Exception {
				Method methodToInvoke = lob.getClass().getMethod("getAsciiOutputStream", (Class[]) null);
				OutputStream out = (OutputStream) methodToInvoke.invoke(lob, (Object[]) null);
				FileCopyUtils.copy(asciiStream, out);
			}
		});
		ps.setClob(paramIndex, clob);
		if (logger.isDebugEnabled()) {
			logger.debug("Set ASCII stream for Oracle CLOB with length " + clob.length());
		}
	}
	else {
		ps.setClob(paramIndex, (Clob) null);
		logger.debug("Set Oracle CLOB to null");
	}
}
 
Example 7
Project: jaffa-framework   File: TypeDefs.java   Source Code and License Vote up 7 votes
/** Sets a parameter in the PreparedStatement.
 * @param engineType The engine type as defined in init.xml
 * @param pstmt The PreparedStatement.
 * @param parameterIndex The index of the parameter that is to be set.
 * @param value The object to be assigned to the parameter.
 * @throws SQLException if a database access error occurs.
 */
public void setAppObject(PreparedStatement pstmt, int parameterIndex, Object value, String engineType)
throws SQLException {
    if (value != null) {
        if (!(value instanceof String))
            value = DataTypeMapper.instance().map(value, String.class);
        if ("oracle".equalsIgnoreCase(engineType) && !supportsStdLob(pstmt)) {
            Clob clob = createClob(pstmt.getConnection(), (String) value);
            pstmt.setClob(parameterIndex, clob);
        } else {
            String str = (String) value;
            Reader reader = new BufferedReader(new StringReader(str));
            pstmt.setCharacterStream(parameterIndex, reader, str.length());
        }
    } else
        pstmt.setNull(parameterIndex, getSqlType(Defaults.CLOB, engineType));
}
 
Example 8
Project: the-vigilantes   File: StatementRegressionTest.java   Source Code and License Vote up 6 votes
public void testNullClob() throws Exception {
    createTable("testNullClob", "(field1 TEXT NULL)");

    PreparedStatement pStmt = null;

    try {
        pStmt = this.conn.prepareStatement("INSERT INTO testNullClob VALUES (?)");
        pStmt.setClob(1, null);
        pStmt.executeUpdate();
    } finally {
        if (pStmt != null) {
            pStmt.close();
        }
    }
}
 
Example 9
Project: OpenVertretung   File: StatementRegressionTest.java   Source Code and License Vote up 6 votes
public void testNullClob() throws Exception {
    createTable("testNullClob", "(field1 TEXT NULL)");

    PreparedStatement pStmt = null;

    try {
        pStmt = this.conn.prepareStatement("INSERT INTO testNullClob VALUES (?)");
        pStmt.setClob(1, null);
        pStmt.executeUpdate();
    } finally {
        if (pStmt != null) {
            pStmt.close();
        }
    }
}
 
Example 10
Project: lams   File: DefaultLobHandler.java   Source Code and License Vote up 6 votes
@Override
public void setClobAsString(PreparedStatement ps, int paramIndex, String content)
		throws SQLException {

	if (streamAsLob) {
		if (content != null) {
			ps.setClob(paramIndex, new StringReader(content), content.length());
		}
		else {
			ps.setClob(paramIndex, (Clob) null);
		}
	}
	else if (wrapAsLob) {
		if (content != null) {
			ps.setClob(paramIndex, new PassThroughClob(content));
		}
		else {
			ps.setClob(paramIndex, (Clob) null);
		}
	}
	else {
		ps.setString(paramIndex, content);
	}
	if (logger.isDebugEnabled()) {
		logger.debug(content != null ? "Set string for CLOB with length " + content.length() :
				"Set CLOB to null");
	}
}
 
Example 11
Project: lams   File: DefaultLobHandler.java   Source Code and License Vote up 6 votes
@Override
public void setClobAsAsciiStream(
		PreparedStatement ps, int paramIndex, InputStream asciiStream, int contentLength)
		throws SQLException {

	if (streamAsLob) {
		if (asciiStream != null) {
			try {
				ps.setClob(paramIndex, new InputStreamReader(asciiStream, "US-ASCII"), contentLength);
			}
			catch (UnsupportedEncodingException ex) {
				throw new SQLException("US-ASCII encoding not supported: " + ex);
			}
		}
		else {
			ps.setClob(paramIndex, (Clob) null);
		}
	}
	else if (wrapAsLob) {
		if (asciiStream != null) {
			ps.setClob(paramIndex, new PassThroughClob(asciiStream, contentLength));
		}
		else {
			ps.setClob(paramIndex, (Clob) null);
		}
	}
	else {
		ps.setAsciiStream(paramIndex, asciiStream, contentLength);
	}
	if (logger.isDebugEnabled()) {
		logger.debug(asciiStream != null ? "Set ASCII stream for CLOB with length " + contentLength :
				"Set CLOB to null");
	}
}
 
Example 12
Project: lams   File: DefaultLobHandler.java   Source Code and License Vote up 6 votes
@Override
public void setClobAsCharacterStream(
		PreparedStatement ps, int paramIndex, Reader characterStream, int contentLength)
		throws SQLException {

	if (streamAsLob) {
		if (characterStream != null) {
			ps.setClob(paramIndex, characterStream, contentLength);
		}
		else {
			ps.setClob(paramIndex, (Clob) null);
		}
	}
	else if (wrapAsLob) {
		if (characterStream != null) {
			ps.setClob(paramIndex, new PassThroughClob(characterStream, contentLength));
		}
		else {
			ps.setClob(paramIndex, (Clob) null);
		}
	}
	else {
		ps.setCharacterStream(paramIndex, characterStream, contentLength);
	}
	if (logger.isDebugEnabled()) {
		logger.debug(characterStream != null ? "Set character stream for CLOB with length " + contentLength :
				"Set CLOB to null");
	}
}
 
Example 13
Project: lams   File: ClobTypeDescriptor.java   Source Code and License Vote up 6 votes
@Override
            public <X> BasicBinder<X> getClobBinder(final JavaTypeDescriptor<X> javaTypeDescriptor) {
	return new BasicBinder<X>( javaTypeDescriptor, this ) {
		@Override
		protected void doBind(PreparedStatement st, X value, int index, WrapperOptions options)
				throws SQLException {
			st.setClob( index, javaTypeDescriptor.unwrap( value, Clob.class, options ) );
		}
	};
}
 
Example 14
Project: QDrill   File: PreparedStatementTest.java   Source Code and License Vote up 6 votes
/** Tests that "not supported" has priority over possible "type not supported"
 *  check. */
@Test( expected = SQLFeatureNotSupportedException.class )
public void testParamSettingWhenUnsupportedTypeSaysUnsupported() throws SQLException {
  PreparedStatement prepStmt = connection.prepareStatement( "VALUES 1" );
  try {
    prepStmt.setClob( 2, (Clob) null );
  }
  catch ( final SQLFeatureNotSupportedException e ) {
    assertThat(
        "Check whether params.-unsupported wording changed or checks changed.",
        e.toString(), PARAMETERS_NOT_SUPPORTED_MSG_MATCHER );
    throw e;
  }
}
 
Example 15
Project: ProyectoPacientes   File: StatementRegressionTest.java   Source Code and License Vote up 6 votes
public void testNullClob() throws Exception {
    createTable("testNullClob", "(field1 TEXT NULL)");

    PreparedStatement pStmt = null;

    try {
        pStmt = this.conn.prepareStatement("INSERT INTO testNullClob VALUES (?)");
        pStmt.setClob(1, null);
        pStmt.executeUpdate();
    } finally {
        if (pStmt != null) {
            pStmt.close();
        }
    }
}
 
Example 16
Project: BibliotecaPS   File: StatementRegressionTest.java   Source Code and License Vote up 6 votes
public void testNullClob() throws Exception {
    createTable("testNullClob", "(field1 TEXT NULL)");

    PreparedStatement pStmt = null;

    try {
        pStmt = this.conn.prepareStatement("INSERT INTO testNullClob VALUES (?)");
        pStmt.setClob(1, null);
        pStmt.executeUpdate();
    } finally {
        if (pStmt != null) {
            pStmt.close();
        }
    }
}