Example Code breaks.
Example Code breaks.
От:
john-paul delaney <jp@justatest.com>
Дата:
Hello List...
I'm trying to setup a servlet that posts images to a postgres table. Following the example code in the PostgreSQL 7.2.1 Documentation (Chapter 8.6. Storing Binary Data) below I get an error:
setBinaryStream(int,java.io.InputStream,int) in java.sql.PreparedStatement cannot be applied to (int,java.io.FileInputStream,long)
ps.setBinaryStream(4, fis, file.length());
^
Seems there's a mismatch of data types between int and long of InputStream and FileInputStream of java.io? How to cast one so it's type is compatible with the other?
Thanks for any suggestions
/j-p.
EXAMPLE CODE
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();
-----------------------
JUSTATEST Art Online
www.justatest.com
Re: Example Code breaks.
От:
"Marin Dimitrov" <marin.dimitrov@sirma.bg>
Дата:
----- Original Message -----
From: "john-paul delaney"
> Seems there's a mismatch of data types between int and long of InputStream
and FileInputStream of java.io? How to cast one so it's type is compatible
with the other?
>
try with
ps.setBinaryStream(2, fis, (int)file.length());
hth,
Marin
----
"...what you brought from your past, is of no use in your present. When
you must choose a new path, do not bring old experiences with you.
Those who strike out afresh, but who attempt to retain a little of the
old life, end up torn apart by their own memories. "