• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Writing CLOB

 
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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());
}
}
 
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
reply
    Bookmark Topic Watch Topic
  • New Topic