CLOB + java

原文:
[url]http://www.experts-exchange.com/Programming/Languages/Java/Q_20274035.html?cid=335[/url]

Title: CLOB + java
Bookmark:
Question: I am trying to use the sample code I found in the oracle doc's for to update my db. However, although it looks like it is updating the clob - when I check the db it is not updated see below code.

**********************************************************

[code]import java.sql.*; // line 1
import java.io.*;
import java.util.*;

// Importing the Oracle Jdbc driver package
// makes the code more readable
import oracle.jdbc.driver.*;

// Import this to get CLOB and BLOB classes
import oracle.sql.*;

public class test1
{
public static void main (String args [])
throws Exception
{
// Load the Oracle JDBC driver
DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());

// Connect to the database. You can put a database
// name after the @ sign in the connection URL.
Connection conn =
DriverManager.getConnection ("jdbc:oracle:oci8:@dev", "IS5_DEV", "IS5_DEV");

// It's faster when auto commit is off
conn.setAutoCommit (false); // line 26

// Create a Statement
Statement stmt = conn.createStatement ();
/*
try
{
stmt.execute ("DROP TABLE basic_lob_table");
}
catch (SQLException e)
{
// An exception could be raised here if the table did
// not exist already but we gleefully ignore it
} // line 38

// Create a table containing a BLOB and a CLOB line 40
stmt.execute ("CREATE TABLE basic_lob_table (x varchar2 (30),
b blob, c clob)");

// Populate the table
stmt.execute ("INSERT INTO basic_lob_table VALUES ('one',
'010101010101010101010101010101', 'onetwothreefour')");
stmt.execute ("INSERT INTO basic_lob_table VALUES ('two',
'0202020202020202020202020202', 'twothreefourfivesix')");
// line 49
System.out.println ("Dumping lobs");
*/
// Select the lobs
ResultSet rset = stmt.executeQuery ("SELECT * FROM is_ui_layout FOR UPDATE");
while (rset.next ())
{
// Get the lobs

CLOB clob = ((OracleResultSet)rset).getCLOB (6);

// Print the lob contents
//dumpClob (conn, clob);

// Change the lob contents
fillClob (conn, clob, 2000);
}
// line 68
System.out.println ("Dumping lobs again");

rset = stmt.executeQuery ("SELECT * FROM is_ui_layout");
while (rset.next ())
{
// Get the lobs

CLOB clob = ((OracleResultSet)rset).getCLOB (6);

// Print the lobs contents

dumpClob (conn, clob);
}
} // line 82

// Utility function to dump Clob contents
static void dumpClob (Connection conn, CLOB clob)
throws Exception
{
// get character stream to retrieve clob data
Reader instream = clob.getCharacterStream();

// create temporary buffer for read line 91
char[] buffer = new char[10];

// length of characters read
int length = 0;

// fetch data line 98
while ((length = instream.read(buffer)) != -1)
{
System.out.print("Read " + length + " chars: ");

for (int i=0; i<length; i++)
System.out.print(buffer[i]);
System.out.println();
}

// Close input stream
instream.close();
} // line 108

// line 135
// Utility function to put data in a Clob
static void fillClob (Connection conn, CLOB clob, long length)
throws Exception
{
Writer outstream = clob.getCharacterOutputStream();

int i = 0;
int chunk = 10;

while (i < length)
{
outstream.write(i + "hello world", 0, chunk); // line 147

i += chunk;
if (length - i < chunk)
chunk = (int) length - i;
}
outstream.close();
} // line 154

}[/code]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值