Store Image (or) Pic into DataBase and Retrieve from Database


Here is an example for How to store image into database and reterive 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
************
<html>
<body>
<form method=”get” action=”./Gettostore.jsp”>
<input type=”file” name=”path” size=”50″/>
<input type=”submit” value=”Submit”/>
</form>
</body>
</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 );
}
%>
<a href=”./Puttoview.jsp” mce_href=”./Puttoview.jsp”> View from Database </a>
<!– better u to redirect to another page and add this above <a href..> link –>
Puttoview.jsp
*************
<%@ page contentType=”text/html; charset=iso-8859-1″ language=”java” import=”java.io.*,java.sql.*” errorPage=”" %>
<html>
<body>
<a href=”./getfile.html” mce_href=”./getfile.html”> Home </a>
<%
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;
%>
<table border=”1″ align=”center” >
<%
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();
%>
<tr><td align=”center”><img src=”./image<%=i%>.jpg” mce_src=”./image<%=i%>.jpg” >
<%=i%></td></tr>
<%
}
pst.close();
con.close();
}
catch(SQLException e)
{
out.println ( e);
}
catch (ClassNotFoundException e)
{
out.println( e );
}
%>
</table>
</body>
</html>
//run : http://localhost:8080/temp/getfile.html

Comments

Popular posts from this blog

What is the difference between asynchronous and synchronous execution?

Yahoo accepting requests for inactive email IDs

How to install JDK 1.7.0 and how to set path and classpath variables