how can insert an image to a ms-access database with using a java swing form and vice versa

try it...


1.I have declared the image datatype as OLE
2.Inserting the image code
--------------------------
PreparedStatement psmnt = con.prepareStatement("INSERT INTO IMAGESTORAGE(IMAGE) VALUES(?)");
File image = new File("Sunset.jpg");
FileInputStream fis = new FileInputStream(image)
psmnt.setBinaryStream(1, (InputStream)fis, (int)(image.length()));
int insertCount= psmnt.executeUpdate();
System.out.println(insertCount);
-------------------------------
3.retreival
Here iam reading and putting the image in a new file called "SunsetImageRead.jpg"
psmnt = con.prepareStatement("SELECT IMAGE FROM IMAGESTORAGE");
ResultSet rs = psmnt.executeQuery();
if(rs.next())
{
InputStream fis1;
FileOutputStream fos;
try
{
fis1 = rs.getBinaryStream("image");
fos = new FileOutputStream(new File("SunsetImageRead.jpg"));
int c;
while ((c = fis1.read()) != -1)
{
fos.write(c);
}
fis1.close();
fos.close();
}catch(Exception ex)
{
System.out.println(ex);
}//try-c

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.