This week's book giveaway is in the Agile and other Processes forum.
We're giving away four copies of The Mikado Method and have Ola Ellnestam and Daniel Brolund on-line!
See this thread for details.
The moose likes JDBC and the fly likes Using Clobs in SQLServer- Please help! Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Databases » JDBC
Reply Bookmark "Using Clobs in SQLServer- Please help!" Watch "Using Clobs in SQLServer- Please help!" New topic
Author

Using Clobs in SQLServer- Please help!

Glen D'cruz
Greenhorn

Joined: Apr 22, 2004
Posts: 15
/* CAN ANYONE PLEASE HELP ME OUT WITH THIS PROGRAM. PLEASE READ THE COMPLETE
PROGRAM AND GIVE ME YOUR SUGESTIONS.

THE DATABASE BEING USED IS MS SQLSERVER

This program retreives the text file stored in the database installed
by the previous program SaveToClob() (Not displayed in this snippet).

The table created in the database consists of the following columns :=
static String SQLCommand =
"CREATE Table DOCUMENTS(" +
"MEMBERID INTEGER NOT NULL PRIMARY KEY, " +
"TITLE VARCHAR(20) NOT NULL , " +
"DOCUMENT TEXT NOT NULL " +
");";

Program SaveToClob sucessfully saved the following :=
values = 01, "GLENS_DOCUMENT", "EMPDOCUMENT"
Note: "DOCUMENT" is stored as a text file.


When I run the program retreiveFromClob (written below)to retreive the clob
I get an error which ia as follows:=

java.lang.UnsupportedOperationException
at sun.jdbc.odbc.JdbcOdbcResultSet.getClob(JdbcOdbcResultSet.java:4287)
at retreivefromclob.RetreiveFromClob.getFileFromClob(RetreiveFromClob.java:31)
at retreivefromclob.RetreiveFromClob.main(RetreiveFromClob.java:60)
Exception in thread "main"

It seems to me that MS SQLServer the database I am using doesent support
Clobs. If is the case is there any other way of retreiving a clob from this
database or what is the alternate name for it.

THANKS TO ALL.

*/
package retreivefromclob;

import java.io.*;
import java.sql.*;
import javax.sql.*;

public class RetreiveFromClob
{
public RetreiveFromClob()
{}


public byte[] getFileFromClob(int memberId)
{
String SQLQuery = "SELECT DOCUMENT FROM DOCUMENTS WHERE MEMBERID = ? ";
Clob clob = null;
String text = null;

try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con = DriverManager.getConnection("jdbc dbc cruz");
PreparedStatement pstmt = con.prepareStatement(SQLQuery);
pstmt.setInt(1, memberId);
ResultSet rs = pstmt.executeQuery();
ResultSetMetaData md = rs.getMetaData();
while (rs.next())
{
clob = rs.getClob("DOCUMENT"); // error identified

}
text = clob.getSubString(1, ( (int) clob.length()));

con.close();

}
catch (ClassNotFoundException clnfe)
{
clnfe.printStackTrace();
}
catch (SQLException e)
{
System.out.print("Error:" + e.getMessage());
}

byte[] bytes = null;
if (text != null)
{
bytes = text.getBytes();
}
return bytes;

}

public static void main(String[] args)
{
RetreiveFromClob retreiveFromClob1 = new RetreiveFromClob();
byte[] bytes = retreiveFromClob1.getFileFromClob(1);

System.out.print(bytes.toString());

}

}
 
I agree. Here's the link: http://zeroturnaround.com/jrebel - it saves me about five hours per week
 
subject: Using Clobs in SQLServer- Please help!
 
Similar Threads
Blob Clob
why won't clob open
PrintWriter does not write. Why?
how to handle data gotten from CLOBs?
Understanding Byte Data and Character Encoding