rs=st.executeQuery("select*from myblob where id=1");
if(rs.next())
{
blob=rs.getBlob("fblob");
ImageIcon icon=new ImageIcon(blob.getBytes(1,(int)blob.length()));
JLabel l=new JLabel(icon);
JFrame jf=new JFrame("mywife");
jf.getContentPane().add(l);
jf.pack();
jf.setVisible(true);
}
}
catch(SQLException se)
{
se.printStackTrace();
}
finally
{
//——finally——start
if(rs!=null)
{
try
{
rs.close();
}
catch(SQLException e)
{
e.printStackTrace();
}
}
//////////////////////////////if(st!=null)
{
try
{
st.close();
}
catch(SQLException e)
{
e.printStackTrace();
}
}
/////////////////////////////
if(conn!=null)
{
try
{
conn.close();
}
catch(SQLException e)
{
e.printStackTrace();
}
}
}
}
}
刚才的程序使用了Swing技术。下面再给出一个用JSP实现的例子。
案例名称:图片数据的读取和在网页上显示
程序名称:showimg.jsp
<%@ page contentType="text/html;charset=gbk"%>
<%@ page import="java.sql.*,java.io.*"%>
<%!
Connection conn=null;
Statement stmt=null;
ResultSet rs=null;
%>
<%
//安装驱动程序——
try
{
Class.forName("oracle.jdbc.driver.OracleDriver");
}
catch(ClassNotFoundException ce)
{
out.println(ce.getMessage());
}
//执行——
try
{
//获得Connection对象
String url="jdbc:oracle:thin:@ localhost:1521:yuanyuan";
conn=DriverManager.getConnection(url,"scott","tiger");
stmt=conn.createStatement();
rs=stmt.executeQuery("SELECT*FROM myblob where id=1");
if(rs.next())
{
…Blob blob=rs.getBlob("fblob");
byte[] buf=blob.getBytes(1,(int)blob.length());
response.setContentType("image/jpeg");
//图片的输出流
OutputStream wt=response.getOutputStream();
//将缓冲区的输入输出到页面
wt.write(buf);
//输入完毕,清除缓冲
wt.flush();
wt.close();
}
}
catch(SQLException e)
{
System.out.println(e.getMessage());
}
finally
{
if(rs!=null)
{
…try
{
rs.close();
}
catch(SQLException ee)
{
System.out.println(ee.getMessage());
}
}
if(stmt!=null)
{
…try
{
stmt.close();
}
catch(SQLException ee)
{
System.out.println(ee.getMessage());
}
}
if(conn!=null)
{
…try
{
conn.close();
}
catch(SQLException ee)
{
System.out.println(ee.getMessage());
}
}
}
%>
5.5.2BLOB的搜索
现在,为fblob列创建索引,语句如下:
create index idx myblob on myblob(fblob)indextype is ctxsys.context;
索引建立好之后,就可以执行全文搜索了。执行下面的语句,在fblob列中搜索“大禹”。
select id,fname from myblob where contains(fblob,"大禹")>0;
5.5.3BLOB搜索的应用
现在,有了前面学习的关于Oracle 二进制文档全文搜索的知识,可以做两个简单的应用。
下面这两个例子使用前面建立的myblob数据表,一个是桌面应用,一个是Web应用。使用的技术分别为:Oracle9i+Java Swing,Oracle9i+JSP2.4。
这两个应用允许用户输入关键词,然后单击“搜索”按钮,执行全文搜索,显示搜索结果。
案例名称:Oracle二进制文档全文搜索桌面应用
程序名称:BinSearcher.java
import java.util.*;
import java.io.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.sql.*;
public classBinSearcher
{
//搜索关键词
static JTextField jtf=null;
//搜索按钮
static JButton jb=null;
//文本区域
static JTextArea jta;
private static void createAndShowGUI()
{
…JFrame.setDefaultLookAndFeelDecorated(true);
JFrame frame=new JFrame("Binary Searcher!yutianen@163.com");
frame.setDefaultCloseOperation(JFrame.EXIT ONCLOSE);
Container con=frame.getContentPane();
con.setLayout(newBorderLayout());
JPanel jpup=new JPanel();
jpup.setLayout(new GridLayout(1,2));
jtf=new JTextField(30);
jb=new JButton("搜索");
jb.addActionListener
(
newActionListener()
{