• 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

Inserting and retrieving Blob data from SQL2000 using JDBC

 
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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());
}
}

}
}
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic