aspose file tools*
The moose likes JDBC and the fly likes Writing CLOB Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Databases » JDBC
Bookmark "Writing CLOB " Watch "Writing CLOB " New topic
Author

Writing CLOB

smitha rai
Ranch Hand

Joined: Aug 15, 2001
Posts: 33
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());
}
}
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61424
    
  67

Welcome to the Ranch smitha!
You'll find this forum a great place to seek help on JDBC, and there aren't many rules you'll have to worry about, but one is that proper names are required. Please take a look at the JavaRanch Naming Policy and change your display name to match it.
Thanks!
bear
JDBC Forum Bartender


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Writing CLOB