• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
pradeep dewda
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2458
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just check the methods provided by java.sql.PreparedStatement and java.sql.ResultSet how to set and get an InputStream.
 
pradeep dewda
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you suggest those methods by a little example..??
 
Bauke Scholtz
Ranch Hand
Posts: 2458
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hint: an image is in fact a binary stream.
 
pradeep dewda
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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();
}
}
}
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic