java存取clob字段时遇到的问题

03-05-28 geng615

//数据库oracle8.16;jdk1.31;

//处理clob字段时,无论存取,都报SQLException。“ORA-01002 :读取违反顺序”;

//使用表(testclob)结构 id number,clobobj clob;只有两个字段;

//下面注释中说明了异常出现的地方;

import java.sql.*;

import java.io.*;

public class TestClob {

public static void main(String args[]){

String serverName = "192.168.2.142";

String sDBDriver = "oracle.jdbc.driver.OracleDriver";

String sConnStr = "jdbc:oracle:thin:@"+serverName+":1521:gdwx";

Connection conn = null;

Statement stmt = null;

ResultSet rs = null;

try{

Class.forName(sDBDriver);

conn = DriverManager.getConnection(sConnStr, "scott", "tiger");

String sqli = "insert into TestClob values(clob_seq.nextval,empty_clob())";

String sqls = "select * from TestClob where id = (select max(id) from TestClob) for update";

stmt = conn.createStatement();

stmt.execute(sqli);

stmt.execute(sqls);

/*

stmt.execute(sqls);错误就是在这一行出现的

*/

rs = stmt.getResultSet();

if(rs.next()){

oracle.sql.CLOB clob = (oracle.sql.CLOB)rs.getClob(2);

Writer writer = clob.getCharacterOutputStream() ;

writer.write("yesyesyesyesyesyesyesyesyesyesyesyesyesyesyesyesyes");

writer.flush() ;

writer.close() ;

}

}catch(Exception e ){

System.out.println(e.getMessage());

}finally{

if(conn!=null){

try{

conn.close();

}catch(Exception ee){

System.out.println(ee);

}

}

}

}

}