Hi,
I am trying to insert a clob data and i am getting the error "ORA-01002: fetch out of sequence". Appreciate if some one could help me correcting my code. My code is as under:
Smitha
===========================================================================
package org.kp.fep2.purchasing;
import java.io.OutputStream;
import java.io.Reader;
import java.io.Writer;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import oracle.sql.CLOB;
import org.kp.j2ee.db.DBUtil;
public class WriteLOB
{
String c_strSQL
= "SELECT contents_clob FROM requisition_t WHERE requisition_t.req_ik = 2000001 FOR UPDATE";
public void writeCLOB()
{
String strTestData = "This is a sample text";
Connection oConn = null;
PreparedStatement pstmt = null;
Statement stmt = null;
ResultSet rsData = null;
char[] cbuf = new char[strTestData.length()];
strTestData.getChars(0, strTestData.length(), cbuf, 0);
try
{
oConn = getConnection();
stmt = oConn.createStatement();
rsData = stmt.executeQuery(c_strSQL);
rsData.next();
CLOB oClob = (CLOB) rsData.getClob(1);
Writer oClobWriter = oClob.getCharacterOutputStream();
oClobWriter.write(cbuf);
oClobWriter.close();
oConn.commit();
}
catch(Exception ex)
{
System.out.println("***************** Exception in writing data : " + ex.getMessage());
}
finally
{
DBUtil.closeAll(rsData, pstmt, oConn);
}
} //writeBLOB
public String readCLOB()
{
Connection oConn = null;
PreparedStatement pstmt = null;
ResultSet rsData = null;
StringBuffer sbData = null;
try
{
oConn = getConnection();
pstmt = oConn.prepareStatement(c_strSQL);
pstmt.setString(1, "2000001");
rsData = pstmt.executeQuery();
rsData.next();
CLOB oClob = (CLOB) rsData.getClob(1);
Reader oClobReader = oClob.getCharacterStream();
sbData = new StringBuffer();
int nChars = 0;
char[] cbuf = new char[10];
while((nChars = oClobReader.read(cbuf)) != -1)
{
sbData.append(cbuf, 0, nChars);
}
oClobReader.close();
}
catch(Exception ex)
{
System.out.println("***************** Exception in reading data : " + ex.getMessage());
}
finally
{
DBUtil.closeAll(rsData, pstmt, oConn);
}
return sbData.toString();
} //readCLOB
public Connection getConnection()
{
Connection oConn = null;
try
{
DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
oConn = DriverManager.getConnection ("jdbc
racle:thin:@8i", "scott", "tiger");
System.out.println("******** Connection established");
}
catch(Exception ex)
{
System.out.println("******** Connection failed");
}
return oConn;
}
public static void main(String[] args)
{
WriteLOB oLob = new WriteLOB();
oLob.writeCLOB();
System.out.println("**** the data is: " + oLob.readCLOB());
}
}