adding images to the database

pls how do i go about adding or storing pics to the database using java at my front end the code if possible
[115 byte] By [hamsidaa] at [2007-11-27 9:13:27]
# 1

don't forget to assign the dukes plz.

Insert an Image

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,(int)file.length());

ps.executeUpdate();

ps.close();

fis.close();

Retrieve an Image

// we assume that the type is LONGVARBINARY

Image myImage = null;

InputStream stream = rset.getBinaryStream(column);

ByteArrayOutputStream output = new ByteArrayOutputStream();

try {

int a1 = stream.read();

while (a1 >= 0) {

output.write((char)a1);

a1 = stream.read();

}

myImage =

Toolkit.getDefaultToolkit().createImage(output.toByteArray());

output.close();

}

catch(Exception e){}

Hope That Helps

java_2006a at 2007-7-12 22:00:52 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...