【问题标题】:How to pass an array of BLOB to a stored oracle procedure?如何将 BLOB 数组传递给存储的 oracle 过程?
【发布时间】:2014-02-26 20:58:08
【问题描述】:

我允许用户将多个文件上传到我的数据库。这些文件内容必须作为 BLOB 存储在我的 oracle 数据库中。

我如何编写一个 oracle 程序来做到这一点? (我对Oracle存储过程有点了解)?

完成此操作后,如何使用 jdbc 的 CallableStatement 在 java 中使用存储过程?

请帮忙。

【问题讨论】:

  • 1.我的程序是:

标签: java oracle stored-procedures jdbc


【解决方案1】:

首先,您必须创建将包含 BLOB 表的类型:

CREATE OR REPLACE TYPE tab_blobs AS TABLE OF BLOB;

在 Java 中,您必须依赖 Oracle sql 提供的 STRUCT 类型。 您将创建一个 STRUCT,其中包含要存储到 DB 中的 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 ?
    }
}

【讨论】:

  • 什么是“价值”.getBytes();
  • 应该是文件,转换成byte[]。
【解决方案2】:

这是另一个帮助您的尝试。 您可以在此处从 oracle 中找到更多信息: http://docs.oracle.com/cd/B28359_01/java.111/e10788/connect.htm#CHDGHFCG http://docs.oracle.com/javase/tutorial/jdbc/basics/

另外,示例取自(当时我不得不这样做的网站有很大帮助): 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();
    }
}

【讨论】:

    【解决方案3】:

    下面是我的代码希望你得到你的答案:

    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 字节), “性别”字符(1 个字节), “设计” VARCHAR2(20 字节), “年”间隔年 (2) 到月, "ID" NUMBER NOT NULL ENABLE, “出生”日期, “证明”CLOB, “地址”VARCHAR2(100 字节), “电话号码, "RAW1" RAW(20), “图像”斑点, CONSTRAINT "O_USER_PK" PRIMARY KEY ("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;
      

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-02-10
      • 1970-01-01
      • 2011-10-23
      • 1970-01-01
      • 2011-03-03
      • 2017-10-24
      • 2012-03-30
      • 2014-12-08
      相关资源
      最近更新 更多