
Originally Posted by
nsayer@kfu.com
This fails the same way with Spring 3.1.1.RELEASE.
And with the Oracle 11.2.0.2.0 JDBC driver.
So I am the only programmer on the entire planet that can't insert a CLOB into Oracle with Spring? Inconceivable.
No Frustration, Take it easy.
Well i have a simple test case as below to suit your inputs to show it works fine with the above .
Code:
CREATE TABLE app_role_data(
APP_NAME VARCHAR2(64) NOT NULL,
XML_CLOB CLOB NOT NULL
)
Get the spring configuration below
Code:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.1.xsd">
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource" >
<property name="driverClassName" value="oracle.jdbc.driver.OracleDriver"/>
<property name="url" value="Fill all the DB credentials "/>
<property name="username" value=""/>
<property name="password" value=""/>
</bean>
<bean id="lobHandler" class="org.springframework.jdbc.support.lob.OracleLobHandler">
<property name="nativeJdbcExtractor" ref="nativeJdbcExtractor"/>
</bean>
<bean id="nativeJdbcExtractor" class="org.springframework.jdbc.support.nativejdbc.CommonsDbcpNativeJdbcExtractor"/>
</beans>
Update the URL, User name, Password of your DB in the above configuration xml file.
Code:
import java.sql.PreparedStatement;
import java.sql.SQLException;
import javax.sql.DataSource;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.PreparedStatementSetter;
import org.springframework.jdbc.support.lob.LobHandler;
public class Main {
public static void main(String[] args) {
ApplicationContext ac = new ClassPathXmlApplicationContext("jdbc-config.xml", Main.class);
DataSource dataSource = (DataSource) ac.getBean("dataSource");
final LobHandler lobHandler = (LobHandler) ac.getBean("lobHandler");
JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
jdbcTemplate.update("MERGE INTO app_role_data x USING ( " +
" SELECT :1 APP_NAME, :2 XML_CLOB FROM DUAL) d ON " +
" (x.APP_NAME = d.APP_NAME) " +
" WHEN MATCHED THEN " +
" UPDATE SET x.XML_CLOB = d.XML_CLOB " +
" WHEN NOT MATCHED THEN INSERT (APP_NAME, XML_CLOB) " +
" VALUES (:3, :4)", new PreparedStatementSetter(){
public void setValues(PreparedStatement ps) throws SQLException {
ps.setString(1, "99");
lobHandler.getLobCreator().setClobAsString(ps, 2, "Updated Clob data");
ps.setString(3, "99");
lobHandler.getLobCreator().setClobAsString(ps, 4, "Inserted Clob data");
}
});
}
Just run the main class which would do the insert and updates as per your merge statement.
Prashanth