Return-Path: X-Original-To: apmail-db-derby-commits-archive@www.apache.org Delivered-To: apmail-db-derby-commits-archive@www.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id DCD7F99B7 for ; Wed, 18 Apr 2012 13:41:49 +0000 (UTC) Received: (qmail 19747 invoked by uid 500); 18 Apr 2012 13:41:49 -0000 Delivered-To: apmail-db-derby-commits-archive@db.apache.org Received: (qmail 19724 invoked by uid 500); 18 Apr 2012 13:41:49 -0000 Mailing-List: contact derby-commits-help@db.apache.org; run by ezmlm Precedence: bulk list-help: list-unsubscribe: List-Post: Reply-To: "Derby Development" List-Id: Delivered-To: mailing list derby-commits@db.apache.org Received: (qmail 19717 invoked by uid 99); 18 Apr 2012 13:41:49 -0000 Received: from nike.apache.org (HELO nike.apache.org) (192.87.106.230) by apache.org (qpsmtpd/0.29) with ESMTP; Wed, 18 Apr 2012 13:41:49 +0000 X-ASF-Spam-Status: No, hits=-2000.0 required=5.0 tests=ALL_TRUSTED X-Spam-Check-By: apache.org Received: from [140.211.11.4] (HELO eris.apache.org) (140.211.11.4) by apache.org (qpsmtpd/0.29) with ESMTP; Wed, 18 Apr 2012 13:41:41 +0000 Received: from eris.apache.org (localhost [127.0.0.1]) by eris.apache.org (Postfix) with ESMTP id F3C69238897A; Wed, 18 Apr 2012 13:41:19 +0000 (UTC) Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit 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 -0000 To: derby-commits@db.apache.org From: chaase3@apache.org X-Mailer: svnmailer-1.0.8-patched Message-Id: <20120418134119.F3C69238897A@eris.apache.org> 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.

Related information

See .

-Example +Examples create table pictures(name varchar(32) not null primary key, pic blob(16M)); -- 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 < b.name and a.pic = b.pic -order by 1,2; +order by 1,2; +

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 +.) You may be better +off using a binary stream, as in the following code fragment.

+ 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();
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 getClob method on the java handle to the underlying data.

Related information

See .

Example -import java.sql.*; + 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 > 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(); } - } -} + s.close(); + ps.close(); + rs.close(); + conn.close(); 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.

Conversions from and to bit strings

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.

+other types: you can cast a BLOB only to another BLOB, but you can cast other +bit string types to a BLOB.

Conversions of date/time values

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