各位csdn的朋友,谁做过使用spring jdbc操作oracle的clob数据类型,能贴出来一些关键代码和配置文件么,谢谢了

解决方案 »

  1.   

    <?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:aop="http://www.springframework.org/schema/aop"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
    http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
    http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">    <bean id="dataSource"
         class="org.apache.commons.dbcp.BasicDataSource">
         <property name="driverClassName"
         value="oracle.jdbc.driver.OracleDriver">
         </property>
         <property name="url"
         value="jdbc:oracle:thin:@192.168.1.116:1521:orcl">
         </property>
         <property name="username" value="zxt"></property>
         <property name="password" value="1111"></property>
        </bean>
        <bean id="sessionFactory"
         class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
         <property name="dataSource">
         <ref bean="dataSource" />
         </property>
         <property name="hibernateProperties">
         <props>
         <prop key="hibernate.dialect">
         org.hibernate.dialect.Oracle9Dialect
         </prop>
         </props>
         </property>
         <property name="mappingResources">
         <list>
             <value>com/ssh2/model/Books.hbm.xml</value>
         <value>com/ssh2/model/Sort.hbm.xml</value>
         <value>com/ssh2/model/Press.hbm.xml</value>
         <value>com/ssh2/model/Bookshelf.hbm.xml</value>
         <value>com/ssh2/model/Readers.hbm.xml</value>
         <value>com/ssh2/model/Borrow.hbm.xml</value>
                    <value>com/ssh2/model/RightToolbar.hbm.xml</value>
                    <value>com/ssh2/model/RightToolbarSort.hbm.xml</value>
         <value>com/ssh2/model/User.hbm.xml</value>
         </list>
         </property>
        </bean>
        
        </beans>
    这个是spring与hibernate结合的spring配置文件
      

  2.   

    就是 用jdbc操作clob数据吧 ,
    和spring关系不打,/**
       * 功能:
       *     clob更新
       * @param sqlInsert
       * @param sqlSelect
       * @param strBlobName
       * @param infile
       * @throws Exception
       */
      public void BlobUpdate(String sqlInsert, String sqlSelect,
                             String strBlobName, String infile) throws
          Exception {
        boolean defaultCommit = connection.getAutoCommit(); /* 设定不自动提交 */
        connection.setAutoCommit(false);
        try {
          stmt.executeUpdate(sqlInsert);
          /* 插入一个空的BLOB对象 "INSERT INTO TEST_BLOB VALUES ('222', EMPTY_BLOB())"*/
          ResultSet rs = stmt.executeQuery(sqlSelect);
              /* 查询此BLOB对象并锁定 "SELECT BLOBCOL FROM TEST_BLOB WHERE ID='222' FOR UPDATE"*/while (
              rs.next()) {
            oracle.sql.BLOB blob = (oracle.sql.BLOB) rs.getBlob(strBlobName);
            /* 取出此BLOB对象"BLOBCOL" */
            BufferedOutputStream out = new BufferedOutputStream(blob.
                getBinaryOutputStream()); /* 向BLOB对象中写入数据 */
            BufferedInputStream in = new BufferedInputStream(new
                FileInputStream(infile));
            int c;
            while ( (c = in.read()) != -1) {
              out.write(c);
            }
            in.close();
            out.close();
          }
          connection.commit(); /* 正式提交 */
        }
        catch (SQLException eSQL) {
          connection.rollback(); /* 出错回滚 */
    //      eSQL.printStackTrace();
          System.out.println("在BLob更新出现如下异常:" + eSQL.getMessage());
          log.WriteFile(eSQL.getMessage() + "   ;sql=" + sqlInsert);
          log.WriteFile(eSQL.getMessage() + "   ;sql=" + sqlSelect);
        }
        connection.setAutoCommit(defaultCommit); /* 恢复原提交状态 */
      }  /**
       * 功能:
       *     clob查询
       * @param sql
       * @param strBlobName
       * @param outfile
       * @throws Exception
       */
      public void BlobQuery(String sql, String strBlobName, String outfile) throws
          Exception {
        boolean defaultCommit = connection.getAutoCommit(); /* 设定不自动提交 */
        connection.setAutoCommit(false);
        try {
          ResultSet rs = stmt.executeQuery(sql); //"SELECT BLOBCOL FROM TEST_BLOB WHERE ID='222'" /* 查询BLOB对象 */
          while (rs.next()) {
            oracle.sql.BLOB blob = (oracle.sql.BLOB) rs.getBlob(strBlobName);
            /* 取出此BLOB对象 */
            BufferedOutputStream out = new BufferedOutputStream(new
                FileOutputStream(outfile)); /* 以二进制形式输出 */
            BufferedInputStream in = new BufferedInputStream(blob.getBinaryStream());
            int c;
            while ( (c = in.read()) != -1) {
              out.write(c);
            }
            in.close();
            out.close();
          }
          connection.commit(); /* 正式提交 */
        }
        catch (SQLException eSQL) {
          connection.rollback(); /* 出错回滚 */
    // eSQL.printStackTrace();
          System.out.println("在BLob查询出现如下异常:" + eSQL.getMessage());
          log.WriteFile(eSQL.getMessage() + "   ;sql=" + sql);
        }
        connection.setAutoCommit(defaultCommit); /* 恢复原提交状态 */
      }