2012-01-11 77 views
0

我有像(7A + 5dfAA7D ...)字母和数字的数据。其长度大于two lack个字符。所以我无法使用update更新该clob列,因为它会抛出一个错误,指出字符串文字太长。如何使用sql oracle更新批量数据的CLOB列?

如何更新值?

请帮帮我。

在此先感谢!

+0

你在做什么样的更新:overwrsubiting?字符串替换?追加? – APC 2012-01-11 13:09:10

+0

实际上它是空列使用简单更新查询简单更新 – 2012-01-11 13:11:35

+0

您将需要使用PreparedStatement(或者编程语言为您提供的任何等效事物) – 2012-01-11 13:27:05

回答

0

这是一些将代码中的数据插入CLOB列的java代码的一部分。诀窍是首先插入一个empty_clob()值,然后更新记录。

try { 
      /* Register the Oracle driver */ 
      DriverManager.registerDriver(new OracleDriver()); 

      /* Establish a connection to the Oracle database. I have used the Oracle Thin driver. 
       jdbc:oracle:[email protected]:port:sid, "user name", "password" */ 

      conn = DriverManager.getConnection(connectString, userName, passWord); 

      /* Set auto commit to false, it helps to speed up things, by default JDBC's auto commit feature is on. 
       This means that each SQL statement is commited as it is executed. */ 

      conn.setAutoCommit(false); 
      stmt = conn.createStatement(); 

      /* Insert all the data, for the BLOB column we use the function empty_blob(), which creates a locator for the BLOB datatype. 
       A locator is an object that ponts to the actual location of the BLOB data in the database. A locator is essential to manipulate BLOB data. */ 

      query = "INSERT INTO CLOB_TABLE (id,data_clob,bestandsnaam,dt_geplaatst) " + 
        "VALUES(" + ID + ", empty_clob(),\'" + fileName + "\',sysdate)"; 

      //System.out.println(query); 

      stmt.execute(query); 

      /* Once the locator has been inserted, we retrieve the locator by executing a SELECT statement 
       with the FOR UPDATE clause to manually lock the row. */ 

      query = "SELECT DATA_CLOB FROM CLOB_TABLE WHERE ID=\'" + ID + "\' FOR UPDATE"; 

      //System.out.println(query); 
      rs = stmt.executeQuery(query); 
      //System.out.println("Select statement uitgevoerd"); 

      if (rs.next()) { 

       /* Once a locator has been retrieved we can use it to insert the binary data into the database. */ 
       CLOB clob = (CLOB)((OracleResultSet)rs).getClob(1); 
       os = clob.getAsciiOutputStream(); 
       final File f = new File(fileName); 
       is = new FileInputStream(f); 
       final byte[] buffer = new byte[clob.getBufferSize()]; 
       int bytesRead = 0; 
       while ((bytesRead = is.read(buffer)) != -1) { 
        os.write(buffer, 0, bytesRead); 
       } 
       clob = null; 
       returnValue = 0; 
      } 


     } catch 
0

您可以使用SQL * Loader来加载数据,但您必须从文件中进行。这可能会也可能并不困难,具体取决于涉及多少数据,尽管您可能会编写一个脚本来设置包含数据的顺序文件。

对于表名为TEST,与和TEST_BLOB为test_id列:

Control.dat: 

load data 
infile data.dat 
replace 
into table TEST 
Fields terminated by ',' 
(
    TEST_ID, 
    lob_file FILLER CHAR, 
    TEST_CLOB LOBFILE(lob_file) TERMINATED BY EOF 
) 

data.dat: 

1,c:\work\clob1_data.dat 
2,c:\work\clob2_data.dat 
etc... 

C:\Work>sqlldr USER/[email protected] control=control.txt 
sqlldr USER/[email protected] control=control.txt 
SQL*Loader: Release 10.2.0.1.0 - Production on Thu Sep 24 01:20:06 2009 
Copyright (c) 1982, 2005, Oracle. All rights reserved. 
Commit point reached - logical record count 2 

这个配置在从这个例子得出:Ask Tom