1. 序言 数据库应用程序,特别是基于WEB的数据库应用程序,常会涉及到图片信息的存储和显示。 通常我们使用的方法是将所要显示的图片存在特定的目录下,在数据库中保存相应的图片的名称,在JSP中建立相应的数据源,利用数据库访问技术处理图片信息。但是,如果我们想动态的显示图片,上述方法就不能满足需要了。我们必须把图片存入数据库,然后通过编程动态地显示我们需要的图片。实际操作中,可以利用JSP的编程模式来实现图片的数据库存储和显示。 2. 建立后台数据库 if exists (select * from dbo.sysobjects where id = ) 3.向数据库存储二进制图片 启动Dreamweaver MX后,新建一个JSP文件。其代码如下所示。 <%@ page contentType=“text/html;charset=gb2312”%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName() +":"+request.getServerPort()+path+"/"; %> <% request.setCharacterEncoding(“gb2312”); //建立Statement对象 String picname=request.getParameter(“picname”); String pic=request.getParameter(“pic”); //获得所要显示图片的标题、存储路径、内容,并进行中文编码 FileInputStream str=new FileInputStream(pic); String sql=“insert into p(picname,pic) values(?,?)”; PreparedStatement pstmt=conn.getPreparedStatement(sql); pstmt.setString(1,picname); pstmt.setBinaryStream(2,str,str.available()); pstmt.execute(); //将数据存入数据库 out.println(“Success,You Have Insert an Image Successfully”); %>