db-derby-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From chaa...@apache.org
Subject svn commit: r1327508 - in /db/derby/docs/trunk/src/ref: rrefblob.dita rrefclob.dita rrefsqlj33562.dita
Date Wed, 18 Apr 2012 13:41:19 GMT
Author: chaase3
Date: Wed Apr 18 13:41:19 2012
New Revision: 1327508

URL: http://svn.apache.org/viewvc?rev=1327508&view=rev
Log:
DERBY-5505  BLOB and CLOB Reference Manual topics could use some fixes

Modified 3 Reference Manual topics.

Patch: DERBY-5505-2.diff

Modified:
    db/derby/docs/trunk/src/ref/rrefblob.dita
    db/derby/docs/trunk/src/ref/rrefclob.dita
    db/derby/docs/trunk/src/ref/rrefsqlj33562.dita

Modified: db/derby/docs/trunk/src/ref/rrefblob.dita
URL: http://svn.apache.org/viewvc/db/derby/docs/trunk/src/ref/rrefblob.dita?rev=1327508&r1=1327507&r2=1327508&view=diff
==============================================================================
--- db/derby/docs/trunk/src/ref/rrefblob.dita (original)
+++ db/derby/docs/trunk/src/ref/rrefblob.dita Wed Apr 18 13:41:19 2012
@@ -42,7 +42,7 @@ handle to the underlying data.</p> </sec
 <section><title>Related information</title>
 <p>See <xref href="rrefjdbc96386.dita#rrefjdbc96386"></xref>.</p>
 </section>
-<example><title>Example</title>
+<example><title>Examples</title>
 <codeblock><b><ph>create table pictures(name varchar(32) not null primary
key, pic blob(16M));
 </ph>
 -- find all logotype pictures
@@ -53,6 +53,53 @@ select a.name as double_one, b.name as d
 from pictures as a, pictures as b
 where a.name &lt; b.name
 and a.pic = b.pic
-order by 1,2;</b></codeblock> </example>
+order by 1,2;</b></codeblock> 
+<p>Using an INSERT statement to put BLOB data into a table has some limitations
+if you need to cast a long string constant to a BLOB. (See
+<xref href="rrefstringlimits.dita#rrefstringlimits"></xref>.) You may be better
+off using a binary stream, as in the following code fragment.</p>
+<codeblock>        String url = "jdbc:derby:blobby;create=true";
+        Connection conn = DriverManager.getConnection(url);
+
+        Statement s = conn.createStatement();
+        s.executeUpdate(
+            "CREATE TABLE images (id INT, img BLOB)");
+
+        // - first, create an input stream
+        InputStream fin = new FileInputStream("image.jpg");
+
+        PreparedStatement ps = conn.prepareStatement(
+            "INSERT INTO images VALUES (?, ?)");
+        ps.setInt(1, 1477);
+
+        // - set the value of the input parameter to the input stream
+        ps.setBinaryStream(2, fin);
+        ps.execute();
+
+        // --- reading the columns
+        ResultSet rs = s.executeQuery(
+            "SELECT img FROM images WHERE id = 1477");
+        byte buff[] = new byte[1024];
+
+        while (rs.next()) {
+            Blob ablob = rs.getBlob(1);
+            File newfile = new File("newimage.jpg");
+
+            InputStream is = ablob.getBinaryStream();
+
+            FileOutputStream fos = 
+                new FileOutputStream(newfile);
+
+            for (int b = is.read(buff); b != -1; b = is.read(buff)) {
+                fos.write(buff, 0, b);
+            }
+
+            is.close();
+            fos.close();
+        }
+        s.close();
+        ps.close();
+        rs.close();
+        conn.close();</codeblock></example>
 </refbody>
 </reference>

Modified: db/derby/docs/trunk/src/ref/rrefclob.dita
URL: http://svn.apache.org/viewvc/db/derby/docs/trunk/src/ref/rrefclob.dita?rev=1327508&r1=1327507&r2=1327508&view=diff
==============================================================================
--- db/derby/docs/trunk/src/ref/rrefclob.dita (original)
+++ db/derby/docs/trunk/src/ref/rrefclob.dita Wed Apr 18 13:41:19 2012
@@ -38,59 +38,41 @@ the <i>getClob</i> method on the <i>java
 handle to the underlying data.</p> </section>
 <section><title>Related information</title><p>See <xref href="rrefjdbc96386.dita#rrefjdbc96386"></xref>.
</p> </section>
 <example><title>Example</title>
