<%@ page language="java" import="java.sql.*,java.util.*"%>
<%
    String image_id = (String) request.getParameter("ID");
    if (image_id != null){
          try 
          {
            Class.forName("oracle.jdbc.driver.OracleDriver");
          Connection con = DriverManager.getConnection("jdbc:oracle:thin:scott/[email protected]:1243:myInstance","java","java");
          Statement stmt = con.createStatement();
        ResultSet rs = stmt.executeQuery("SELECT * FROM IMMAGINE WHERE IMMAGINE_ID = " + image_id);
        if (rs.next())
            {
        String dim_image = rs.getString("IMMAGINE_DIMENSIONE");
        byte [] blocco = rs.getBytes("IMMAGINE_IMMAGINE");
        response.setContentType("image/jpeg");
        ServletOutputStream op = response.getOutputStream();
        for(int i=0;i<Integer.parseInt(dim_image);i++)
        {
            op.write(blocco[i]);
        }
        } 
      rs.close();
      stmt.close();
      con.close();
     } catch(Exception e) {
        out.println("An error occurs : " + e.toString());                
        }            
    }
%>

解决方案 »

  1.   

    保存
    File file = new File("myimage.gif");
    FileInputStream fis = new FileInputStream(file);
    PreparedStatement ps = 
      conn.prepareStatement("insert into images values (?,?)");
    ps.setString(1,file.getName());
    ps.setBinaryStream(2,fis,file.length());
    ps.executeUpdate();
    ps.close();
    fis.close();
      

  2.   

    我要存字符串,而不是存图片或其他文件。因为我还要对存到Clob中的内容进行查询检索。
      

  3.   

    既然你不知道你的字户串多长,可以将他们存为txt文件
      

  4.   

    package com.broadvident.database;import java.sql.*;
    import java.io.*;
    import oracle.sql.*;
    import oracle.jdbc.driver.*;
    import com.broadvident.database.DBConnection;/**
     * <p>Title: Oracle LOB类型数据操作类</p>
     * <p>Description: 操作Oracle的LOB数据类型,主要是<b>读取</b>和<b>更新</b>,此类为Oracle数据库专有类可能无法用在其它数据库上</p>
     * <p>同时应该注意,由于此类所有的方法都是静态的,使用此类时不必实例化,直接采用classname.method()即可。</p>
     * <p>Copyright: Copyright (c) 2002 </p>
     * <p>Company: </p>
     * @author 
     * @version 0.9
     */
    public class DBMS_LOB {

    /**
     * 以字符串的形式返回指定的CLOB字段中存储的内容。
     * @param rs 查询记录集,此方法从ResultSet中当前记录指针指向记录的指定CLOB字段内容。
     * @param lob_fieldName clob字段名,注意如果SQL语句类似于select a as b from table1,则lob_fieldName应该为b而不是a
     * @return String clob内容,以String表示。
     * @throws Exception 抛出的异常,需要在客户调用代码出捕获异常
     */
    public static String getClob(ResultSet rs,String lob_fieldName) throws Exception {

    String result=null;

    if (rs!=null) {

    try{

    CLOB clob=((OracleResultSet)rs).getCLOB(lob_fieldName);

    result=clob.getSubString(1,(int)clob.length());

    }catch(SQLException se){

    System.out.println("**错误: 无法获取CLOB对象!");

    System.out.println(se.getMessage());

    throw se;
    }
    }

    return result;
    }

    /**
     * 以字符串的形式返回指定的CLOB字段中存储的内容。
     * @param rs 查询记录集,此方法从ResultSet中当前记录指针指向记录的指定CLOB字段内容。
     * @param nIndex clob字段索引,依据SQL标准,索引从1开始
     * @return String clob内容,以String表示。
     * @throws Exception 抛出的异常,需要在客户调用代码出捕获异常
     */
    public static String getClob(ResultSet rs,int nIndex) throws Exception {

    String result=null;

    if (rs!=null) {

    try{

    CLOB clob=((OracleResultSet)rs).getCLOB(nIndex);

    result=clob.getSubString(1,(int)clob.length());

    }catch(SQLException se){

    System.out.println("**错误: 无法获取CLOB对象!");

    System.out.println(se.getMessage());

    throw se;
    }
    }

    return result;
    } /**
     * 以字符串的形式返回指定的CLOB字段中存储的内容。
     * @param strSl 查询SQL语句
     * @param lob_fieldName clob字段名称
     * @return String clob内容,以String表示。
     * @throws Exception 抛出的异常,需要在客户调用代码出捕获异常
     */
    public static String getClob(String strSql,String lob_fieldName) throws Exception {

    DBConnection conn=new DBConnection();

    ResultSet rs=null;

    String result=null;

    try{

    rs=conn.executeQuery(strSql);

    if (rs.next()) {

    CLOB clob=((OracleResultSet)rs).getCLOB(lob_fieldName);

    result=clob.getSubString(1,(int)clob.length());

    }

    rs.close();

    rs=null;
    }
    catch(SQLException se) {

    System.out.println("**错误: 无法获取CLOB字段内容!");

    System.out.println(se.getMessage());

    throw se;
    }

    try{

    conn.close();

    }catch(Exception e){

    System.out.println("**错误: 返还连接错误!");

    System.out.println(e.getMessage());

    throw e;
    } return result;
    } /**
     * 以字符串的形式返回指定的CLOB字段中存储的内容。本方法默认SQL语句的第一个查询字段为CLOB字段
     * @param strSl 查询SQL语句
     * @return String clob内容,以String表示。
     * @throws Exception 抛出的异常,需要在客户调用代码出捕获异常
     */
    public static String getClob(String strSql) throws Exception {

    DBConnection conn=new DBConnection();

    ResultSet rs=null;

    String result=null;

    try{

    rs=conn.executeQuery(strSql);

    if (rs.next()) {

    CLOB clob=((OracleResultSet)rs).getCLOB(1);

    result=clob.getSubString(1,(int)clob.length());

    }

    rs.close();

    rs=null;
    }
    catch(SQLException se) {

    System.out.println("**错误: 无法获取CLOB字段内容!");

    System.out.println(se.getMessage());

    throw se;
    }

    try{

    conn.close();

    }catch(Exception e){

    System.out.println("**错误: 返还连接错误!");

    System.out.println(e.getMessage());

    throw e;
    } return result;
    } /**
     * 以字符串的形式返回指定的CLOB字段中存储的内容。本方法默认SQL语句的第一个查询字段为CLOB字段
     * @param strSl 查询SQL语句
     * @param nIndex clob字段索引
     * @return String clob内容,以String表示。
     * @throws Exception 抛出的异常,需要在客户调用代码出捕获异常
     */
    public static String getClob(String strSql,int nIndex) throws Exception {

    DBConnection conn=new DBConnection();

    ResultSet rs=null;

    String result=null;

    try{

    rs=conn.executeQuery(strSql);

    if (rs.next()) {

    CLOB clob=((OracleResultSet)rs).getCLOB(nIndex);

    result=clob.getSubString(1,(int)clob.length());

    }

    rs.close();

    rs=null;
    }
    catch(SQLException se) {

    System.out.println("**错误: 无法获取CLOB字段内容!");

    System.out.println(se.getMessage());

    throw se;
    }

    try{

    conn.close();

    }catch(Exception e){

    System.out.println("**错误: 返还连接错误!");

    System.out.println(e.getMessage());

    throw e;
    } return result;
    }
      

  5.   

    //继续---后半部分
    /**
     * 更新一个CLOB字段中的数据,但是要求这个CLOB字段不可以为null,CLOB字段中原有的数据将被清除
     * @param strSql select语句,选出CLOB字段,例:select id,lob_text from table1 where id=5 for update ,因为要更新,所以for update子句不可省略
     * @param lob_fieldName CLOB字段名。
     * @param strValue 要更新的CLOB字段值
     * @throws Exception 更新过程中抛出的异常,需要在客户代码中捕获 
     */
    public static void setClob(String strSql,String lob_fieldName,String strValue) throws Exception {     PreparedStatement stmt = null;
        
        //OracleCallableStatement cstmt=null;
        
        ResultSet rs =  null;
        
        Writer writer; DBConnection conn=new DBConnection();

    try{     conn.beginTrans();

    stmt = conn.getPreparedStmt(strSql,ResultSet.TYPE_FORWARD_ONLY,ResultSet.CONCUR_UPDATABLE);
        rs =  stmt.executeQuery();
        
        if (rs.next()) {      try{
        
         CLOB clob =(CLOB) rs.getClob(lob_fieldName);
        
         clob.trim(0);
        
         //调用oracle dbms_lob包以读写模式打开CLOB
         /* cstmt=(OracleCallableStatement)(conn.getConnection().prepareCall("BEGIN DBMS_LOB.OPEN(?,DBMS_LOB.LOB_READWRITE);END;"));
        
         cstmt.setCLOB(1,clob);
        
         cstmt.execute();
        
         //裁减原有lob字段长度为零
         cstmt=(OracleCallableStatement)(conn.getConnection().prepareCall("BEGIN DBMS_LOB.TRIM(?,0);END;"));
        
         cstmt.setCLOB(1,clob);
        
         cstmt.execute();
        
         //关闭CLOB
         cstmt=(OracleCallableStatement)(conn.getConnection().prepareCall("BEGIN DBMS_LOB.CLOSE(?);END;"));
        
         cstmt.setCLOB(1,clob);
        
         cstmt.execute(); */
        

        
         writer=clob.getCharacterOutputStream();
        
         writer.write(strValue);
        
         writer.flush();
        
         writer.close();
        
         conn.commit();
         }
         catch(Exception e) {
        
         conn.rollback();
        
         System.out.println("**错误:存储CLOB对象异常: "+e.getMessage());
        
         throw e;
         }
        }
        else {
        
         conn.rollback();
        }
             
        rs.close();

    stmt.close();

    conn.close();

    }catch(SQLException e){

             System.out.println("**错误:存储CLOB对象异常: "+e.getMessage());
            
             throw e;
        }
    }

    /**
     * 更新一个CLOB字段中的数据,但是要求这个CLOB字段不可以为null,CLOB字段中原有的数据将被清除
     * @param strSql select语句,选出CLOB字段,例:select id,lob_text from table1 where id=5 for update ,因为要更新,所以for update子句不可省略
     * @param nIndex CLOB字段索引值,依据SQL规范,字段索引从1开始。
     * @param strValue 要更新的CLOB字段值
     * @throws Exception 更新过程中抛出的异常,需要在客户代码中捕获 
     */
     public static void setClob(String strSql,int nIndex,String strValue) throws Exception {     PreparedStatement stmt = null;
        
        //OracleCallableStatement cstmt=null;
        
        ResultSet rs =  null;
        
        Writer writer; DBConnection conn=new DBConnection();

    try{     conn.beginTrans();

    stmt = conn.getPreparedStmt(strSql,ResultSet.TYPE_FORWARD_ONLY,ResultSet.CONCUR_UPDATABLE);
        
        rs =  stmt.executeQuery();
        
        if (rs.next()) {      try{
        
         CLOB clob =(CLOB) rs.getClob(nIndex);
        
         clob.trim(0);
        
         //调用oracle dbms_lob包以读写模式打开CLOB
         /* cstmt=(OracleCallableStatement)(conn.getConnection().prepareCall("BEGIN DBMS_LOB.OPEN(?,DBMS_LOB.LOB_READWRITE);END;"));
        
         cstmt.setCLOB(1,clob);
        
         cstmt.execute();
        
         //裁减原有lob字段长度为零
         cstmt=(OracleCallableStatement)(conn.getConnection().prepareCall("BEGIN DBMS_LOB.TRIM(?,0);END;"));
        
         cstmt.setCLOB(1,clob);
        
         cstmt.execute();
        
         //关闭CLOB
         cstmt=(OracleCallableStatement)(conn.getConnection().prepareCall("BEGIN DBMS_LOB.CLOSE(?);END;"));
        
         cstmt.setCLOB(1,clob);
        
         cstmt.execute();  */
        
         writer=clob.getCharacterOutputStream();
        
         writer.write(strValue);
        
         writer.flush();
        
         writer.close();
        
         conn.commit();
         }
         catch(Exception e) {
        
         conn.rollback();
        
         System.out.println("**错误:存储CLOB对象异常: "+e.getMessage());
        
         throw e;
         }
        }
        
        else {
        
         conn.rollback();
        }     
        rs.close();

    stmt.close();

    conn.close();

    }catch(SQLException e){

             System.out.println("**错误:存储CLOB对象异常: "+e.getMessage());
            
             throw e;
        }
    }

    /**
     * 更新一个CLOB字段中的数据,但是要求这个CLOB字段不可以为null,CLOB字段中原有的数据将被清除
     * @param strSql select语句,选出CLOB字段,例:select lob_text from table1 where id=5 for update,如果单纯只选出clob字段,则可以不必指定字段名或索引,因为要更新,所以for update子句不可省略
     * @param strValue 要更新的CLOB字段值
     * @throws Exception 更新过程中抛出的异常,需要在客户代码中捕获 
     */
     public static void setClob(String strSql,String strValue) throws Exception {     PreparedStatement stmt = null;
        
        //OracleCallableStatement cstmt=null;
        
        ResultSet rs =  null;
        
        Writer writer; DBConnection conn=new DBConnection();

    try{     conn.beginTrans();

    stmt = conn.getPreparedStmt(strSql,ResultSet.TYPE_FORWARD_ONLY,ResultSet.CONCUR_UPDATABLE);
        
        rs =  stmt.executeQuery();
        
        if (rs.next()) {      try{
        
         CLOB clob =(CLOB) rs.getClob(1);
        
         clob.trim(0);
        
         //调用oracle dbms_lob包以读写模式打开CLOB
         /* cstmt=(OracleCallableStatement)(conn.getConnection().prepareCall("BEGIN DBMS_LOB.OPEN(?,DBMS_LOB.LOB_READWRITE);END;"));      cstmt.setCLOB(1,clob);
        
         cstmt.execute();
        
         //裁减原有lob字段长度为零
         cstmt=(OracleCallableStatement)(conn.getConnection().prepareCall("BEGIN DBMS_LOB.TRIM(?,0);END;"));
        
         cstmt.setCLOB(1,clob);
        
         cstmt.execute();
        
         //关闭CLOB
         cstmt=(OracleCallableStatement)(conn.getConnection().prepareCall("BEGIN DBMS_LOB.CLOSE(?);END;"));
        
         cstmt.setCLOB(1,clob);
        
         cstmt.execute(); */
        
         writer=clob.getCharacterOutputStream();
        
         writer.write(strValue);
        
         writer.flush();
        
         writer.close();
        
         conn.commit();
         }
         catch(Exception e) {
        
         conn.rollback();
        
         System.out.println("**错误:存储CLOB对象异常: "+e.getMessage());
        
         throw e;
         }
        }
        
        else {
        
         conn.rollback();
        }
        
        rs.close();

    stmt.close();

    conn.close();

    }catch(SQLException e){

             System.out.println("**错误:存储CLOB对象异常: "+e.getMessage());
            
             throw e;
        }
    }

      

  6.   

    //继续---最后部分
    /**
     * 更新一个CLOB字段中的数据,但是要求这个CLOB字段不可以为null,CLOB字段中原有的数据将被清除,并且必须在事务中调用此方法
     * @param rs 记录集对象,必须使用Select a,b,c from d for update 生成的。
     * @param lob_fieldName CLOB字段名
     * @param strValue 要更新的CLOB字段值
     * @throws Exception 更新过程中抛出的异常,需要在客户代码中捕获 
     */
     public static void setClob(ResultSet rs,String lob_fieldName,String strValue) throws Exception {     //OracleCallableStatement cstmt=null;
        
        Writer writer;

        if (rs!=null) {      try{
        
         CLOB clob =(CLOB) rs.getClob(lob_fieldName);
        
         clob.trim(0);
        
         //调用oracle dbms_lob包以读写模式打开CLOB
         /* cstmt=(OracleCallableStatement)(rs.getStatement().getConnection().prepareCall("BEGIN DBMS_LOB.OPEN(?,DBMS_LOB.LOB_READWRITE);END;"));
        
         cstmt.setCLOB(1,clob);
        
         cstmt.execute();
        
         //裁减原有lob字段长度为零
         cstmt=(OracleCallableStatement)(rs.getStatement().getConnection().prepareCall("BEGIN DBMS_LOB.TRIM(?,0);END;"));
        
         cstmt.setCLOB(1,clob);
        
         cstmt.execute();
        
         //关闭CLOB
         cstmt=(OracleCallableStatement)(rs.getStatement().getConnection().prepareCall("BEGIN DBMS_LOB.CLOSE(?);END;"));
        
         cstmt.setCLOB(1,clob);
        
         cstmt.execute();  */
        
         writer=clob.getCharacterOutputStream();
        
         writer.write(strValue);
        
         writer.flush();
        
         writer.close();
        
         }
         catch(Exception e) { System.out.println("**错误:存储CLOB对象异常: "+e.getMessage());

    throw e;
         }
        }
    } /**
     * 更新一个CLOB字段中的数据,但是要求这个CLOB字段不可以为null,CLOB字段中原有的数据将被清除,并且必须在事务中调用此方法
     * @param rs 记录集对象,必须使用Select a,b,c from d for update 生成的。
     * @param nIndex CLOB字段索引,依据SQL标准,索引从1开始
     * @param strValue 要更新的CLOB字段值
     * @throws Exception 更新过程中抛出的异常,需要在客户代码中捕获 
     */
     public static void setClob(ResultSet rs,int nIndex,String strValue) throws Exception {    // OracleCallableStatement cstmt=null;
        
        Writer writer;

        if (rs!=null) {      try{
        
         CLOB clob =(CLOB) rs.getClob(nIndex);
        
         clob.trim(0);
        
         //调用oracle dbms_lob包以读写模式打开CLOB
         /* cstmt=(OracleCallableStatement)(rs.getStatement().getConnection().prepareCall("BEGIN DBMS_LOB.OPEN(?,DBMS_LOB.LOB_READWRITE);END;"));
        
         cstmt.setCLOB(1,clob);
        
         cstmt.execute();
        
         //裁减原有lob字段长度为零
         cstmt=(OracleCallableStatement)(rs.getStatement().getConnection().prepareCall("BEGIN DBMS_LOB.TRIM(?,0);END;"));
        
         cstmt.setCLOB(1,clob);
        
         cstmt.execute();
        
         //关闭CLOB
         cstmt=(OracleCallableStatement)(rs.getStatement().getConnection().prepareCall("BEGIN DBMS_LOB.CLOSE(?);END;"));
        
         cstmt.setCLOB(1,clob);
        
         cstmt.execute();  */
        
         writer=clob.getCharacterOutputStream();
        
         writer.write(strValue);
        
         writer.flush();
        
         writer.close();
        
         }
         catch(Exception e) { System.out.println("**错误:存储CLOB对象异常: "+e.getMessage());

    throw e;
         }
        }
    } /**
     * 更新一个CLOB字段中的数据,但是要求这个CLOB字段不可以为null,CLOB字段中原有的数据将被清除,并且必须在事务中调用此方法
     * @param rs 记录集对象,必须使用Select a,b,c from d for update 生成的。如果单纯只选出clob字段,则可以不必指定字段名或索引,因为要更新,所以for update子句不可省略
     * @param strValue 要更新的CLOB字段值
     * @throws Exception 更新过程中抛出的异常,需要在客户代码中捕获 
     */
     public static void setClob(ResultSet rs,String strValue) throws Exception {    // OracleCallableStatement cstmt=null;
        
        Writer writer;

        if (rs!=null) {      try{
        
         CLOB clob =(CLOB) rs.getClob(1);
        
         clob.trim(0);
        
         //调用oracle dbms_lob包以读写模式打开CLOB
         /* cstmt=(OracleCallableStatement)(rs.getStatement().getConnection().prepareCall("BEGIN DBMS_LOB.OPEN(?,DBMS_LOB.LOB_READWRITE);END;"));
        
         cstmt.setCLOB(1,clob);
        
         cstmt.execute();
        
         //裁减原有lob字段长度为零
         cstmt=(OracleCallableStatement)(rs.getStatement().getConnection().prepareCall("BEGIN DBMS_LOB.TRIM(?,0);END;"));
        
         cstmt.setCLOB(1,clob);
        
         cstmt.execute();
        
         //关闭CLOB
         cstmt=(OracleCallableStatement)(rs.getStatement().getConnection().prepareCall("BEGIN DBMS_LOB.CLOSE(?);END;"));
        
         cstmt.setCLOB(1,clob);
        
         cstmt.execute();  */
        
         writer=clob.getCharacterOutputStream();
        
         writer.write(strValue);
        
         writer.flush();
        
         writer.close();
        
         }
         catch(Exception e) { System.out.println("**错误:存储CLOB对象异常: "+e.getMessage());

    throw e;
         }
        }
    }
    }