This week's book giveaway is in the Agile forum.
We're giving away four copies of Head First Agile and have Andrew Stellman & Jennifer Greene on-line!
See this thread for details.
Win a copy of Head First Agile this week in the Agile forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

How can I get image from sql server in servlet?  RSS feed

 
Blues Yang
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I write a program to get image from sql server, but I can not show the pic in the webpage, it is just a X picture, why ? please help me.
the code is below :
import java.sql.*;
import java.io.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class DownLoad2 extends HttpServlet{
public DownLoad2() {
}
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doPost(request,response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
System.out.println("======DownLoad begin=====");
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection conn = DriverManager.getConnection("jdbc dbc s1","u3","p3");
Statement stmt=conn.createStatement();
String sql="select * from t1";
ResultSet rs=stmt.executeQuery(sql);
if(rs.next()){
InputStream in = rs.getBinaryStream(2);
response.setContentType("image/jpeg");
ServletOutputStream sout = response.getOutputStream();
byte[] b = new byte[1024*1024];
int len;
while((len=in.read(b)) >0){
sout.write(b,0,len);
}
sout.flush();
sout.close();
in.close();
rs.close();
stmt.close();
conn.close();
}
}catch(Exception ex){
ex.printStackTrace();
}
System.out.println("======DownLoad end=====");
}
}
 
Chris Smith
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Error message? Any other information? On first glance, the given servlet looks fine... and it'll be hard to help unless you give some indication of what's breaking. Merely saying it doesn't work is not a good problem description.
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!