在现在很多的应用中,数据库会经常要存取一些大容量的数据,典型的代表:Blob,LongRaw
本文主要介绍这两种类型存取图形的操作(jsp页面的访问)。
一 Blob
建立Table:
CREATE TABLE PHOTO
(
PHOTO_NO VARCHAR2(1) NOT NULL,
IMAGE BLOB
) showimage.jsp
<%
/**//*
從Oracle數據庫中讀取Blob的類型,並且在jsp頁面中顯示出來
*/
%>
<%@ page contentType="text/html; charset=BIG5" %>
<%@ page import="java.io.*"%>
<%@ page import="java.sql.*, javax.sql.*" %>
<%@ page import="java.util.*"%>
<%@ page import="java.math.*"%>
<%
String photo_no = request.getParameter("photo_no");
//out.print(photo_no);
//Oracle連結
Class.forName("oracle.jdbc.driver.OracleDriver");
String url="jdbc:oracle:thin:@IP:1521:SID";
String userName="user";
String pwd="pwd";
Connection conn=DriverManager.getConnection(url,userName,pwd);
try{
Statement stmt = conn.createStatement();
String sql = " SELECT * FROM PHOTO WHERE photo_no = "+ photo_no;
ResultSet rs = stmt.executeQuery(sql);
if (rs.next()) {
Blob b = rs.getBlob("image");
long size = b.length();
//out.print(size);
byte[] bs = b.getBytes(1, (int)size);
response.setContentType("image/jpeg");
OutputStream outs = response.getOutputStream();
outs.write(bs);
outs.flush();
rs.close();
}
else {
rs.close();
}
}
finally{
conn.close();
}
%>
index.html
<HTML>
<HEAD>
<TITLE>图像测试</TITLE>
</HEAD>
<BODY>
<TABLE>
<TR>
<TD>图像测试</TD>
</TR>
<TR>
<TD><img src="showimage.jsp?photo_no=1"></TD>
</TR>
</TABLE>
</BODY>
</HTML>
二 LongRaw
建立Table:
CREATE TABLE PHOTO_2
(
PHOTO_NO VARCHAR2(1) NOT NULL,
IMAGE LONG RAW
) showimage.jsp
<%
/**//*
從Oracle數據庫中讀取LongRaw的類型,並且在jsp頁面中顯示出來
*/
%>
<%@ page contentType="text/html; charset=BIG5" %>
<%@ page import="java.io.*"%>
<%@ page import="java.sql.*, javax.sql.*" %>
<%@ page import="java.util.*"%>
<%@ page import="java.math.*"%>
<%
String photo_no = request.getParameter("photo_no");
out.print(photo_no);
//Oracle連結
Class.forName("oracle.jdbc.driver.OracleDriver");
String url="jdbc:oracle:thin:@IP:1521:SID";
String userName="user";
String pwd="pwd";
Connection conn=DriverManager.getConnection(url,userName,pwd);
try{
Statement stmt = conn.createStatement();
String sql = " SELECT * FROM PHOTO_2 WHERE photo_no = "+ photo_no;
ResultSet rs = stmt.executeQuery(sql);
if (rs.next()) {
byte bs[]=rs.getBytes("image");
response.setContentType("image/jpeg");
OutputStream outs = response.getOutputStream();
outs.write(bs);
outs.flush();
rs.close();
}
else {
rs.close();
}
}
finally{
conn.close();
}
%>
PS:这样的访问必须要保证这些Blog或LongRaw类型的字段中存储的是图片文件。
posted on 2005-08-17 13:13
Java&Inter 阅读(1480)
评论(0) 编辑 收藏 所属分类:
数据库技术