This week's book giveaway is in the OCPJP forum.
We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates on-line!
See this thread for details.
The moose likes JDBC and the fly likes How to store and retrieve images to MS access and MYsql database Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Databases » JDBC
Bookmark "How to store and retrieve images to MS access and MYsql database" Watch "How to store and retrieve images to MS access and MYsql database" New topic
Author

How to store and retrieve images to MS access and MYsql database

pradeep dewda
Greenhorn

Joined: Apr 12, 2009
Posts: 9
I never worked with images..so i don't know how to store and retrieve images to and from the database..can any one suggest..
Some body suggest me about using BLOB or storing links in the database..but how?? i tried but faild every time..
Bauke Scholtz
Ranch Hand

Joined: Oct 08, 2006
Posts: 2458
Just check the methods provided by java.sql.PreparedStatement and java.sql.ResultSet how to set and get an InputStream.
pradeep dewda
Greenhorn

Joined: Apr 12, 2009
Posts: 9
Can you suggest those methods by a little example..??
Bauke Scholtz
Ranch Hand

Joined: Oct 08, 2006
Posts: 2458
Hint: an image is in fact a binary stream.
pradeep dewda
Greenhorn

Joined: Apr 12, 2009
Posts: 9
Bauke Scholtz wrote:Hint: an image is in fact a binary stream.


I found it..
getBinaryStream() and setBinaryStream()
ok Bauke..I will study and if i get any doubt i'll come to you later..thanks
pradeep dewda
Greenhorn

Joined: Apr 12, 2009
Posts: 9
I code below class to store the image in a database but not getting result..exception : NullPointerException why???

package src;

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.sql.*;

public class StorePhoto extends HttpServlet
{
public void init(ServletConfig s) throws ServletException
{
super.init(s);
System.out.println("Servlet Initiated");
}
public void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException,IOException
{
doPost(req,res);
}
public void doPost(HttpServletRequest req, HttpServletResponse res) throws ServletException,IOException
{
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con = DriverManager.getConnection("jdbcdbc:adsn");
Statement stmt = con.createStatement();
System.out.println("connection created");
HttpSession session = req.getSession();
String url = req.getParameter("path");//here path is the parameter coming from html page...
System.out.println("URL = "+url);
System.out.println("session ID ="+session.getId());
PreparedStatement pst = con.prepareStatement("insert into image(photo) values(?)");
System.out.println("query executed");
FileInputStream fis = new FileInputStream(url);
System.out.println("FIS object created");
byte[] b = new byte[fis.available()+1];
fis.read(b);
pst.setBytes(1,b);
pst.executeUpdate();
System.out.println("query exccuted and update done");
pst.close();
con.close();
}
catch(SQLException e)
{
System.out.println("SQL exception");
e.printStackTrace();
}
catch (ClassNotFoundException e)
{
e.printStackTrace();
}
}
}
 
Don't get me started about those stupid light bulbs.
 
subject: How to store and retrieve images to MS access and MYsql database