-<codeblock>import java.sql.*;
+<codeblock>        String url = "jdbc:derby:clobberyclob;create=true";
+        Connection conn = DriverManager.getConnection(url);
 
-public class clob
-{
-    public static void main(String[] args) {
-        try {
-            String url = "jdbc:derby:clobberyclob;create=true";
-
-            // Load the driver. This code is not needed if you are using 
-            // JDK 6, because in that environment the driver is loaded 
-            // automatically when the application requests a connection.
-            Class.forName("org.apache.derby.jdbc.EmbeddedDriver");
-
-            Connection conn = DriverManager.getConnection(url);
-
-            Statement s = conn.createStatement();
-            s.executeUpdate(
-                "CREATE TABLE documents (id INT, text CLOB(64 K))");
-            conn.commit();
-
-            // --- add a file
-            java.io.File file = new java.io.File("asciifile.txt");
-            int fileLength = (int) file.length();
-
-            // - first, create an input stream
-            java.io.InputStream fin = new java.io.FileInputStream(file);
-            PreparedStatement ps = conn.prepareStatement("INSERT
-            INTO documents VALUES (?, ?)");
-            ps.setInt(1, 1477);
-
-            // - set the value of the input parameter to the input stream
-            ps.setAsciiStream(2, fin, fileLength);
-            ps.execute();
-            conn.commit();
-
-            // --- reading the columns
-            ResultSet rs = s.executeQuery(
-                "SELECT text FROM documents WHERE id = 1477");
-            while (rs.next()) {
-                java.sql.Clob aclob = rs.getClob(1);
-                java.io.InputStream ip = rs.getAsciiStream(1);
-                int c = ip.read();
-                while (c &gt; 0) {
-                    System.out.print((char)c);
-                    c = ip.read();
-                }
-                System.out.print("\n");
-                // ...
+        Statement s = conn.createStatement();
+        s.executeUpdate(
+            "CREATE TABLE documents (id INT, text CLOB)");
+
+        // - first, create an input stream
+        InputStream fis = new FileInputStream("asciifile.txt");
+
+        PreparedStatement ps = conn.prepareStatement(
+            "INSERT INTO documents VALUES (?, ?)");
+        ps.setInt(1, 1477);
+
+        // - set the value of the input parameter to the input stream
+        ps.setAsciiStream(2, fis);
+        ps.execute();
+
+        // --- reading the columns back
+        ResultSet rs = s.executeQuery(
+            "SELECT text FROM documents WHERE id = 1477");
+
+        while (rs.next()) {
+            Clob aclob = rs.getClob(1);
+            InputStream ip = aclob.getAsciiStream();
+
+            for (int c = ip.read(); c != -1; c = ip.read()) {
+                System.out.print((char)c);
             }
-        } catch (Exception e) {
-            System.out.println("Error! "+e);
+
+            ip.close();
         }
-    }
-}</codeblock> </example>
+        s.close();
+        ps.close();
+        rs.close();
+        conn.close();</codeblock> </example>
 </refbody>
 </reference>

Modified: db/derby/docs/trunk/src/ref/rrefsqlj33562.dita
URL: http://svn.apache.org/viewvc/db/derby/docs/trunk/src/ref/rrefsqlj33562.dita?rev=1327508&r1=1327507&r2=1327508&view=diff
==============================================================================
--- db/derby/docs/trunk/src/ref/rrefsqlj33562.dita (original)
+++ db/derby/docs/trunk/src/ref/rrefsqlj33562.dita Wed Apr 18 13:41:19 2012
@@ -752,11 +752,12 @@ the target numeric cannot represent the 
 source numeric, then the source is silently truncated to fit into the target.
 For example, casting 763.1234 as INTEGER yields 763.</p> </section>
 <section><title>Conversions from and to bit strings</title> <p>Bit
strings
-can be converted to other bit strings, but not character strings. Strings
+can be converted to other bit strings, but not to character strings. Strings
 that are converted to bit strings are padded with trailing zeros to fit the
 size of the target bit string. The BLOB type is more limited and requires
 explicit casting. In most cases the BLOB type cannot be cast to and from
-other types.</p> </section>
+other types: you can cast a BLOB only to another BLOB, but you can cast other
+bit string types to a BLOB.</p> </section>
 <section id="sqlj75614"><title>Conversions of date/time values</title>
<p>A
 date/time value can always be converted to and from a TIMESTAMP. If a DATE
 is converted to a TIMESTAMP, the TIME component of the resulting TIMESTAMP



Mime
View raw message