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

Hi Lovables,

Today i Posted a new Topic for u.. what i done successfully,

Helping Hands is better than Praying Lips Thats my Principle

Program by : d.r. vijaykumar

email : drvijayy2k2@yahoo.co.in , drvijayy2k2@gmail.com

ph : hyd 09912877343 , tn 09842088860

Plese Feedback

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"> 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"> 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" >

<%=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

***xx***

[4035 byte] By [drvijayy2k2a] at [2007-11-27 9:13:55]
# 1

You can improve on the following:

1. This will only work if the path for the image is on the server's file system. It will not work if the use accesses it remotely and selects a file from his system.

2. In Puttoview, you're creating the image again;

a. You've hardcoded the path; what if you install Tomcat in a different directory tomorrow? Or if you deploy this app on a different machine? Will not work.

b. You don't need to create the image file; you can directly send the bytestream data to the output stream of the servlet, the servlet should be the source of the image.

c. You should use buffered streams for performance improvement.

3. You database processing shouldn't be in the JSPs.

4. See this: http://balusc.xs4all.nl/srv/dev-jep-img.html

nogoodatcodinga at 2007-7-12 22:01:42 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 2
Hi,it for new entry(programmers) only to understanding purpose, any way thankx
drvijayy2k2a at 2007-7-12 22:01:42 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 3

> Hi,

>

> it for new entry(programmers) only to understanding

> purpose, any way thankx

Alright but you said:

> Plese Feedback

so that's what this was.

And anyway, I think they'd understand and learn better if they were shown the correct principles and design patterns right from the start instead of using something less efficient or something that is not recommended for actual use.

nogoodatcodinga at 2007-7-12 22:01:42 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 4

This 'tutorial' is flawed and needs work. Use with care.

1 - Upload the file correctly. This is NOT an example of how to upload a file from a web page. You need method="post" and enctype="multipart/form-data". Then in the servlet doing the file retrieval you need to get the input stream from the request. Using a library like the jakarta commons File Upload is recommended.

2 - Database connection should not be done in a JSP. 'nuff said.

3 - In fact scriptlet code in a JSP is a bad approach in general. avoid.

4 - A better approach would be to use an ImageServlet as exampled, and just have img tag reference that servlet directly.

While I applaud the intentions behind wanting to share the code, the code shows several basic design flaws. I think it is a better example of what NOT to do rather than the intended how-to.

Thats my 2 cents.

evnafets

evnafetsa at 2007-7-12 22:01:42 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 5
Hehe, exactly what I've said above :D
nogoodatcodinga at 2007-7-12 22:01:42 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 6
You can make a servlet and call that servlet in <img> tag. Give src as a servlet path.In that servlet u can retrieve the image file from database and write it to request output stream.
niraj.tha at 2007-7-12 22:01:42 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 7
<sigh> Why is everyone repeating what I've already pointed out? :D
nogoodatcodinga at 2007-7-12 22:01:42 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...