What do you think about this post ?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • CM

    What do you think about this post ?

    Hi,
    I found this post in a forum. What do you think about it? :)

    Christophe

    ----------------------------------------------------------------------------
    ----



    I have used JDBC extensively with pg 7.2.4, but I have not used the bytea
    type to send data to/from a java application.

    I found the most relevant data in the 7.4 documentation, and it says 7.2 is
    the first version that supports bytea through the JDBC driver.


    Version 7.2 was the first release of the JDBC driver that supports the bytea
    data type. The introduction of this functionality in 7.2 has introduced a
    change in behavior as compared to previous releases. Since 7.2, the methods
    getBytes(), setBytes(), getBinaryStream (), and setBinaryStream () operate on
    the bytea data type. In 7.1 and earlier, these methods operated on the oid
    data type associated with Large Objects. It is possible to revert the driver
    back to the old 7.1 behavior by setting the property compatible on the
    Connection object to the value 7.1.

    To use the bytea data type you should simply use the getBytes(), setBytes(),
    getBinaryStream (), or setBinaryStream () methods.


    and perhaps exactly what you may be looking for



    To insert an image, you would use:

    File file = new File("myimage.g if");
    FileInputStream fis = new FileInputStream (file);
    PreparedStateme nt ps = conn.prepareSta tement("INSERT INTO images VALUES (?,
    ?)");
    ps.setString(1, file.getName()) ;
    ps.setBinaryStr eam(2, fis, file.length());
    ps.executeUpdat e();
    ps.close();
    fis.close();

    Here, setBinaryStream () transfers a set number of bytes from a stream into
    the column of type bytea. This also could have been done using the
    setBytes() method if the contents of the image was already in a byte[].

    Retrieving an image is even easier. (We use PreparedStateme nt here, but the
    Statement class can equally be used.)

    PreparedStateme nt ps = con.prepareStat ement("SELECT img FROM images WHERE
    imgname = ?");
    ps.setString(1, "myimage.gi f");
    ResultSet rs = ps.executeQuery ();
    if (rs != null) {
    while (rs.next()) {
    byte[] imgBytes = rs.getBytes(1);
    // use the data in some way here
    }
    rs.close();
    }
    ps.close();

    Here the binary data was retrieved as an byte[]. You could have used a
    InputStream object instead.


  • CM

    #2
    Re: What do you think about this post ?

    My questions are ... ^^
    Why insert files in a data base? And is this powerful?

    Greeting
    Christophe


    Comment

    • Dave

      #3
      Re: What do you think about this post ?

      "CM" <no-spam@ulg.ac.bew rote in message news:<c7alat$bl i$1@ikaria.beln et.be>...
      My questions are ... ^^
      Why insert files in a data base? And is this powerful?
      >
      Greeting
      Christophe

      Security perhaps. Also, adding attributes to the files and indexing
      them. For instance, annotating the people in a picture and when the
      picture was taken. Then later being able to find any pictures with a
      certain person in a time range.

      Comment

      Working...