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
posted Apr 12, 2009 06:38:49
0
Just check the methods provided by java.sql.PreparedStatement and java.sql.ResultSet how to set and get an InputStream .
Code depot of a Java EE / JSF developer | JSF / Eclipse / Tomcat kickoff tutorial | DAO kickoff tutorial | I ♥ Unicode
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
posted Apr 12, 2009 07:39:37
0
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("jdbc dbc: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();
}
}
}
subject: How to store and retrieve images to MS access and MYsql database