簡體   English   中英

如何將BLOB數組傳遞給存儲的oracle過程?

[英]How to pass an array of BLOB to a stored oracle procedure?

我允許用戶將多個文件上傳到我的數據庫。 這些文件內容必須作為BLOB存儲在我的oracle數據庫中。

我如何編寫一個oracle過程來做到這一點? (我對Oracle存儲過程有一點了解)?

完成此操作后,如何使用jdbc的CallableStatement在Java中使用存儲過程?

請幫忙。

首先,您必須創建將包含BLOB表的類型:

CREATE OR REPLACE TYPE tab_blobs AS TABLE OF BLOB;

在Java中,您必須依靠Oracle sql提供的STRUCT類型。 您將創建一個STRUCT,其中包含要存儲到數據庫中的BLOB數組。

該代碼如下所示:

import java.sql.Connection;
import java.sql.SQLException;

import oracle.jdbc.driver.OracleDriver;
import oracle.sql.ARRAY;
import oracle.sql.ArrayDescriptor;
import oracle.sql.STRUCT;
import oracle.sql.StructDescriptor;

public class ArrayDemo
{

    public static void passArray()
        throws SQLException
    {
        Connection conn = new OracleDriver().defaultConnection();

        byte[] fileInByteArray = "value".getBytes();
        StructDescriptor itemDescriptor = StructDescriptor.createDescriptor("BLOB", conn);

        Object[] itemAtributes = new Object[] {};
        STRUCT itemObject1 = new STRUCT(itemDescriptor, conn, itemAtributes);

        itemAtributes = new Object[] {};
        STRUCT itemObject2 = new STRUCT(itemDescriptor, conn, itemAtributes);

        STRUCT[] idsArray = { itemObject1, itemObject2 };

        ArrayDescriptor descriptor = ArrayDescriptor.createDescriptor("IDS_TABLE", conn);

        ARRAY array_to_pass = new ARRAY(descriptor, conn, idsArray);

        OraclePreparedStatement ps = (OraclePreparedStatement) conn.prepareStatement("begin getInfo(:x); end;");

        ps.setARRAY(1, array_to_pass);
        ps.execute();

    }
}

但是為什么不通過遍歷文件,一個接一個地插入它們來簡化處理:

public static void insererBlob(String name, String path) {
   File file = new File(path);
   try{
      //link to DB
      Connection connection = DriverManager.getConnection("url","user","password");

      //link to file
      FileInputStream stream = new FileInputStream(file);

      //prepare the SQL instruction
      String sql = "INSERT INTO file_table VALUES (?, ?)";
      PreparedStatement statement = connection.prepareStatement(sql);

      //blob insertion
      statement.setString(1, name);
      statement.setBinaryStream(2, stream, (int)file.length());
      statement.executeUpdate();

    }catch(Exception e){
       //ERROR SQL, IO, etc .
    }finally {
       //close connection ?
    }
}

詳細信息可以在此鏈接找到, 使用Oracle數據庫進行文件的裝卸

這是另一種嘗試來幫助您。 您可以在此處從oracle找到更多信息: http : //docs.oracle.com/cd/B28359_01/java.111/e10788/connect.htm#CHDGHFCG http://docs.oracle.com/javase/tutorial/jdbc/基本/

另外,示例取自(我不得不這樣做的時候在我的網站上提供了很大幫助): http : //docs.oracle.com/javase/tutorial/jdbc/basics/blob.html

public void addRowToCoffeeDescriptions(
    String coffeeName, String fileName)
    throws SQLException {

    PreparedStatement pstmt = null;
    try {
        Clob myClob = this.con.createClob();
        Writer clobWriter = myClob.setCharacterStream(1);
        String str = this.readFile(fileName, clobWriter);
        System.out.println("Wrote the following: " +
            clobWriter.toString());

        if (this.settings.dbms.equals("mysql")) {
            System.out.println(
                "MySQL, setting String in Clob " +
                "object with setString method");
            myClob.setString(1, str);
        }
        System.out.println("Length of Clob: " + myClob.length());

        String sql = "INSERT INTO COFFEE_DESCRIPTIONS " +
                     "VALUES(?,?)";

        pstmt = this.con.prepareStatement(sql);
        pstmt.setString(1, coffeeName);
        pstmt.setClob(2, myClob);
        pstmt.executeUpdate();
    } catch (SQLException sqlex) {
        JDBCTutorialUtilities.printSQLException(sqlex);
    } catch (Exception ex) {
      System.out.println("Unexpected exception: " + ex.toString());
    } finally {
        if (pstmt != null)pstmt.close();
    }
}

以下是我的代碼,希望您能得到答案:

  1. 從Java代碼:

      try {Class.forName("oracle.jdbc.driver.OracleDriver"); String url = "jdbc:oracle:thin:@localhost:1521:orcl"; Connection con = DriverManager.getConnection(url, db_user, password); System.out.println("Connected to database"); SimpleDateFormat simpleDateFormat = new SimpleDateFormat("dd/MM/yyyy"); Date now = new java.sql.Date(simpleDateFormat.parse("12/02/2001").getTime()); String command2 = "{call USER1(?,?,?,?)}"; String path; File[] roots = File.listRoots(); path=roots[0].getPath()+"dfg.jpg"; System.out.println("path: " + path); //shows drives in you computer for(int i = 0; i < roots.length ; i++){ System.out.println("drive: " + roots[i].getPath()); } CallableStatement insertStatment = con.prepareCall(command2); insertStatment.setInt(1, 18); insertStatment.setString(2, "ankssaait"); insertStatment.setDate(3, now); File file = new File(path); //link to file FileInputStream stream = new FileInputStream(file); insertStatment.setBinaryStream(4, stream,(int)file.length());; System.out.println("onExecute: "+ insertStatment.executeUpdate()); insertStatment.close(); System.out.println("done"); } catch (Exception e) { e.printStackTrace(); } 
  2. 我的桌子是:

    創建表“ ANKIT”。“ O_USER”(“名稱” VARCHAR2(20 BYTE),“ GENDER” CHAR(1 BYTE),“ DESIGNTION” VARCHAR2(20 BYTE),“ YEAR”間隔年(2)到MONTH,“ ID “ NUMBER NOT NULL ENABLE,” DOB“日期,” PROOF“ CLOB,” ADDRESS“ VARCHAR2(100 BYTE),” TELEPHONE“ NUMBER,” RAW1“ RAW(20),” IMAGE“ BLOB,CONSTRAINT” O_USER_PK“主鍵( “ID”));

  3. 我的程序是:

     CREATE OR REPLACE PROCEDURE USER1 (U_ID IN o_user.id%TYPE, U_NAME in o_user.name%TYPE, u_DOB in o_user.dob%TYPE, u_image in o_user.image%TYPE) AS BEGIN insert into o_user(id,name,dob,image) values(u_id,u_name,u_dob,u_image); END USER1; 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM