以下内容是将 图片写入到数据库的 Blob 字段,但不知为什么,执行后,没有异常,但没有数据写入到数据库表中。
____________________________________________
____________________________________________
package mytest;import java.sql.*;
import oracle.sql.*;
import java.io.*;
//import org.apache.commons.fileupload.*;
public class Myconn { /**
 * @param args
 */
public Connection GetConn(){
Connection conn = null;

return conn;
}
public static void main(String[] args) {
// TODO Auto-generated method stub System.out.println("hello ");
OutputStream outstream = null;
InputStream  instream  = null;
try{
Class.forName("oracle.jdbc.driver.OracleDriver"); Connection con = DriverManager.getConnection(           "jdbc:oracle:thin:@192.168.0.11:1521:rantu", "mytest", "123456");// con.setAutoCommit(false); Statement st = con.createStatement();// st.executeUpdate("insert into BLOBIMG  values('"+2008+"',empty_blob())");

//for update 方式锁定数据行
ResultSet rs = st.executeQuery("select B_cont from BlobImg where B_id ='"+2008+"' for update");

if(rs.next()){
System.out.println("已经select出数据------46-----------");
BLOB  blob =(oracle.sql.BLOB)rs.getBlob(1);

//得到数据库的输出流
try{
outstream = blob.getBinaryOutputStream();

File file = new File("g:\\logo-yy.gif");

    instream = new FileInputStream(file);

//将输入流写到输出流
byte[] b = new byte[blob.getBufferSize()];
int len = 0;
while((len=instream.read(b))!=-1){
outstream.write(b,0,len);
System.out.println("将文件写到到   -----Blob");
}
}catch(IOException ie){
System.out.println(ie.getMessage()+"读取文件有误");

}finally{
try{
instream.close();
outstream.flush();
outstream.close();
}catch(IOException ioex){ioex.getMessage();}

}

try{
ResultSet rst = st.executeQuery("select b_cont from blobimg where b_id ='"+2008+"'");
if(rst.next()){
System.out.println("已经select出数据-----------78------");
java.sql.Blob jblob = rst.getBlob(1);
instream = jblob.getBinaryStream();
File outfile = new File("g:\\logo-yyaaaaaaaaaaaaaaaaaaa.gif");
if(outfile.exists()){
outfile.delete();
}
outfile.createNewFile();
try{
outstream = new FileOutputStream(outfile);

//将Blob写入到文件
byte[] b = new byte[1024];
int leo = 0;
while((leo=instream.read(b))!=-1){
outstream.write(b,0,leo);
}
}catch(IOException ioex){System.out.println("将Blob写入到文件时出错!"+ioex.getMessage());}
finally{
outstream.close();
instream.close();

}
}
}catch(SQLException sqle){System.out.println("取Blob时出错!"+sqle.getMessage());}
catch(IOException io){System.out.println(io.getMessage());}


}

}catch(ClassNotFoundException cnfe ){
System.out.println("驱动注册错误"+cnfe.getMessage());
}
catch(SQLException sqlex){
System.out.println("连接数据库失败!"+sqlex.getMessage());
} }}
____________________________________________
____________________________________________
将图片写入到数据库的 Blob 字段,代码至此结束以下是将Blob中的内容,输出到jsp页面  viewblob-bak.jsp 将接收 view.jsp页面传来的参数 b_id  ,以下是viewblob-bak.jsp 页面的代码
--------------------------------------
<%@ page language="java" import="java.util.*" pageEncoding="GBk"%>
<%@ page import="rantu.common.database.*" %>
<%@ page import="com.rantukj.util.*"%>
<%@ page import="java.sql.*" %>
<%@ page import="java.io.*" %>
<%
Connection conn = null;
Statement stmt = null;
ResultSet rst = null;
GetConn gc = new GetConn();
conn = gc.GetConn();
stmt = conn.createStatement(ResultSet.TYPE_FORWARD_ONLY,ResultSet.CONCUR_READ_ONLY);
String sql = "select b_cont from blobimg where b_id='"+request.getParameter("b_id")+"'";
System.out.println(sql+"---------13------");
byte [] buff = null;
byte [] bu = null;
try{
rst = stmt.executeQuery(sql);String filename = "b_cont";
if(rst.next()){
buff = new LobUtil().readBlob(rst,filename);
//InputStream in = rst.getBlob("b_cont").getBinaryStream();
//bu = new byte[(int)rst.getBlob("b_cont").length()];
//if(in!=null){
//in.read(bu);
//in.close();
//}
    rst.close();
    stmt.close();
    conn.close();    
}else{
rst.close();
}
}catch(SQLException sqlex){
System.out.println(sqlex.getMessage()+"   >>>>>>在页面中显示blob图片时出错");
}
catch(Exception iox){System.out.println(iox.getMessage()+"-----错");}
finally{
stmt.close();
conn.close();

}OutputStream os = response.getOutputStream();
response.setContentType("image/gif");if(buff!=null){
System.out.println("start ");
os.write(buff);
response.getOutputStream().write(buff);
//for(int i = 0;i<buff.length;i++){
//os.write(buff[i]);
//}
}os.close();
 %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
  </head>
  
  <body>
    
  </body>
</html>--------------------------------------
以下是将Blob中的内容,输出到jsp页面代码至此结束
以下是显示图片的 view.jsp
----------------------------------------------------------------
<%@ page language="java" import="java.util.*" pageEncoding="GBk"%>
<html>
  <head>
    <title>viewblob.html</title>    
    <!--<link rel="stylesheet" type="text/css" href="./styles.css">-->  </head>
  
  <body>
    <img src="viewblob-bak.jsp?b_id=2008" >---显示图片
  </body>
</html>
-----------------------------------------------------
至此结束