ibatis-user-java mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From soussou97 <dbousse...@yahoo.fr>
Subject RE: Scanning byte array
Date Mon, 02 Oct 2006 22:14:34 GMT

Hi Christopher;

OK but I must only read the  byte[] or write it also in using 
ByteArrayOutputStream or both (read & write) ?

Thank's


Christopher.Mathrusse wrote:
> 
> 
> 
> 
> 
> 
> Once you have a byte[] you can read it using a ByteArrayInputStream and
> you can write it using ByteArrayOutputStream. 
> You should look at the JavaDocs to learn more about these 
> classes. 
> &nbsp; 
> For reading the byte[]&nbsp;you could, for example, do the 
> following: 
> &nbsp; 
> 
> java.sql.Connection connection = this.getConnection(); 
> java.sql.PreparedStatement statement = 
> connection.prepareStatement("select data from 
> DATASTORE where ID = ?"); 
> statement.setObject(1, primaryKey); 
> byte[] documentData = null; 
> 
> java.sql.ResultSet result = statement.executeQuery(); 
> if 
> (result.next()) { 
> documentData = result.getBytes(1); 
> } 
> 
> java.io.ByteArrayInputStream bis = new 
> java.io.ByteArrayInputStream(documentData); 
> 
> java.io.BufferedReader in 
> = new 
> java.io.BufferedReader(new java.io.InputStreamReader(bis)); 
> 
> String str = null; 
> do 
> { 
> str = in.readLine(); 
> // do something 
> here 
> } while(str != 
> null); 
> &nbsp; 
> 
> 
> From: soussou97 &lt;dboussebha@yahoo.fr&gt; 
> [mailto:soussou97 &lt;dboussebha@yahoo.fr&gt;] Sent: Monday, October 
> 02, 2006 1:20 AM To: user-java@ibatis.apache.org Subject: 
> RE: Scanning byte array 
> Hi Christopher ; OK for your example but imagined that 
> you do not have of documents but of data, so firstly to get the data out
> of 
> the table as into the example : connection = this.getConnection(); 
> statement = connection.prepareStatement("select data from DATASTORE 
> where ID = ?"); statement.setObject(1, primaryKey); byte[] documentData 
> = null; ResultSet result = statement.executeQuery(); if (result.next()) 
> { Data = result.getBytes(1); } Next you make how to extract the 
> data inside of array (which can be : integer, string, etc...) ? 
> Regards; Christopher.Mathrusse wrote: &gt; &gt; 
> &gt; &gt; &gt; &gt; &gt; When I was working with Oracle and 
> needed to store XML &gt; documents in the database I defined the column
> type 
> as LONG RAW. Then I &gt; was able &gt; to stream the data into the table 
> by using the following: &gt; &nbsp; &gt; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;

> PreparedStatement &gt; statement = connection.prepareStatement("insert
> into 
> DOCUMENTSTORE &gt; values(?,?)"); &gt; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;

> ByteArrayInputStream &gt; stream = new &gt; 
> ByteArrayInputStream(documentData); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
&gt; 
> statement.setBinaryStream(1, stream, &gt; documentData.length); &gt; To 
> get the data out of the&nbsp;table I &gt; used the following: &gt; 
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; connection = &gt; this.getConnection();

> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; statement = &gt; 
> connection.prepareStatement("select DOCUMENT from DOCUMENTSTORE where ID = 
> &gt; ?"); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; statement.setObject(1,
&gt; 
> primaryKey); &gt; &nbsp; &gt; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;byte[]

> documentData &gt; = null; &gt; &nbsp; &gt; 
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ResultSet result = &gt; 
> statement.executeQuery(); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if &gt;

> (result.next()) &gt; { &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;

> documentData = &gt; result.getBytes(1); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;

> &gt; } &gt; &nbsp; &gt; &nbsp; &gt; &gt; &gt; From: 
> soussou97 &lt;dboussebha@yahoo.fr&gt; &gt; [mailto:soussou97 
> &lt;dboussebha@yahoo.fr&gt;] Sent: Friday, &gt; September 29, 2006 5:16 AM

> To: &gt; user-java@ibatis.apache.org Subject: Scanning byte &gt; array 
> &gt; Hi; I have a method which return a byte array from a BLOB &gt; 
> (Oracle) : public static byte[] getBLOB(int id, Connection conn) throws
> &gt; 
> Exception { ResultSet rs = null; PreparedStatement pstmt = null; &gt;
> String 
> query = "SELECT data FROM Table1 WHERE id = ?"; try { pstmt = &gt; 
> conn.prepareStatement(query); pstmt.setInt(1, id); rs = &gt; 
> pstmt.executeQuery(); rs.next(); Blob blob = rs.getBlob(1); return &gt; 
> blob.getBytes(1, (int) blob.length()); } finally { rs.close(); &gt; 
> pstmt.close(); conn.close(); } } } I must to &gt; scan the byte array 
> position by position for extracting of the data &gt; contained into this 
> array for a display in using a GUI. Which is the best &gt; (performance) 
> solution to implemente this ? Regards; -- &gt; View this message in
> context: 
> &gt; http://www.nabble.com/Scanning-byte-array-tf2356548.html#a6563833
> Sent 
> &gt; from &gt; the iBATIS - User - Java mailing list archive at 
> Nabble.com. &gt; &gt; &gt; &gt; -- View this message 
> in context:
> http://www.nabble.com/Scanning-byte-array-tf2356548.html#a6597501 
> Sent from the iBATIS - User - Java mailing list archive at Nabble.com. 
> 
> 
> 
> 

-- 
View this message in context: http://www.nabble.com/Scanning-byte-array-tf2356548.html#a6610758
Sent from the iBATIS - User - Java mailing list archive at Nabble.com.


Mime
View raw message