473,385 Members | 1,464 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

What do you think about this post ?

CM
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.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());
ps.executeUpdate();
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 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
}
rs.close();
}
ps.close();

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

Greeting
Christophe
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?

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

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

220
by: Brandon J. Van Every | last post by:
What's better about Ruby than Python? I'm sure there's something. What is it? This is not a troll. I'm language shopping and I want people's answers. I don't know beans about Ruby or have...
65
by: perseus | last post by:
I think that everyone who told me that my question is irrelevant, in particular Mr. David White, is being absolutely ridiculous. Obviously, most of you up here behave like the owners of the C++...
125
by: Sarah Tanembaum | last post by:
Beside its an opensource and supported by community, what's the fundamental differences between PostgreSQL and those high-price commercial database (and some are bloated such as Oracle) from...
10
by: Ka | last post by:
Hi, all I use Redhat8.0, and I complie a program with wine in this box, my wine version is wine-20020605-2 by Redhat. and after install wine, I just locate the library in /usr/lib/wine ...
46
by: Keith K | last post by:
Having developed with VB since 1992, I am now VERY interested in C#. I've written several applications with C# and I do enjoy the language. What C# Needs: There are a few things that I do...
9
by: Nadav | last post by:
Hi, I am tring to pass messages between threads, using the good old C++ I would call the GetMessage/PostThreadMessage APIs, Now, I am using C# and i can't find any equivalenty for these calls, any...
13
by: Jason Huang | last post by:
Hi, Would someone explain the following coding more detail for me? What's the ( ) for? CurrentText = (TextBox)e.Item.Cells.Controls; Thanks. Jason
12
by: Veeru | last post by:
Hi, Can anyone tell me the difference between heap memory and stack stack both physically and fundamentally. It would be a real favour. Regards, Veeru
21
by: Tee | last post by:
Hi, Just curious what is clickone? Is it the same as Installshield? Been follow the following instructions and it seems to create an installation exe for me which is good but how come it...
41
by: process | last post by:
I have heard some criticism about Python, that it is not fully object- oriented. What is not an object in Python? Why isn't len implemented as a str.len and list.len method instead of a...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.