 |
| Java Databases Discussion specific to working with Java Databases. For other Java topics, please see related Java forums. For database discussions not specific to Java, please see the Database category. |
Welcome to the p2p.wrox.com Forums.
You are currently viewing the Java Databases section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
|
|
|
|

March 2nd, 2004, 07:40 AM
|
|
Registered User
|
|
Join Date: Mar 2004
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Display picture from database using JSP codes
I have no idea how to start displaying a picture in a JSP page from the database.
I started off with these codes..
My Servlet
import java.io.*;
import javax.servlet.http.*;
import javax.servlet.*;
import java.sql.*;
public class ImageServlet extends HttpServlet {
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
try {
String filename = request.getParameter("filename");
filename = "baby.gif";
//execute you sql to get the clob here
String url = "jdbc:mysql://localhost/test";
String userID="root";
String PWD="";
Connection c = null;
String classPath= "com.mysql.jdbc.Driver";
try{
Class.forName(classPath).newInstance();
conn = DriverManager.getConnection(url,userID,PWD);
}catch(Exception exc){
out.println(exc.toString());
}
Statement s = null;
ResultSet rst = null;
// Connection c = ...
Statement s = c.createStatement();
String strSQL = "SELECT binaryfile FROM uploads WHERE filename='" +filename +"'";
rst = stm.executeQuery(strSQL);
byte [] barray = rst.getBytes("binaryfile");
rst.close();
s.close();
response.setContentType("image/gif");
response.getOutputStream().write(barray);
} catch (Exception sqle) {
}
}
}
in my JSp page, i used this
<embed src="http://localhost:8080/servlet/ImageServlet" content="image/gif">
However, no picture was displayed...
What went wrong? Please advise me. Thanks to all.
|
|

March 3rd, 2004, 03:11 AM
|
|
Authorized User
|
|
Join Date: Feb 2004
Posts: 32
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
There is mistakes in :
/********************* mistake **********************/
Connection c = null;
conn = DriverManager.getConnection(url,userID,PWD);//conn mistake
Statement s = c.createStatement();
rst = stm.executeQuery(strSQL);//stm mistake
/********************* mistake **********************/
would be:
Connection c = null;
c = DriverManager.getConnection(url,userID,PWD);
Statement s = c.createStatement();
rst = s.executeQuery(strSQL);
|
|

March 3rd, 2004, 11:39 AM
|
|
Friend of Wrox
|
|
Join Date: Jun 2003
Posts: 175
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Cash has already pointed out that your source contains several typos so I'm won't comment on them. Have a look at this URL, which has examples of what you are trying to do: http://jguru.com/faq/view.jsp?EID=159
Cheers
Martyn
|
|

November 15th, 2007, 03:26 AM
|
|
Registered User
|
|
Join Date: Mar 2007
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Hi, i am facing problems so hope you guys can help me out! I did try out many different solutions. What i want is to retrieve the BLOB image in mysql and display it in jsp.
I tried out the code. But there is no image displayed out. I have no idea what went wrong.
I embed the servlet in one of the tags so i am not sure if i am right. If not, please guide me. Thanks.
---comments.jsp---
table border="1" borderColor="#ffe9bf" cellPadding="0" cellSpacing="0" width="428" height="63">
<tbody>
<td bgColor="#008080" width="28" align="center" height="19"><b>No.</b></td>
<td bgColor="#008080" width="126" align="center" height="19"><b>Name</b></td>
<td bgColor="#008080" width="224" align="center" height="19"><b>Picture</b></td>
<td bgColor="#008080" width="270" align="center" height="19"><b>Comments</b></td>
<%
String DRIVER = "com.mysql.jdbc.Driver";
Class.forName(DRIVER).newInstance();
Connection conn = null;
Statement stmt = null;
try{
String dbUrl = "jdbc:mysql://localhost/homepage?user=root&password=mysql";
int i=1;
conn = DriverManager.getConnection(dbUrl);
stmt = conn.createStatement();
String query = "Select * FROM Comments";
ResultSet rst = stmt.executeQuery(query);
while(rst.next()){
if(i==(i/2)*2){
%>
<tr>
<td width="28" height="19" align="center" vAlign="top" bgColor="#ffff98" class="title"><%=i%></td>
<td width="126" height="19" align="center" vAlign="top" bgColor="#ffff98" class="title"><%=rst.getString(3)%></td>
<td width="224" height="19" align="center" vAlign="top" bgColor="#ffff98" class="title"> <embed src="http://localhost:8080/servlet/ImageServlet" content="image/jpeg"></embed></td></td>
<td width="270" height="19" align="center" vAlign="top" bgColor="#ffff98" class="title"><%=rst.getString(4)%></td>
</tr>
<%
}else{
%>
<tr>
<td width="28" height="19" align="center" vAlign="top" bgColor="#ffcc68" class="title"><%=i%></td>
<td width="126" height="19" align="center" vAlign="top" bgColor="#ffcc68" class="title"><%=rst.getString(3)%></td>
<td width="224" height="19" align="center" vAlign="top" bgColor="#ffff98" class="title"><embed src="http://localhost:8080/servlet/ImageServlet" content="image/jpeg"></embed> </td>
<td width="270" height="19" align="center" vAlign="top" bgColor="#ffcc68" class="title"><%=rst.getString(4)%></td>
</tr>
<% }
i++;
}
rst.close();
stmt.close();
conn.close();
}catch(Exception e){
System.out.println(e.getMessage());
}
%>
</tbody>
</table>
<---ImageServlet.java-->
import java.io.*;
import javax.servlet.http.*;
import javax.servlet.*;
import java.sql.*;
public class ImageServlet extends HttpServlet {
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
try {
String filename = request.getParameter("name");
//filename = "baby.gif";
//execute you sql to get the clob here
String url = "jdbc:mysql://localhost/homepage";
String userID="root";
String PWD="mysql";
Connection conn = null;
String classPath= "com.mysql.jdbc.Driver";
try{
Class.forName(classPath).newInstance();
conn = DriverManager.getConnection(url,userID,PWD);
}catch(Exception exc){
out.println(exc.toString());
}
Statement stmt = null;
ResultSet rst = null;
// Connection c = ...
stmt = conn.createStatement();
//String strSQL = "SELECT Picture FROM comments WHERE name='" +filename +"'";
String strSQL = "SELECT Picture1 FROM comments WHERE name='" +filename+"'";
rst = stmt.executeQuery(strSQL);
byte [] barray = rst.getBytes("binaryfile");
rst.close();
stmt.close();
response.setContentType("image/jpeg");
response.getOutputStream().write(barray);
} catch (Exception sqle) {
}
}
}
|
|

November 16th, 2007, 09:44 AM
|
|
Friend of Wrox
|
|
Join Date: Mar 2007
Posts: 373
Thanks: 0
Thanked 1 Time in 1 Post
|
|
You can just use simple <img> tag and src of this tag should be the servlet you've written. And set the content type in the servlet itself. That would be very simple.
- Rakesh
|
|

November 16th, 2007, 09:46 AM
|
|
Friend of Wrox
|
|
Join Date: Mar 2007
Posts: 373
Thanks: 0
Thanked 1 Time in 1 Post
|
|
Try also by calling the flush() method on the output stream.
- Rakesh
|
|
 |