469,326 Members | 1,555 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,326 developers. It's quick & easy.

What do you think about this post ?

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



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.gif");
FileInputStream fis = new FileInputStream(file);
PreparedStatement ps = conn.prepareStatement("INSERT INTO images VALUES (?,
ps.setString(1, file.getName());
ps.setBinaryStream(2, fis, file.length());

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 PreparedStatement here, but the
Statement class can equally be used.)

PreparedStatement ps = con.prepareStatement("SELECT img FROM images WHERE
imgname = ?");
ps.setString(1, "myimage.gif");
ResultSet rs = ps.executeQuery();
if (rs != null) {
while (rs.next()) {
byte[] imgBytes = rs.getBytes(1);
// use the data in some way here

Here the binary data was retrieved as an byte[]. You could have used a
InputStream object instead.
Jul 19 '05 #1
2 2106
My questions are ... ^^
Why insert files in a data base? And is this powerful?

Jul 19 '05 #2
"CM" <no*****@ulg.ac.be> wrote in message news:<c7**********@ikaria.belnet.be>...
My questions are ... ^^
Why insert files in a data base? And is this powerful?


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.
Jul 19 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

220 posts views Thread by Brandon J. Van Every | last post: by
46 posts views Thread by Keith K | last post: by
13 posts views Thread by Jason Huang | last post: by
21 posts views Thread by Tee | last post: by
41 posts views Thread by process | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by suresh191 | last post: by
reply views Thread by mdpf | last post: by
reply views Thread by harlem98 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.