[英]java array blob data insert to oracle db using by stored procudure(Fail to convert to internal representation:error)
I want to array BLOB data save to Oracle DB using by stored procedure. 我想通过存储过程将BLOB数据存储到Oracle DB。 (reference from this : Pass array from Java to Oracle: java.sql.SQLException: Fail to convert to internal representation:error ) (引用自: 从Java到Oracle传递数组:java.sql.SQLException:无法转换为内部表示:错误 )
But I have added BLOB column to struct. 但是我已经在struct中添加了BLOB列。 Here is my db script and java code. 这是我的db脚本和java代码。
--DB code-- CREATE TABLE project_types ( proj_id VARCHAR2(10), proj_title VARCHAR2(10), proj_data BLOB ); --DB代码 - 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. 我已将BLOB数据列添加到project_types表和project_type TYPE,SturedProcedure。
--Table--
proj_data BLOB
-- TYPE--
proj_data BLOB
--StruedProcedure--
p_projects_array(v_i).proj_data
-- Java code -- - Java代码 -
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. //当我尝试将blob数据插入DB时发生错误。 // //
Object[] project1 = new Object[] {"1", "Title 1", blob1}; Object [] project1 = new Object [] {“1”,“Title 1”,blob1};
Object[] project2 = new Object[] {"2", "Title 2", blob2}; Object [] project2 = new Object [] {“2”,“Title 2”,blob2};
java.sql.SQLException: Fail to convert to internal java.sql.SQLException:无法转换为内部
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) 表示:oracle.jdbc.oracore.OracleTypeBLOB.toDatum(OracleTypeBLOB.java:69)的错误,位于oracle.sql.StructDescriptor.toOracleArray(StructDescriptor.java)的oracle.jdbc.oracore.OracleType.toDatumInternal(OracleType.java:142) 741)位于oracle.jdbc.driver.PhysicalConnection.createStruct(PhysicalConnection.java:8733)的oracle.sql.STRUCT。(STRUCT.java:136)的oracle.sql.StructDescriptor.toArray(StructDescriptor.java:1322)
-- No error when I try to use NULL data -- - 当我尝试使用NULL数据时没有错误 -
Object[] project1 = new Object[] {"1", "Title 1", null}; Object [] project1 = new Object [] {“1”,“Title 1”,null};
Object[] project2 = new Object[] {"2", "Title 2", null}; Object [] project2 = new Object [] {“2”,“Title 2”,null};
Please let me know how to solve this problem. 请让我知道如何解决这个问题。
Thanks. 谢谢。
Change 更改
Blob blob1 = new SerialBlob(("test1").getBytes());
to 至
BLOB blob1 = new BLOB((oracle.jdbc.OracleConnection)conn, ("test1").getBytes());
where BLOB
is import oracle.sql.BLOB;
其中BLOB
是import oracle.sql.BLOB;
And the rest of your code should be OK. 其余的代码应该没问题。
I thought I would post an answer because I was struggling with this for a while. 我以为我会发布一个答案,因为我一直在努力解决这个问题。 I had a method in place that worked that was similar to @Arkadiusz answer, but it is deprecated. 我有一个类似于@Arkadiusz答案的方法,但它已被弃用。
I am not sure why you are getting your that error, but maybe try this method. 我不确定你为什么会得到你的错误,但也许尝试这种方法。
import java.sql.Blob;
...
public class MyClassDAO {
public static int sendBlobToDb(MyObject myObject) {
CallableStatement cstmt = null;
OutputStream os = null;
ObjectOutputStream oop = null;
Blob blob = null;
Connection conn = null;
index = 0;
int result = 0;
String sql = "begin "
+ "insert into myTable "
+ "(ITEMID, SOME_TEXT, SOME_NUMBER, THE_BLOB_FIELD) values "
+ "(ITEMID_SEQ.nextval, ?, ?, empty_blob()) "
+ "return THE_BLOB_FIELD into ?; "
+ "end; ";
try {
conn = DriverManager.getConnection(url, user, password);
conn.setAutoCommit(false);
cstmt = conn.prepareCall(sql);
cstmt.setString(++index, myObject.getSomeText());
cstmt.setLong(++index, myObject.getSomeNumber());
cstmt.registerOutParameter(++index, java.sql.Types.BLOB);
cstmt.executeUpdate();
blob = (Blob) cstmt.getBlob(index);
os = blob.setBinaryStream(1L);
oop = new ObjectOutputStream(os);
oop.writeObject(myObject.getListData());
} catch(SQLException se) {
result = -1;
//log exception
try {
if(conn != null) {
conn.rollback();
}
} catch(SQLException se2) {
//log exception
}
} catch(Exception e) {
result = -1;
//log exception
} finally {
try {
if (conn != null) {
conn.close();
}
if (oop != null) {
oop.flush();
oop.close();
}
if (os != null) {
os.close();
}
if (cstmt != null) {
cstmt.close();
}
} catch(SQLException se) {
result = -1;
//log exception
} catch (Exception e) {
result = -1;
//log exception
}
}
return result;
}
}
I would send an object to a blob field in Oracle using something like this. 我会使用类似的东西将对象发送到Oracle中的blob字段。 I have not tested this yet so hopefully it works. 我还没有测试过,所以希望它有效。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.