1) Convert CLOB to String using BufferedReader:
import java.io.BufferedReader;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
/**
* @author http://java-sample-program.blogspot.in/
* Convert CLOB to String using BufferedReader
*/
public class ClobTest {
public static void main(String[] args) throws Exception{
Connection con = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
con = DriverManager.getConnection("jdbc:oracle:thin:@IPAddress:Port:Database",
"username","password");
pstmt = con.prepareStatement("SELECT clobCol FROM TableName");
rs = pstmt.executeQuery();
while(rs.next()){
StringBuffer strOut = new StringBuffer();
String aux;
try {
BufferedReader br = new BufferedReader(rs.getClob("clobCol").
getCharacterStream());
while ((aux=br.readLine())!=null) {
strOut.append(aux);
strOut.append(System.getProperty("line.separator"));
}
}catch (Exception e) {
e.printStackTrace();
}
String clobStr = strOut.toString();
System.out.println(clobStr);
}
}catch(Exception e){
e.printStackTrace();
} finally {
try{
if(pstmt != null){
pstmt.close();
}
if(rs!= null){
rs.close();
}
if(con!= null){
con.close();
}
}catch (SQLException e) {
e.printStackTrace();
}
}
}
}
Use the above method if a very large Clob can be returned by the database.
2) Convert CLOB to String using getSubString() method:
import java.io.BufferedReader;
import java.sql.Clob;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
/**
* @author http://java-sample-program.blogspot.in/
* Convert CLOB to String using getSubString() method
*/
public class ClobTest {
public static void main(String[] args) throws Exception{
Connection con = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
con = DriverManager.getConnection("jdbc:oracle:thin:@IPAddress:Port:Database",
"username","password");
pstmt = con.prepareStatement("SELECT clobCol FROM TableName");
rs = pstmt.executeQuery();
while(rs.next()){
Clob clob = rs.getClob("clobCol");
if (clob != null) {
String clobStr = clob.getSubString(1, (int) clob.length());
System.out.println(clobStr);
}
}
}catch(Exception e){
e.printStackTrace();
} finally {
try{
if(pstmt != null){
pstmt.close();
}
if(rs!= null){
rs.close();
}
if(con!= null){
con.close();
}
}catch (SQLException e) {
e.printStackTrace();
}
}
}
}
Hi! How to update CLOB field using java JDBC. I need to write one method that can update a CLOB, field but I can't yet.
ReplyDeleteIt is a good website to learn Java. I am also writing many useful java programs for free learn. You can check them at http://www.javatheprogram.blogspot.com.
ReplyDeletethank you man the second method is what i needed for my jasper report.
ReplyDeletegetSubString
ReplyDeleteworked for me
thanks
getSubString also worked for me... thanks a lot
ReplyDeleteGreat Article I love to read your articles because your writing style is too good, its is very very helpful for all of us and I never get bored while reading your article because it becomes more and more interesting from the starting lines until the end. So Thank you for sharing a COOL Meaningful stuff with us Keep it up..!
ReplyDeleteSAP training in Chennai
googd
ReplyDeleteThis information really worth saying, i think you are master of the content and thank you so much sharing that valuable information and get new skills after refer that post.
ReplyDeleteInterior Decorators in Chennai
Home Interior Designers in Chennai
Home Interiors in Chennai
Words Doctorate is providing PhD Thesis Writing Services, Dissertation Writing Services & Research Paper Writing Services for PhD Students-all stream.
ReplyDeleteThesis writing services
Thesis editing services
PhD Thesis Writing Services
PhD Dissertation Writing Services
PhD Dissertation Editing Services
Dissertation Writing Services
Dissertation Editing Services
Research Paper Writing Services
This is very useful information about Java programming.
ReplyDeleteTop 10 Interior Designers in Chennai
Top Interior Design Companies in Chennai
Nice blog.Thank you...
ReplyDeleteTop Interior Decorators in Chennai
Top 10 Interior Decorators in Chennai