2012-01-11 103 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