⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 lobutils.java

📁 OA典型例子
💻 JAVA
字号:
/*
 * LobUtils.java
 * Copyright (c) 2000-2002 Jinsong Chu.
 * License http://www.gnu.org/copyleft
 */
package com.sure.util;

//import oracle.sql.*;
import java.io.*;
import java.sql.*;

/**
 * 存取Lob数据的工具类,不同的数据库的具体实现可能不一样,但接口一致
 */
public class LobUtils {

    /**
     * 从数据库中获取一个Oracle的BLOB对象
     * @param conn 数据库连接
     * @param fileldName 表中的Blob字段的名称
     * @param tableName 表的名称
     * @param where 条件子句
     * @return 返回Blob对象
     * @throws SQLException
     */
    public static Blob loadBlob(Connection conn, String fileldName,
            String tableName, String where) throws SQLException {
        String sql = "Select " + fileldName + " from " + tableName + " ";
        Blob blob = new BlobImp();
        if (where.length() != 0)
            sql += where;
        Statement st = conn.createStatement();
        ResultSet rs = st.executeQuery(sql);
        if(rs.next()){
          ((BlobImp)blob).setContent(rs.getBytes(1));
        }

        rs.close();
        st.close();
        return blob;
    }

    /**
     * 将指定的Blob字段置空
     * @param conn 数据库连接
     * @param fileldName 表中的Blob字段的名称
     * @param tableName 表的名称
     * @param where 条件子句
     * @throws SQLException
     */
    public static void emptyBlob(Connection conn, String fileldName,
            String tableName, String where) throws SQLException {
        String sql = "Update " + tableName + " Set " + fileldName + "=null ";
        if (where.length() != 0)
          sql += where;
        Statement st = conn.createStatement();
        st.executeUpdate(sql);
        st.close();
    }

    /**
     * 将二进制数据填充到一个Oracle的BLOB对象
     * @param blob 待填充的Blob对象
     * @param instream 输入流
     * @throws SQLException
     * @throws IOException
     */
    public static void fillBlob(Blob blob, InputStream instream)
            throws SQLException, IOException {
        ((BlobImp)blob).setContent(instream);
    }

    /**
     * 将输入流输出到指定的输出流
     * @param in 输入流
     * @param out 输出流
     * @param bufferSize 缓冲区大小,建议为1024字节
     * @throws SQLException
     * @throws IOException
     */
    public static void dumpBlob(InputStream in, OutputStream out,
            int bufferSize) throws SQLException, IOException {
        byte[] buffer = new byte[bufferSize];
        int length = 0;
        while ((length = in.read(buffer)) != -1) // Read from Blob stream
          out.write(buffer,0,length); // Write to file stream
    }

    /**
     * 将Blob数据流输出到指定的输出流
     * @param blob 待输出的Blob对象
     * @param out 输出流
     * @throws SQLException
     * @throws IOException
     */
    public static void dumpBlob(Blob blob, OutputStream out)
            throws SQLException, IOException {
        InputStream in = blob.getBinaryStream();
        int bufferSize = ((BlobImp)blob).getBufferSize();
        dumpBlob(in, out, bufferSize);
        in.close();
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -