Keep in touch

you can keep in touch with my all blogs and sites by install this Toolbar...
http://rworldtoolbar.ourtoolbar.com/

Tuesday, August 26, 2008

Store Image or Picture into DataBase and Retrieve from Database

Here is an example for How to store image into database and retrive that and display one by one

steps

*****

1. create one temp folder under root

2. save this 3 files into that

3. create database in ms-sql server

4. note : if connection not establishing with this driver , better to use jdbc :-> Odbc :-> driver

5. database code for ms-sqlserver

-create table image (uid numeric(6) identity (1001,1), img image )
-select * from image

getfile.html
************







Gettostore.jsp
**************

<%@ page language=”java” import=”java.sql.*,java.io.*” errorPage=”" %>
<%
try
{
Connection con = null;
System.setProperty( “jdbc.drivers”, “com.microsoft.jdbc.sqlserver.SQLServerDriver” );
Class.forName( “com.microsoft.jdbc.sqlserver.SQLServerDriver” );
con = DriverManager.getConnection( “jdbc:microsoft:sqlserver://SCALAR6:1433;DatabaseName=JAVATEAM;SelectMethod=cursor;User=sa;Password=ontrack20″ );
PreparedStatement pst = con.prepareStatement(”insert into image(img) values(?)”);

//logo path is a value which is come from file browser
FileInputStream fis=new FileInputStream(request.getParameter ( “path” ) );
byte[] b= new byte[fis.available()+1];
fis.read(b);
pst.setBytes(1,b);
pst.executeUpdate();
pst.close();
con.close();
}
catch(SQLException e)
{
out.println ( e);
}
catch (ClassNotFoundException e)
{
out.println( e );
}

%>

View from Database

link –>

Puttoview.jsp
*************

<%@ page contentType=”text/html; charset=iso-8859-1″ language=”java” import=”java.io.*,java.sql.*” errorPage=”" %>


Home

<%
try
{
Connection con = null;
System.setProperty( “jdbc.drivers”, “com.microsoft.jdbc.sqlserver.SQLServerDriver” );
Class.forName( “com.microsoft.jdbc.sqlserver.SQLServerDriver” );
con = DriverManager.getConnection( “jdbc:microsoft:sqlserver://SCALAR6:1433;DatabaseName=JAVATEAM;SelectMethod=cursor;User=sa;Password=ontrack20″ );
PreparedStatement pst = null;
ResultSet rs=null;
FileOutputStream fos;
int i = 0;
%>

<%
pst = con.prepareStatement(”select uid,img from image “); // better to use where uniqueid = ‘value’
rs=pst.executeQuery();
while(rs.next())
{
i = rs.getInt (1);
byte[] b=rs.getBytes(”img”);
fos=new FileOutputStream(”c://tomcat4.1/webapps/ROOT/temp/image” + i + “.jpg”);
fos.write(b);
fos.close();

%>

<%
}
pst.close();
con.close();
}
catch(SQLException e)
{
out.println ( e);
}
catch (ClassNotFoundException e)
{
out.println( e );
}

%>

.jpg” mce_src=”./image<%=i%>.jpg” >
<%=i%>


//run : http://localhost:8080/temp/getfile.html

No comments: