ibatis-user-java mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Christopher.Mathru...@sybase.com
Subject RE: Scanning byte array
Date Mon, 02 Oct 2006 15:37:44 GMT
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML><HEAD>
<META http-equiv=Content-Type content="text/html; charset=us-ascii" plain; text 
Content-Type: 7bit Content-Transfer-Encoding:>
<META content="MSHTML 6.00.2900.2963" name=GENERATOR></HEAD>
<BODY>
<DIV dir=ltr align=left><SPAN class=718550515-02102006><FONT color=#0000ff

size=2>Once you have a byte[] you can read it using a <FONT 
size=2>ByteArrayInputStream and you can write it using ByteArrayOutputStream. 
You should look at the JavaDocs to learn more about these 
classes.</FONT></FONT></SPAN></DIV>
<DIV dir=ltr align=left><SPAN class=718550515-02102006><FONT color=#0000ff

size=2><FONT size=2></FONT></FONT></SPAN>&nbsp;</DIV>
<DIV dir=ltr align=left><SPAN class=718550515-02102006><FONT color=#0000ff

size=2><FONT size=2>For reading the byte[]&nbsp;you could, for example, do the

following:</FONT></FONT></SPAN></DIV>
<DIV dir=ltr align=left><SPAN class=718550515-02102006><FONT color=#0000ff

size=2></FONT></SPAN>&nbsp;</DIV>
<DIV dir=ltr align=left><SPAN class=718550515-02102006><FONT size=2>
<P align=left>java.sql.Connection connection = </FONT><B><FONT color=#7f0055

