0

我有一个表,其中包含外部文件系统上文件的位置。用于包含文件位置的列的数据类型是 BFILE。

如何使用java从数据库中读取这些文件?

4

2 回答 2

0

使用以下链接

https://docs.oracle.com/cd/A97335_02/apps.102/a83724/oralob3.htm#1059336

在那里你会发现很多这样的例子

OraclePreparedStatement ops = (OraclePreparedStatement)conn.prepareStatement
                        ("INSERT INTO my_bfile_table VALUES (?,?)");
ops.setString(1,"one");
ops.setBFILE(2, bfile);
ops.execute();
于 2014-11-14T22:00:15.873 回答
0

Hi please read following post and this Link will be helpful

This sample demonstrates Oracle JDBC BFILE support. It illustrates filling a table with BFILEs and includes a utility for dumping the contents of a BFILE. For information on BFILEs.

/* 
 * This sample demonstrate basic File support
 */

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

//including this import makes the code easier to read

import oracle.jdbc.driver.*;

// needed for new BFILE class

import oracle.sql.*;

public class FileExample{

public static void main (String args [])throws Exception{
// Register the Oracle JDBC driver
DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());

// Connect to the database
// You can put a database name after the @ sign in the connection URL.
//
// The sample creates a DIRECTORY and you have to be connected as
// "system" to be able to run the test.
// I you can't connect as "system" have your system manager
// create the directory for you, grant you the rights to it, and
// remove the portion of this program that drops and creates the directory.
Connection conn =
  DriverManager.getConnection ("jdbc:oracle:oci8:@", "system", "manager");

// It's faster when auto commit is off
conn.setAutoCommit (false);

// Create a Statement
Statement stmt = conn.createStatement ();

try
{
  stmt.execute ("drop directory TEST_DIR");
}
catch (SQLException e)
{
  // An error is raised if the directory does not exist.  Just ignore it.
}
stmt.execute ("create directory TEST_DIR as '/tmp/filetest'");

try
{
  stmt.execute ("drop table test_dir_table");
}
catch (SQLException e)
{
  // An error is raised if the table does not exist.  Just ignore it.
}

// Create and populate a table with files
// The files file1 and file2 must exist in the directory TEST_DIR created
// above as symbolic name for /private/local/filetest.
stmt.execute ("create table test_dir_table (x varchar2 (30), b bfile)");
stmt.execute ("insert into test_dir_table values 
               ('one', bfilename ('TEST_DIR', 'file1'))");
stmt.execute ("insert into test_dir_table values 
               ('two', bfilename ('TEST_DIR', 'file2'))");

// Select the file from the table
ResultSet rset = stmt.executeQuery ("select * from test_dir_table");
while (rset.next ())
{
  String x = rset.getString (1);
  BFILE bfile = ((OracleResultSet)rset).getBFILE (2);
  System.out.println (x + " " + bfile);

  // Dump the file contents
  dumpBfile (conn, bfile);
}

// Close all resources
rset.close();
stmt.close();
conn.close();}

// Utility function to dump the contents of a Bfile

static void dumpBfile (Connection conn, BFILE bfile) throws Exception{

System.out.println ("Dumping file " + bfile.getName());

System.out.println ("File exists: " + bfile.fileExists());

System.out.println ("File open: " + bfile.isFileOpen());

System.out.println ("Opening File: ");

bfile.openFile();

System.out.println ("File open: " + bfile.isFileOpen());

long length = bfile.length();
System.out.println ("File length: " + length);

int chunk = 10;

InputStream instream = bfile.getBinaryStream();

// Create temporary buffer for read
byte[] buffer = new byte[chunk];

// Fetch data  
while ((length = instream.read(buffer)) != -1)
{
  System.out.print("Read " + length + " bytes: ");

  for (int i=0; i<length; i++)
    System.out.print(buffer[i]+" ");
  System.out.println();
}

// Close input stream
instream.close();

// close file handler
bfile.closeFile();}}
于 2013-01-13T11:54:35.837 回答