从oracle 11g xe获取Blob文件时创建的空文件

时间:2014-02-10 14:17:17

标签: java sql oracle jdbc oracle11g

下面给出了

的两个程序

1>将blob文件存储到数据库中

2 - ;从数据库中检索blob文件

1。将blob文件存储到数据库中

package jdbc;

import java.net.URL;
import java.sql.*;
import java.io.*;
import java.util.Properties;



public class Storing_Image {

public static void main(String[] args) throws Exception{

    Properties p = new Properties();
    p.put("user", "system");
    p.put("password", "password");

    URL url = Storing_Clob.class.getResource("/images/bunny.jpg");
    File file = new File(url.toURI());
    FileInputStream fis = new FileInputStream(file);

    DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());

    Connection con = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe",p);

    PreparedStatement pstmt = con.prepareStatement("insert into myblob(BLOB_FILE) values(?)");

 pstmt.setBinaryStream(1, fis, (byte)file.length());

    System.out.println("Length of string: "+file.length());
    System.out.println("No of rows affected: "+pstmt.executeUpdate());
    con.close();
}
}

myblob表只有一个Blob数据类型。

插入表格后,我执行了以下命令: -

select * from myblob;

给出了以下输出: -

myblob.jpg]![result of myblob table contents

我猜是表格中图片的字节值。

2.从数据库中检索Blob文件(图像)

import java.sql.*;
import java.util.Properties;
import java.io.*;

public class Retrieving_Image {

public static void main(String[] args) throws Exception{

    Properties p = new Properties();
    p.put("user", "system");
    p.put("password", "password");

    DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
    Connection con = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe",p);

    PreparedStatement pstmt = con.prepareStatement("select BLOB_FILE from myblob",ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);

    ResultSet rs = pstmt.executeQuery();

    rs.first();

        InputStream is = rs.getBinaryStream(1);

    FileOutputStream fos = new FileOutputStream("H:/result1.jpg");


    while(((byte) is.read())!=-1)
    {
        fos.write((byte) is.read());
    }

    fos.close();
    con.close();
 }
}

给出以下输出: -

image fetched from database

你可以看到一个空白图像,即0kb。 我正在使用oracle 11g xe和ojdbc6_g.jar驱动程序。

0 个答案:

没有答案