size=2>this</B></FONT><FONT size=2>.getConnection();</FONT></P>
<P align=left><FONT size=2>java.sql.PreparedStatement statement = 
connection.prepareStatement(</FONT><FONT color=#2a00ff size=2>"select data from

DATASTORE where ID = ?"</FONT><FONT size=2>); </P>
<P align=left>statement.setObject(1, primaryKey); </P>
<P align=left></FONT><B><FONT color=#7f0055 size=2>byte</B></FONT><FONT

size=2>[] documentData = </FONT><B><FONT color=#7f0055 
size=2>null</B></FONT><FONT size=2>; </P>
<P align=left></P>
<P align=left>java.sql.ResultSet result = statement.executeQuery(); </P>
<P align=left></FONT><B><FONT color=#7f0055 size=2>if</B></FONT><FONT
size=2> 
(result.next()) { </P>
<P align=left>documentData = result.getBytes(1); </P>
<P align=left>} </P>
<P align=left></P>
<P align=left>java.io.ByteArrayInputStream bis = </FONT><B><FONT color=#7f0055

size=2>new</B></FONT><FONT size=2> 
java.io.ByteArrayInputStream(documentData);</P>
<P align=left></P>
<P align=left>java.io.BufferedReader in</P>
<P align=left>= </FONT><B><FONT color=#7f0055 size=2>new</B></FONT><FONT
size=2> 
java.io.BufferedReader(</FONT><B><FONT color=#7f0055 size=2>new</B></FONT><FONT

size=2> java.io.InputStreamReader(bis));</P>
<P align=left></P>
<P align=left>String str = </FONT><B><FONT color=#7f0055 
size=2>null</B></FONT><FONT size=2>;</P>
<P align=left></FONT><B><FONT color=#7f0055 size=2>do</B></FONT><FONT
size=2> 
{</P>
<P align=left>str = in.readLine();</P>
<P align=left></FONT><FONT color=#3f7f5f size=2>// do something 
here</P></FONT><FONT size=2>
<P>} </FONT><B><FONT color=#7f0055 size=2>while</B></FONT><FONT
size=2>(str != 
</FONT><B><FONT color=#7f0055 size=2>null</B></FONT><FONT

size=2>);</P></FONT></SPAN></DIV>
<DIV dir=ltr align=left><FONT color=#0000ff size=2></FONT><BR>&nbsp;</DIV>
<DIV class=OutlookMessageHeader lang=en-us dir=ltr align=left>
<HR tabIndex=-1>
<FONT face=Tahoma size=2><B>From:</B> soussou97 &lt;dboussebha@yahoo.fr&gt;

[mailto:soussou97 &lt;dboussebha@yahoo.fr&gt;] <BR><B>Sent:</B>
Monday, October 
02, 2006 1:20 AM<BR><B>To:</B> user-java@ibatis.apache.org<BR><B>Subject:</B>

RE: Scanning byte array<BR></FONT><BR></DIV>
<DIV></DIV><BR>Hi Christopher ; <BR><BR>OK for your example
but imagined that 
you do not have of documents but of <BR>data, so firstly to get the data out of 
the table as into the example : <BR><BR>connection = this.getConnection(); 
<BR>statement = connection.prepareStatement("select data from <BR>DATASTORE 
where ID = ?"); <BR>statement.setObject(1, primaryKey); <BR>byte[] documentData

= null; <BR>ResultSet result = statement.executeQuery(); <BR>if (result.next())

{ <BR>Data = result.getBytes(1); <BR>} <BR><BR>Next you make how to
extract the 
data inside of array (which can be : <BR>integer, string, etc...) ? 
<BR><BR>Regards; <BR><BR><BR>Christopher.Mathrusse wrote: <BR>&gt;
<BR>&gt; 
<BR>&gt; <BR>&gt; <BR>&gt; <BR>&gt; <BR>&gt;
When I was working with Oracle and 
needed to store XML <BR>&gt; documents in the database I defined the column type

as LONG RAW. Then I <BR>&gt; was able <BR>&gt; to stream the data into
the table 
by using the following: <BR>&gt; &nbsp; <BR>&gt; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;

PreparedStatement <BR>&gt; statement = connection.prepareStatement("insert into

DOCUMENTSTORE <BR>&gt; values(?,?)"); <BR>&gt; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;

ByteArrayInputStream <BR>&gt; stream = new <BR>&gt; 
ByteArrayInputStream(documentData); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <BR>&gt;

statement.setBinaryStream(1, stream, <BR>&gt; documentData.length); <BR>&gt;
To 
get the data out of the&nbsp;table I <BR>&gt; used the following: <BR>&gt;

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; connection = <BR>&gt; this.getConnection();

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

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

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

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

<BR>&gt; } <BR>&gt; &nbsp; <BR>&gt; &nbsp; <BR>&gt;
<BR>&gt; <BR>&gt; From: 
soussou97 &lt;dboussebha@yahoo.fr&gt; <BR>&gt; [mailto:soussou97 
&lt;dboussebha@yahoo.fr&gt;] Sent: Friday, <BR>&gt; September 29, 2006 5:16
AM 
To: <BR>&gt; user-java@ibatis.apache.org Subject: Scanning byte <BR>&gt;
array 
<BR>&gt; Hi; I have a method which return a byte array from a BLOB <BR>&gt;

(Oracle) : public static byte[] getBLOB(int id, Connection conn) throws <BR>&gt;

Exception { ResultSet rs = null; PreparedStatement pstmt = null; <BR>&gt; String

query = "SELECT data FROM Table1 WHERE id = ?"; try { pstmt = <BR>&gt; 
conn.prepareStatement(query); pstmt.setInt(1, id); rs = <BR>&gt; 
pstmt.executeQuery(); rs.next(); Blob blob = rs.getBlob(1); return <BR>&gt; 
blob.getBytes(1, (int) blob.length()); } finally { rs.close(); <BR>&gt; 
pstmt.close(); conn.close(); } } } I must to <BR>&gt; scan the byte array 
position by position for extracting of the data <BR>&gt; contained into this 
array for a display in using a GUI. Which is the best <BR>&gt; (performance) 
solution to implemente this ? Regards; -- <BR>&gt; View this message in context:

<BR>&gt; http://www.nabble.com/Scanning-byte-array-tf2356548.html#a6563833 Sent

<BR>&gt; from <BR>&gt; the iBATIS - User - Java mailing list archive at

Nabble.com. <BR>&gt; <BR>&gt; <BR>&gt; <BR>&gt; <BR><BR>--
<BR>View this message 
in context: http://www.nabble.com/Scanning-byte-array-tf2356548.html#a6597501 
<BR>Sent from the iBATIS - User - Java mailing list archive at Nabble.com. 
<BR><BR><BR></BODY></HTML>


Mime
View raw message