Article View: comp.lang.java.databases
Article #48Re: Problem with blob insertion
From: lacouc01
Date: Wed, 07 Mar 2001 20:07
Date: Wed, 07 Mar 2001 20:07
76 lines
3065 bytes
3065 bytes
hi I work with blob...
this is my code
//Connection and open session
Driver DriverRecordset1 = (Driver)Class.forName
(MM_lacouc01_DRIVER).newInstance();
Connection conn = DriverManager.getConnection
(MM_lacouc01_STRING,MM_lacouc01_USERNAME,MM_lacouc01_PASSWORD);
//httpMultipartRequest
session = request.getSession(true);
HttpServletRequest req = request;
HttpServletResponse res = response;
res.setContentType("text/html");
PrintWriter out1 = res.getWriter();
//Insert Blob
BLOB blob;
File binFile;
//FileInputStream instream;
InputStream instream;
OutputStream outstream;
//set autocommit to OFF
conn.setAutoCommit(false);
//Create Statement
Statement stmt = conn.createStatement();
//OracleStatement stmt = ((OracleStatement)
conn.createStatement());
//Insert an empty blob in the database
stmt.execute("INSERT INTO FORMSCONT VALUES('test2',
empty_BLOB())");
//Execute the query and lock the blob row
ResultSet rset = stmt.executeQuery("SELECT PDFFILE FROM
FORMSCONT WHERE FORMNBR = 'test2' FOR UPDATE");
rset.next();
//Get blob locator
blob = ((OracleResultSet)rset).getBLOB(1);
//Get the large binary txt file
//Prendre le document du bouton browser
//instream = req.getInputStream();
instream = req.getResourceAsStream("c:\mdsps.jpg");
//Insert to the BLOB from an output stream
outstream = blob.getBinaryOutputStream();
//Read the input stream and write the output stream by
chunks
byte[] chunk = new byte[blob.getChunkSize()];
int i = -1;
while((i = instream.read(chunk))!=-1)
{
outstream.write(chunk,0,i);
}
//Close the input and output stream.
instream.close();
outstream.close();
//Close the statement.
stmt.close();
conn.close();
good luck..
Caroline
Technical Details
Message-ID:
<00000056.58bdee6c@usw-ex0108-192.remarq.com>Path:
arc1-ca.pugleaf.net!supernews.google.com!sn-xit-03!sn-xit-01!sn-post-01!supernews.com!WReNclone!WReNphoon4.POSTED!WReN!not-for-mailReferences:
<985m87$5ua$1@reader1.fr.uu.net>Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: f74b3,c583775225220860 X-Google-Attributes: gidf74b3,public X-Google-Thread: 10a146,c583775225220860 X-Google-Attributes: gid10a146,public X-Google-Thread: 107d46,c583775225220860 X-Google-Attributes: gid107d46,public X-Google-ArrivalTime: 2001-03-07 12:20:08 PST Path: arc1-ca.pugleaf.net!supernews.google.com!sn-xit-03!sn-xit-01!sn-post-01!supernews.com!WReNclone!WReNphoon4.POSTED!WReN!not-for-mail From: lacouc01 <lacouc01NOlaSPAM@sun.partner.remarq.com.invalid> Newsgroups: comp.lang.java.databases,borland.public.jbuilder.java.language,comp.lang.java.programmer Subject: Re: Problem with blob insertion Date: Wed, 07 Mar 2001 12:07:29 -0800 Organization: http://www.remarq.com: The World's Usenet/Discussions Start Here Message-ID: <00000056.58bdee6c@usw-ex0108-192.remarq.com> X-Originating-Host: 199.202.216.21 Sender: caroline.lacoursiereNOcaSPAM@mdsps.com.invalid References: <985m87$5ua$1@reader1.fr.uu.net> Bytes: 1834 X-Wren-Trace: eHdTangjO39nOHZ8ImZWbXBqKWJzYSNvd2k2ZntubyVpPzR5dHczICI4ISIreTYxIDUpKQ== NNTP-Posting-Host: 10.0.2.192 X-Trace: WReNphoon4 983996239 10.0.2.192 (Wed, 07 Mar 2001 12:17:19 PST) X-Complaints-To: wrenabuse@remarq.com Lines: 77