This is my first time posting a question in stackoverflow, I need to write a java stored procedure that creates an excel file and returns a blob containing the file data in bytes.
My pl/sql function is in the following form
function test_create_excel(i_username IN varchar2) return BLOB
AS LANGUAGE JAVA NAME 'NTO.Excel.ExcelFunctions.PushToExcel( java.lang.String ) return java.sql.Blob';
my Java method is as follows
public static java.sql.Blob TestPushToExcel(String username) throws IOException, SQLException{
//create excel file, read content to byte array and set to a blob
}
My problem is that I cannot find any way to create an instance of java.sql.Blob so that i can use the blob.setBinaryStream(..) method to write the file data byte array.
I tried to use the SerialBlob implementation but it results in the following oracle error
ORA-00932: inconsistent datatypes: expected a return value that is an instance of a user defined Java class convertible to an Oracle type got an object that could not be converted
has anyone come across this issue and if so can you share on how you got through it.
Thank you in Advance.
EDIT JAVA
public static oracle.sql.BLOB getBlob(byte[] data) throws SQLException, IOException{
oracle.jdbc.OracleConnection conn = (oracle.jdbc.OracleConnection)new OracleDriver().defaultConnection();
oracle.sql.BLOB retBlob = oracle.sql.BLOB.createTemporary(conn, true, oracle.sql.BLOB.DURATION_SESSION);
java.io.OutputStream outStr = retBlob.setBinaryStream(0);
outStr.write(data);
outStr.flush();
return retBlob;
}
public static ExcelFileStore PushToExcel(String userId) throws IOException, SQLException{
ExcelFileStore fileStore = new ExcelFileStore();
fileStore.NU_USERID = userId;
fileStore.CreatedTime = new java.sql.Date(new Date().getTime());
fileStore.Last_Updated = new java.sql.Date(new Date().getTime());
fileStore.FileSize = fileData.length;
fileStore.FileData = getBlob(fileData);
return fileStore;
}
PL/SQL
function test_create_excel(i_username IN varchar2) return EXCELFILESTORE AS LANGUAGE JAVA NAME 'NTO.Excel.ExcelFunctions.PushToExcel( java.lang.String, ) return OracleObjects.ExcelFileStore';
OracleObject.ExcelfileStore is a class that implements java.sql.SqlData and EXCELFILESTORE is a UDT in oracle.
I loaded the reference jars and jar created for my code using 'sys.dbms_java.loadjava'
I hope you understand my question, as i'm quite new to pl/sql programming