DB2中BLOB字段的处理

//DB2插入blob字段数据
public void insertBlobData()
{
    String url = "jdbc:db2:tar20";
        String user = "db2admin";
        String password = "target";

        try {
            Class.forName("COM.ibm.db2.jdbc.app.DB2Driver").newInstance();
            Connection conn = DriverManager.getConnection(url, user, password);
            PreparedStatement preparedStatement =
                    conn.prepareStatement(
                            "INSERT INTO SHERP.TAR_T_EXECINFOFILE("
                            +
                            "TASKCODE, EXECTIME, SORTNO, FILEID, FILENAME, FILEDETAIL)"
                            + " VALUES(?,?,?,?,?,?)");
            File docFile =
                    new File(
                            "e://test.doc");
            InputStream inputStream = new FileInputStream(docFile);
            preparedStatement.setInt(1, 44);
            preparedStatement.setString(2, "2006-08-08");
            preparedStatement.setInt(3, 1);
            preparedStatement.setInt(4, 1);
            preparedStatement.setString(5, "测试");
            preparedStatement.setBinaryStream(6, inputStream,
                                              (int) (docFile.length()));
            preparedStatement.executeUpdate();

        } catch (ClassNotFoundException cnfe) {
            cnfe.printStackTrace();
        } catch (IllegalAccessException iae) {
            iae.printStackTrace();
        } catch (InstantiationException ie) {
            ie.printStackTrace();
        } catch (SQLException sqle) {
            sqle.printStackTrace();
        } catch (FileNotFoundException fnfe) {
            fnfe.printStackTrace();
        }
}


//获取blob字段数据
public void getBlobData()
{
    String url = "jdbc:db2:tar20";
        String user = "db2admin";
        String password = "target";

        try {
            Class.forName("COM.ibm.db2.jdbc.app.DB2Driver").newInstance();
            Connection conn = DriverManager.getConnection(url, user, password);
            PreparedStatement preparedStatement =
                    conn.prepareStatement(
                            "SELECT FILEDETAIL FROM SHERP.TAR_T_EXECINFOFILE WHERE FILEID=?");
            preparedStatement.setInt(1, 3);
            ResultSet resultSet = preparedStatement.executeQuery();
            while (resultSet.next()) {
                Blob blob = resultSet.getBlob("FILEDETAIL");
                InputStream inputStream = blob.getBinaryStream();

                File fileOutput =
                        new File(
                                "e://demo777.doc");
                FileOutputStream fo =
                        new FileOutputStream(fileOutput);
                int c;
                while ((c = inputStream.read()) != -1) {
                    fo.write(c);
                }
                fo.close();
            }

        } catch (ClassNotFoundException ex) {
        } catch (IllegalAccessException ex) {
        } catch (InstantiationException ex) {
        } catch (SQLException ex) {
            /** @todo Handle this exception */
        } catch (FileNotFoundException ex) {
            /** @todo Handle this exception */
        } catch (IOException ex) {
            /** @todo Handle this exception */
        }
}


//QueryDataSet获取blob数据
/**
     * 此方法和getBytes的不同之处是,这里需要两个字符转成一个字节码。
     * @param str String
     * @return byte[]
     */
    private byte[] str2ByteArray(String str) {
        int length = 0;
        if (str.length() % 2 == 0) {
            length = str.length() / 2;
        } else {
            length = str.length() / 2 + 1;
        }
        byte[] result = new byte[length];
        String s = "";
        int j = 0;
        for (int i = 0; i < str.length(); i += 2) {
            if ((i + 2) < str.length()) {
                s = str.substring(i, i + 2);
            } else {
                s = str.substring(i, str.length());
            }
            if (!s.equals("") && !s.equals("##")) {
                byte b = new Integer(Integer.parseInt(s, 16)).byteValue();
                result[j++] = b;
            }
        }
        return result;
    }

    //测试blob字段处理
    public void btnTest_actionPerformed(ActionEvent e) {

        String url = "jdbc:db2:tar20";
        String user = "db2admin";
        String password = "target";

        try {
            Class.forName("COM.ibm.db2.jdbc.app.DB2Driver").newInstance();
            Connection conn = DriverManager.getConnection(url, user, password);
            Database db = new Database(conn);
            String sql =
                    "select FILEDETAIL from SHERP.TAR_T_EXECINFOFILE where FILEID=1";
            qdsTemp.setQuery(new QueryDescriptor(db, sql));
            qdsTemp.executeQuery();

            String str = qdsTemp.getString("FILEDETAIL");

            InputStream in = new ByteArrayInputStream(this.str2ByteArray(str));
            FileOutputStream out = new FileOutputStream("E://demo888.doc");
            int c = 0;
            while ((c = in.read()) != -1) {
                out.write(c);
            }
            in.close();
            out.close();

        } catch (ClassNotFoundException cnfe) {
            cnfe.printStackTrace();
        } catch (IllegalAccessException iae) {
            iae.printStackTrace();
        } catch (InstantiationException ie) {
            ie.printStackTrace();
        } catch (SQLException sqle) {
            sqle.printStackTrace();
        } catch (FileNotFoundException ex) {
            /** @todo Handle this exception */
        } catch (IOException ex) {
            /** @todo Handle this exception */
        }

    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值