473,406 Members | 2,849 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,406 software developers and data experts.

storing image in database...

i have some problem on ilplementing the code for the insertion of image into database that didnot get how to use the code for image storege.so can u help me in the use for the code by which i want to store the image of teacher with their respective database and i also have to retrive the same when i want to see the details of teacher..here is the code-

import java.io.*;
import java.sql.*;
import java.util.*;

public class StoreFile
{
Connection con=null;
DataBase db = null;
String filename=null;
String filedesc=null;

public StoreFile()
{

}

public StoreFile(String filedesc,String filename)
{
this.filedesc = filedesc;
this.filename=filename;
}

public void storeFile() throws Exception
{
System.out.println("Before DataBase Connection ");
PreparedStatement pst = null;
try
{
db = new DataBase();
con = db.getConnection();
pst = con.prepareStatement("insert into files values(?,?)");
pst.setString(1,filedesc);
FileInputStream fis=new FileInputStream(filename);
byte[] b= new byte[fis.available()+1];
fis.read(b);
pst.setBytes(2,b);
pst.execute();
System.out.println("File Stored Successfully");
}
catch(Exception e)
{
System.out.println(" Problem : "+e);
}
finally
{
con.close();
pst.close();
}

}

public void getFile(String filename) throws Exception
{
System.out.println("Before DataBase Connection ");
PreparedStatement pst = null;
ResultSet rs=null; try
{
db = new DataBase();
con = db.getConnection();
pst = con.prepareStatement("select filetype from files where filename=?");
pst.setString(1,filename);
rs=pst.executeQuery();
while(rs.next())
{
byte[] b=rs.getBytes("filetype");
FileOutputStream fos=new FileOutputStream(new File("new1.html"));
fos.write(b);
}
System.out.println("File Written Successfully");
}
catch(Exception e)
{
System.out.println(" Problem : "+e);
}
finally
{
con.close();
pst.close();
}

}

/*dbStatement = dbConnection.prepareStatement("select * from photos ");
rs=dbStatement.executeQuery();
if (rs.next()==true)
{
b=rs.getBytes("photo");
FileOutputStream fos=new FileOutputStream(new File("Anil.jpg"));
fos.write(b);
}*/


public static void main(String args[]) throws Exception
{
//System.out.println("What is The File Description ");
//String filedesc = new DataInputStream(System.in).readLine();
System.out.println("What is The File Name");
String filename = new DataInputStream(System.in).readLine();
//StoreFile store = new StoreFile(filedesc,filename);
//store.storeFile();
StoreFile store = new StoreFile();
store.getFile(filename);
}

}

please help me as soon as posible...
Mar 31 '07 #1
0 1438

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: dave | last post by:
Hello there, I am at my wit's end ! I have used the following script succesfully to upload an image to my web space. But what I really want to be able to do is to update an existing record in a...
7
by: Dave | last post by:
I have a system that basically stores a database within a database (I'm sure lots have you have done this before in some form or another). At the end of the day, I'm storing the actual data...
5
by: Don Vaillancourt | last post by:
I'm building a system when one can upload a document to the website. I will be storing the document on the hard-drive for quick/easy access, but I was also thinking of storing it in an existing...
2
by: santosh_stb | last post by:
Is there any way of storing an image file into a specific Table . It would be of great help for me if i come to know something about it.
6
by: bissatch | last post by:
Hi, I am currently writing a news admin system. I would like to add the ability to add images to each article. What I have always done in the past is uploaded (using a form) the image to a...
11
by: bissatch | last post by:
Hi, I am trying to upload an image, create a new file based on that image and then store the base64 encoded image data in a database. I dont really know where my code is going wrong so I will...
2
by: bissatch | last post by:
Hi, I am trying to write script that is run when a form is submitted. The form contains an image input field and when submitted, the image is uploaded, resized and added as binary information to...
4
by: lorirobn | last post by:
Hi, I need to add photos to my database. Back End is on MS SQL Server (I believe 2000), and Front End is on MS Access. I have read about storing the photos as BLOBS, but I am not sure how to...
0
by: RussCRM | last post by:
I want to use a command button, say "cmdDisplayImage" to display/ transfer a captured image from the ezVidCap1 control to an image or OLE box on the same form such as "PhotoPreview" (not sure...
1
by: Jonathan Wood | last post by:
My site includes a feature that allows users to upload an image. (Never more than one image per user.) I've been considering storing these uploaded images as a file on the server with a filename...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.