Problems with JDBCTemplate and CLOB insertion

7 篇文章 0 订阅
6 篇文章 0 订阅

当用spring jdbcTemplate执行merge语句时,总会出现”非法列类型提示“,通过google找到了以下这篇文章,目前没有采用以下这种方式,先记录下以备日后参考

转载自:http://forum.springsource.org/showthread.php?129285-Problems-with-JDBCTemplate-and-CLOB-insertion

Problems with JDBCTemplate and CLOB insertion

This is with Spring 3.0.5.RELEASE.

The code:
jdbcTemplate.update("MERGE INTO app_role_data x USING (select ? name, ? xml FROM dual) d ON (x.app_name = d.name) WHEN MATCHED THEN UPDATE SET x.xml_blob = d.xml WHEN NOT MATCHED THEN INSERT(app_name, xml_blob) VALUES(d.name, d.xml)",
                    new AbstractLobCreatingPreparedStatementCallback(lobHandler) {
                            protected void setValues(PreparedStatement ps, LobCreator lobCreator) throws SQLException {
                                    ps.setString(1, appName);
                                    lobCreator.setClobAsString(ps, 2, xmlBlob);
                            }
                    });

But note that this fails the same way with a simple INSERT on the clob column.

lobHandler is an instance of OracleLobHandler, injected via the context.

The table:
SQL> desc app_role_data
 Name                      Null?    Type
 ----------------------------------------- -------- ----------------------------
 APP_NAME                       VARCHAR2(64)
 XML_BLOB                       CLOB

The exception:

Caused by: java.sql.SQLException: Invalid column type
    at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:112)
    at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:146)
    at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:208)
    at oracle.jdbc.driver.OraclePreparedStatement.setObjectCritical(OraclePreparedStatement.java:9231)
    at oracle.jdbc.driver.OraclePreparedStatement.setObjectInternal(OraclePreparedStatement.java:8812)
    at oracle.jdbc.driver.OraclePreparedStatement.setObjectInternal(OraclePreparedStatement.java:9534)
    at oracle.jdbc.driver.OraclePreparedStatement.setObject(OraclePreparedStatement.java:9517)
    at org.springframework.jdbc.core.StatementCreatorUtils.setValue(StatementCreatorUtils.java:351)
    at org.springframework.jdbc.core.StatementCreatorUtils.setParameterValueInternal(StatementCreatorUtils.java:216)
    at org.springframework.jdbc.core.StatementCreatorUtils.setParameterValue(StatementCreatorUtils.java:144)
    at org.springframework.jdbc.core.ArgPreparedStatementSetter.doSetValue(ArgPreparedStatementSetter.java:65)
    at org.springframework.jdbc.core.ArgPreparedStatementSetter.setValues(ArgPreparedStatementSetter.java:46)
    at org.springframework.jdbc.core.JdbcTemplate$2.doInPreparedStatement(JdbcTemplate.java:815)
    at org.springframework.jdbc.core.JdbcTemplate$2.doInPreparedStatement(JdbcTemplate.java:1)
    at org.springframework.jdbc.core.JdbcTemplate.execute(JdbcTemplate.java:586)

WTF?


prashanthbs is offline Junior Member
Join Date
Mar 2009
Location
NH, USA
Posts
3

Default

Quote Originally Posted by  nsayer@kfu.com  View Post
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

以上为转载内容,本人采取的处理方式是将merge放入存储过程中,然后通过spring jdbctemplate直接调用存储过程


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值