I want to array BLOB data save to Oracle DB using by stored procedure. (reference from this : Pass array from Java to Oracle: java.sql.SQLException: Fail to convert to internal representation:error)
But I have added BLOB column to struct. Here is my db script and java code.
--DB code-- CREATE TABLE project_types ( proj_id VARCHAR2(10), proj_title VARCHAR2(10), proj_data BLOB ); /
CREATE OR REPLACE TYPE project_type AS OBJECT (
proj_id VARCHAR2(10),
proj_title VARCHAR2(10),
proj_data BLOB
);
/
CREATE OR REPLACE TYPE my_array AS TABLE OF project_type;
/
CREATE OR REPLACE PROCEDURE add_projects(p_projects_array IN my_array)
AS
BEGIN
IF p_projects_array IS NOT NULL THEN
FOR v_i IN 1..p_projects_array.LAST
LOOP
INSERT INTO project_types
VALUES (p_projects_array(v_i).proj_id,
p_projects_array(v_i).proj_title,
p_projects_array(v_i).proj_data);
END LOOP;
END IF;
END;
/
I have added BLOB data column to project_types table and project_type TYPE, SturedProcedure.
--Table--
proj_data BLOB
-- TYPE--
proj_data BLOB
--StruedProcedure--
p_projects_array(v_i).proj_data
-- Java code --
import java.sql.Array;
import java.sql.Blob;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Struct;
import javax.sql.rowset.serial.SerialBlob;
import oracle.jdbc.OracleCallableStatement;
import oracle.jdbc.driver.OracleConnection;
public class calMilisecond {
public static void main(String[] args) throws Exception {
String url = "jdbc:oracle:thin:@localhost:1521:orcl";
String user = "testuser";
String password = "testuser123456";
Connection conn = DriverManager.getConnection(url, user, password);;
OracleCallableStatement callStmt = null;
try {
callStmt = (OracleCallableStatement)conn.prepareCall("{call add_projects(?)}");
Blob blob1 = new SerialBlob(("test1").getBytes());
Blob blob2 = new SerialBlob(("test2").getBytes());
// create array holding values for ProjectType object's properties
Object[] project1 = new Object[] {"1", "Title 1", blob1};
Object[] project2 = new Object[] {"2", "Title 2", null};
// each struct is one ProjectType object
Struct structProject1 = conn.createStruct("PROJECT_TYPE", project1);
Struct structProject2 = conn.createStruct("PROJECT_TYPE", project2);
Struct[] structArrayOfProjects = {structProject1, structProject2};
// array holding two ProjectType objects
Array arrayOfProjects = ((OracleConnection) conn).createOracleArray("MY_ARRAY", structArrayOfProjects);
callStmt.setArray(1, arrayOfProjects);
callStmt.execute();
//conn.commit();
System.out.println("Committed.");
} catch (Exception e) {
if (conn != null) try { conn.rollback(); } catch (Exception ex) { System.out.println("Rollback failed."); }
throw e;
} finally {
callStmt.close();
conn.close();
}
}
-- Error Code --
//Error occur when I try to insert blobs data to DB. //
Object[] project1 = new Object[] {"1", "Title 1", blob1};
Object[] project2 = new Object[] {"2", "Title 2", blob2};
java.sql.SQLException: Fail to convert to internal
representation:error at oracle.jdbc.oracore.OracleTypeBLOB.toDatum(OracleTypeBLOB.java:69) at oracle.jdbc.oracore.OracleType.toDatumInternal(OracleType.java:142) at oracle.sql.StructDescriptor.toOracleArray(StructDescriptor.java:741) at oracle.sql.StructDescriptor.toArray(StructDescriptor.java:1322) at oracle.sql.STRUCT.(STRUCT.java:136) at oracle.jdbc.driver.PhysicalConnection.createStruct(PhysicalConnection.java:8733)
-- No error when I try to use NULL data --
Object[] project1 = new Object[] {"1", "Title 1", null};
Object[] project2 = new Object[] {"2", "Title 2", null};
Please let me know how to solve this problem.
Thanks.