aspose file tools*
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 EJB 3 in Action this week in the EJB and other Java EE Technologies 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
Author

Inserting and retrieving Blob data from SQL2000 using JDBC

Ravi Pydi
Greenhorn

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
Greenhorn

Joined: Jun 17, 2002
Posts: 23
Hi,
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);
ts.setNanos(1);
try
{
Class.forName("COM.ibm.db2.jdbc.app.DB2Driver");
//Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
//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 =con.prepareStatement("INSERT INTO EAIM.DOC_CURVER_TBL (DOC_ID,DOC_NAME,DOC_CAT,DOC_AUTHOR,DOC_VERSION_NO,DOC_FORMAT_TYPE ,DOC_STATUS ,DOC_KEYWORDS,DOC_ABSTRACT,DOC_CONTACT_PER_NAME,DOC_CONTACT_PER_EMAIL,DOC_STD_OWNER,DOC_STD_DEL,DOC_PUBLISH_DT,DOC_TARGET_REV_DT,DOC_LENGTH,DOC_ACCESS_LEVEL,DOC_LAST_UPDATE_TS,DOC_LAST_UPDATE_USER_ID,CHAPTER_INFO,CHAPTER_INFO_LEN) VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)");

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());
e1.printStackTrace();
}
finally
{

System.out.println("I am in finally");
try
{
if(pstmt!=null)
pstmt.close();
//rs.close();
if(con!=null)
con.close();
}
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
How Result Set can be upadated while we insert the data programmatcally
JavaDb creating table error
Binary XML-- Any Decoder for it --
Help regarding insertion in the database