日期:2014-05-18  浏览次数:20876 次

jsp插入,读取sql server数据库图片
<%
String   tup=request.getParameter( "b1 ");
Connection   conn   =   null;
Statement   stmt   =   null;
        Class.forName( "sun.jdbc.odbc.JdbcOdbcDriver ").newInstance();  
        String   url   =   "jdbc:odbc:dsdl ";
        String   user= "jhgs ";
        String   passwd= "jhgs ";
        conn   =   DriverManager.getConnection(url,user,passwd);
        stmt=conn.createStatement();
        File   files   =new   File( "tup ");    
        FileInputStream   fis=new   FileInputStream(files);  
        PreparedStatement   ps   =   conn.prepareStatement( "insert   into   image(f_tup)   values( ' "+tup+ " ') ");            
        ps.setString(2,files.getName());
        ps.executeUpdate();
%>

               
我知道有二种方法,
用二进制流或者保存图片的路径。
目前我想使用二进制流来实现,
数据库就f_id,f_tup   字段,
我该具体怎么呢?
哪位会的能否给指导下,
或者是给源代码参考,
非常感谢!



------解决方案--------------------
显示图片的页面 <td width= "437 " valign= "middle "> <p align= "center "> <img src= "showPic.jsp?id= <%=id%> " width= "150 " height= "200 "> </p> </td>

showPic.jsp:Procedure p=new Procedure();
ResultSet rs=null;

Blob b=null;
boolean connFlag=false;
try {
if(p.getCon())connFlag=true;
if(connFlag)rs=p.getProPlanPic(buildingID);// 查询信息
if(rs!=null){
while(rs.next()){
b=rs.getBlob( "picstorage ");
if(b!=null){
long size = b.length();
byte[] bs = b.getBytes(1, (int)size);
response.setContentType( "image/jpeg ");
OutputStream outs = response.getOutputStream();
outs.write(bs);
outs.flush();
}else{ response.sendRedirect( "../images/noPic.jpg ");
}
}

}else{

response.sendRedirect( "../images/noPic.jpg ");
}
p.freeConnection();
connFlag=false;
}
catch (Exception ex) {
if(connFlag)p.freeConnection();
connFlag=false;
ex.printStackTrace();
}

//注意在 % > 后不能加任何东西
%>
------解决方案--------------------
拉把椅子上帮 "zhonghai88() "顶,
呵呵