wood burning stoves 2.0*
The moose likes JDBC and the fly likes Inserting and retrieving Blob data from SQL2000 using JDBC Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Databases » JDBC
Bookmark "Inserting and retrieving Blob data from SQL2000 using JDBC" Watch "Inserting and retrieving Blob data from SQL2000 using JDBC" New topic

Inserting and retrieving Blob data from SQL2000 using JDBC

Ravi Pydi

Joined: Sep 01, 2002
Posts: 25
Iam working on a task where i have to read a huge XML file and insert it into a single field in SQL Server 2000. The table contains 2 columns viz., ItemNo(varchar), Itemdata(text or ntext). After inserting the data, the same data is to be retrieved back and sent onto a standard output.
I have to use java.sql.Blob object in Java. Can somebody help me with a code sample.
Ravi Pydi
Praveen K Pippiri

Joined: Jun 17, 2002
Posts: 23
I am attaching sample code to write Clob(Character Stream)/Blob(Binary Stream) data into the database.I am using UDB so change the driver for your program.

import java.io.PrintStream;
import java.sql.*;
import java.io.*;
import java.text.*;
public class DocInsert
public static void main(String args[])

Connection con=null;
ResultSet rs=null;
PreparedStatement pstmt=null;
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
String publishDate = sdf.format(new java.util.Date());
sdf.applyPattern("yyyy-MM-dd HH:mm:ss");
String lastUpdateTS = sdf.format(new java.util.Date());
Timestamp ts=Timestamp.valueOf(lastUpdateTS);
//con = DriverManager.getConnection( "jdbc dbc:testDSN","","") ;
con = DriverManager.getConnection( "jdbc b2:sample","","") ;

String file=args[0];
File fileObj = new File(file);
FileInputStream fis = new FileInputStream(fileObj);
BufferedInputStream bis = new BufferedInputStream(fis);
byte buf[] = new byte[(int)fileObj.length()];
int noBytes=bis.read(buf,0,buf.length);


pstmt.setString(1, "12");
pstmt.setString(2, "12");
pstmt.setString(3, "12");
pstmt.setString(4, "12");
pstmt.setInt(5, 1);
pstmt.setString(6, "12");
pstmt.setString(7, "1");
pstmt.setString(8, "12");
pstmt.setString(9, "12");
pstmt.setString(10, "12");
pstmt.setString(11, "12");
pstmt.setString(12, "12");
pstmt.setString(13, "12");
pstmt.setDate(14, Date.valueOf(publishDate));
pstmt.setDate(15, Date.valueOf(publishDate));
pstmt.setInt(16, 1);
pstmt.setInt(17, 1);
pstmt.setTimestamp(18, ts);
pstmt.setString(19, "12");
pstmt.setAsciiStream(20, new ByteArrayInputStream(buf), buf.length);
//Use Binary Stream for Setting the BLOB Data Type
pstmt.setInt(21, buf.length);
int result =pstmt.executeUpdate();
System.out.println("Insert result" + result);
catch(Exception e1)
System.out.println("Exception e1" + e1.getMessage());

System.out.println("I am in finally");
catch(Exception e){
System.out.println("Exception in finally"+e.getMessage());


Praveen<br />Sun Certified Programmer for Java 2 Platform.
I agree. Here's the link: http://aspose.com/file-tools
subject: Inserting and retrieving Blob data from SQL2000 using JDBC
Similar Threads
Reguarding JDBC
Binary XML-- Any Decoder for it --
JavaDb creating table error
Help regarding insertion in the database
How Result Set can be upadated while we insert the data programmatcally