基于jsp访问oracle数据库blob字段并显示图形的解决方案.doc
文本预览下载声明
基于jsp访问oracle数据库blob字段并显示图形的解决方案
篇一:从数据库中读取Blob对象图片并显示
从数据库中读取Blob对象图片并显示
第一种方法:
大致方法就是,从数据库中读出Blob的流来,写到页面中去:
Connection conn = DBManager.getConnection();
String sql = SELECT picture FROM teacher WHERE id=1;
PreparedStatement ps = null;
ResultSet rs = null;
InputStream is = null;
OutputStream os = null;
try {
ps = conn.prepareStatement(sql);
rs = ps.executeQuery();
if(rs.next()){
is = rs.getBinaryStream(1);
}
response.setContentType(text/html);
os = response.getOutputStream();
int num;
byte buf[] = new byte[1024];
while((num=is.read(buf))!=-1){
os.write(buf, 0, num);
}
} catch (SQLException e) {
e.printStackTrace();
}
try {
is.close();
os.close();
rs.close();
ps.close();
} catch (SQLException e) {
e.printStackTrace();
}
在页面中:
lt;%
String path = request.getContextPath();
String basePath
request.getScheme()+://+request.getServerName()+:+request.getServerPort()+path+/; %
lt;img name=pic src=lt;%=basePath+servlet/DownloadAsStream%/
搞定。
=
第二种方法:
整个流程分为四步,连接oracle数据库 - 读取blob图片字段 - 对图片进行缩放 -把图片展示在jsp页面上。
import java.sql.*;
import java.io.*;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.awt.image.AffineTransformOp;
import java.awt.geom.AffineTransform;
public class OracleQueryBean {
private final String oracleDriverName = oracle.jdbc.driver.OracleDriver;
private Connection myConnection = null;
private String strTabName;
private String strIDName;
private String strImgName;
public OracleQueryBean(){
try{
Class.forName(oracleDriverName);
}catch(ClassNotFoundException ex){
System.out.println(加载jdbc驱动失败,原因: + ex.getMessage());
}
}
public Connection getConnection(){
try{
//用户名+密码; 以下使用的Test就是Oracle里的表空间
//从配置文件中读取数据库信息
GetPara oGetPara = new GetPara();
String strIP = oGetPara.getPara(serverip);
String strPort = oGetPara.getPara(port);
String strDBName = oGetPara.getPara(dbname);
String strUser = oGetPara.getPara(user);
String strPassword = oGetPara.getPar
显示全部