Reading a BLOB using JDBC Spring without a result set - oracle

I have an Oracle stored procedure that returns a BLOB in an output parameter:
PROCEDURE GET_IMAGE_DATA(i_image_type IN NUMBER, o_image_data OUT BLOB) IS
BEGIN
SELECT IMAGE_DATA
INTO o_image_data
FROM IMAGES
WHERE IMAGE_TYPE = i_image_type;
END GET_IMAGE_DATA;
I want to use JDBC Spring to read this data. However, DefaultLobHandler (and I think OracleLobHandler) getBlobAsBytes() requires a resultset.
private static class QueryForBinaryCryptKey extends StoredProcedure {
private static final String SQL = "IMAGE_PKG.GET_IMAGE_DATA";
private DefaultLobHandler lobHandler;
QueryForImageData(DataSource dataSource) {
super(dataSource, SQL);
setFunction(false);
lobHandler = new DefaultLobHandler();
declareParameter(new SqlParameter(KEY_TYPE, OracleTypes.NUMBER));
declareParameter(new SqlOutParameter(KEY_BLOB, OracleTypes.BLOB));
}
public Map getImage(int keyType) {
Map outParams = super.execute(inParams(keyType));
//how can I get the contents of the blob right here since
//getBlobAsBytes requires a resultSet???
return outParams;
}
private Map inParams(int keyType) {
Map params = new HashMap();
params.put(KEY_TYPE, new Integer(keyType));
return params;
}
}
How can I get the blob data when all I have is an out parameter and not a resultset?

The JDBC Spring API (DefaultLobHandler and OracleLobHandler) require a ResultSet object for their BLOB related methods.
You need to convert your GET_IMAGE_DATA procedure into a function:
FUNCTION ATTACHMENT_BLOB_GET(IN_IMAGE_TYPE IN IMAGES.IMAGE_TYPE%TYPE)
RETURN SYS_REFCURSOR AS
results_cursor SYS_REFCURSOR;
BEGIN
OPEN results_cursor FOR
SELECT t.image_data
FROM IMAGES t
WHERE t.image_type = IN_IMAGE_TYPE;
RETURN results_cursor;
END;
OUT parameters are always good for some grief, BLOBs especially.

It is indeed possible to read Blob as Stream/byte[] without a ResultSet. Check this one out.

Related

In Spring Boot, how to I get the column names from a StoredProcedureQuery result?

I am working on creating a simple utility that allows our users to execute a pre-selected list of stored procedures that return a simple list result set as a JSON string. The result set varies based on the selected procedure. I am able to get the results easily enough (and pass back as JSON as required), but the results don't include the column names.
The most common answer I found online is to use ResultSetMetaData or NativeQuery, but I couldn't figure out how to extract the metadata or transform the query properly using a StoredProcedureQuery object. How do I get the column names from a StoredProcedureQuery result?
Here is my code:
#SuppressWarnings("unchecked")
public String executeProcedure(String procedure, String jsonData) {
//Set up a call to the stored procedure
StoredProcedureQuery query = entityManager.createStoredProcedureQuery(procedure);
//Register and set the parameters
query.registerStoredProcedureParameter(0, String.class, ParameterMode.IN);
query.setParameter(0, jsonData);
String jsonResults = "[{}]";
try {
//Execute the query and store the results
query.execute();
List list = query.getResultList();
jsonResults = new Gson().toJson(list);
} finally {
try {
//Cleanup
query.unwrap(ProcedureOutputs.class).release();
} catch(Exception e) {
e.printStackTrace();
}
}
return jsonResults;
}
The challenge is to get a ResultSet. In order to list the column names you need a ResultSet to do the following to access metadata. (Column names are metadata)
ResultSetMetaData resultSetMetaData = resultSet.getMetaData();
System.out.println("Column name: "+resultSetMetaData.getColumnName(1));
System.out.println("Column type: "+resultSetMetaData.getColumnTypeName(1));
You can't get ResultSet (or metadata) from javax.persistence.StoredProcedureQuery or from spring-jpa Support JPA 2.1 stored procedures returning result sets
You can with low-level JDBC as follows:
CallableStatement stmnt = conn.prepareCall("{call demoSp(?, ?)}");
stmnt.setString(1, "abcdefg");
ResultSet resultSet1 = stmnt.executeQuery();
resultSet1.getMetaData(); // etc

passing an object having array to oracle stored procedure

I've a class "User" as follows:
public class User{
private String firstname;
private String lastname;
private String[] departments;
}
I want to pass an object of User type to my oracle stored procedure that takes an object type as parameter. So I did this to achieve my solution:-
1. Created USER_SEARCH_OBJ as a type in database as follows:-
create or replace
TYPE USER_SEARCH_OBJ AS OBJECT (
FIRST_NAME VARCHAR2(256),
LAST_NAME VARCHAR2(256),
DEPARTMENTS TABLE_OF_VALUES,
);
2. Created TABLE_OF_VALUES as a type in database as follows:-
create or replace TYPE TABLE_OF_VALUES AS TABLE OF VARCHAR2(20);
3. Passed the User object from Java class as follows:-
Object[] departments = {"1","2"};
StructDescriptor objDescriptor = StructDescriptor.createDescriptor("USER_SEARCH_OBJ", conn.getMetaData().getConnection());
ArrayDescriptor arrayDescriptor = ArrayDescriptor.createDescriptor("TABLE_OF_VALUES", conn.getMetaData().getConnection());
ARRAY departmentArr = new ARRAY(arrayDescriptor, conn.getMetaData().getConnection(), departments );
Object[] userProperties = new Object[2];
userProperties [0] = "paras";//first_name
userProperties [1] = "anand";//last_name
userProperties [2] = departmentArr ;//department array
STRUCT searchObj = new STRUCT(objDescriptor, conn.getMetaData().getConnection(), userProperties );
CallableStatement cStmt = conn.prepareCall("PCK_SEARCH2.USER_SEARCH(?,?)");
cStmt.setObject(1, searchObj);
cStmt.registerOutParameter(2, OracleTypes.CURSOR);
cStmt.execute();
But when I run this code I get an exception as follows:-
java.sql.SQLException: Inconsistent java and sql object types
at oracle.sql.StructDescriptor.toOracleArray(StructDescriptor.java:709)
at oracle.sql.StructDescriptor.toArray(StructDescriptor.java:1296)
at oracle.sql.STRUCT.<init>(STRUCT.java:165)
at com.ensenda.lmp.web.controller.User.main(User.java:75)
This exception comes at the following line of code:-
STRUCT searchObj = new STRUCT(objDescriptor, conn.getMetaData().getConnection(), userProperties );
Please let me know where I'm going wrong.
Firstly I think you should get an ArrayOutOfBoundException because you have declared an Object[2] and trying to put a 3rd element into it. Considering that is a typo.
For the original problem instead of using setObject you can use
*JDBCUtil.setStruct(conn,cStmt,1,"USER_SEARCH_OBJ",userProperties);*

Call Oracle Stored procedure from JDBC with complex Input and Output type

I'm so close in solving this question but I'm apparently missing something. My requirement is to call a stored procedure in Oracle from JDBC. The stored procedure takes 1 user-defined Oracle object as INput and another user-defined Oracle object as OUTput. The INput and OUTput objects have mix of both primitive Oracle data types and collection of another set of user-defined objects. I'm successfully able to call the stored procedure and get results back as long as I set NULL for the collection types in the INput and OUTput objects. If I try to create ArrayDescriptor for the list of Oracle objects to send it to the stored procedure I keep hitting roadblocks. So I need help with figuring out how to set the Array to the INput object and set that to CallableStatement. Please note, I am aware of how I can send the primitive type and array as direct inputs to the stored procedure. But I do not want to go that way as we later have to send 10 additional fields to the procedure, I do not want to add them to method signature. Here's the list of classes. Also, there is no compilation errors for the code below.
Package in oracle:
CREATE OR REPLACE PACKAGE testPkg AS
PROCEDURE spGetTestData (
TESTDATA_IN IN TESTDATA_IN_OBJ,
TESTDATA_OUT OUT TESTDATA_OUT_OBJ
);
END;
INput object for the stored procedure:
CREATE OR REPLACE TYPE TESTDATA_IN_OBJ AS OBJECT(
testStr1 VARCHAR2(5),
arrObj1 ARR_OBJ_1_NT);
Array Object as part of INput Object:
create or replace TYPE ARR_OBJ_1_NT AS TABLE OF ARR_OBJ_1_OBJ;
UserDefined Object part of INput Object:
CREATE OR REPLACE TYPE ARR_OBJ_1_OBJ AS OBJECT
(
teststr VARCHAR2(14),
testNumber NUMBER(4),
);
TestDataINObj.java:
import java.sql.Array;
import java.sql.SQLData;
import java.sql.SQLException;
import java.sql.SQLInput;
import java.sql.SQLOutput;
public class TestDataINObj implements SQLData
{
private String sql_type = "TESTDATA_IN_OBJ";
protected String testStr1;
protected Array arrObj1;
#Override
public String getSQLTypeName() throws SQLException
{
return this.sql_type;
}
// getter and setter for fields
#Override
public void readSQL(SQLInput stream, String typeName) throws SQLException
{
this.sql_type=typeName;
this.testStr1 = stream.readString();
this.arrObj1 = stream.readArray();
}
#Override
public void writeSQL(SQLOutput stream) throws SQLException
{
stream.writeString(this.testStr1);
stream.writeArray(this.arrObj1);
}
}
TestDataINObjConverter.java:
public class TestDataINObjConverter
{
public static TestDataINObj convertPOJOToDBInObj(Connection connection)
throws SQLException
{
TestDataINObj testDataINObj = new TestDataINObj();
testDataINObj.setTestStr1("some string");
ArrObj1NT[] ArrObj1NTList = ArrObj1NTConverter.convertPOJOToDBObj(); // this will return Java array of ArrObj1NT class
testDataINObj.setArrObj1(getOracleArray("ARR_OBJ_1_NT",connection, ArrObj1NTList));
return testDataINObj;
}
private static Array getOracleArray(final String typeName, Connection connection, ArrObj1NT[] ArrObj1NTList) throws SQLException
{
if (typeName == null)
{
return null;
}
Array oracleArray = new ARRAY(new ArrayDescriptor(typeName, connection), connection, ArrObj1NTList);
return oracleArray;
}
Code that actually executes call to stored procedure:
... //code to get connection
..// connection is of type T4CConnection
Map typeMap = connection.getTypeMap();
typeMap.put("TESTDATA_IN_OBJ", TestDataINObj.class);
typeMap.put("TESTDATA_OUT_OBJ", TestDataOUTObj.class);
typeMap.put("ARR_OBJ_1_NT", ArrObj1NT.class);
TestDataINObj testDataINObj = TestDataINObjConverter.convertPOJOToDBInObj(connection);
getMetaDataCallableStatement = connection.prepareCall("begin " + "testPkg" + ".spGetTestData (?,?);"+ " end;");
getMetaDataCallableStatement.setObject(1, testDataINObj);
getMetaDataCallableStatement.registerOutParameter(2, Types.STRUCT, "TESTDATA_OUT_OBJ");
rs = getMetaDataCallableStatement.executeQuery();
TestDataOUTObj testDataOUTObj = (TestDataOUTObj) getMetaDataCallableStatement.getObject(2, typeMap);
Miscellaneous:
1. The objects are declared in Schema level and is available for the db user to access it.
2. I've not included all of the corresponding Java objects here as it will take more space. They implement SQLData interface and their type names match with DB names. The read and writeSQL methods uses getString, getArray and corresponding setter methods.
This is a very old approach, why are you not using "Oradata" and "Oradatum" interface?
It will save lot of effort.
Your approach leaves a lot of scopr for error, you will have to read the stream in proper manner and check for ordering of fields yourself which can be tricky. Oradata approach will do that for you.
Coming to your approach, Your code is not very clear.
But just to give an overview, StructDescriptor will map to oracle record type and ArrayDescriptor will map to oracle table type, from your code i am confused about whta you are trying to achieve.
I can help if you can make it more clear.

Resultset Metadata from Spring JDBCTemplate Query methods

Is there any way I can get resultset object from one of jdbctemplate query methods?
I have a code like
List<ResultSet> rsList = template.query(finalQuery, new RowMapper<ResultSet>() {
public ResultSet mapRow(ResultSet rs, int rowNum) throws SQLException {
return rs;
}
}
);
I wanted to execute my sql statement stored in finalQuery String and get the resultset. The query is a complex join on 6 to 7 tables and I am select 4-5 columns from each table and wanted to get the metadata of those columns to transform data types and data to downstream systems.
If it is a simple query and I am fetching form only one table I can use RowMapper#mapRow and inside that maprow method i can call ResultsetExtractor.extractData to get list of results; but in this case I have complex joins in my query and I am trying to get resultset Object and from that resultset metadata...
The above code is not good because for each result it will return same resultset object and I dont want to store them in list ...
Once more thing is if maprow is called for each result from my query will JDBCTemplate close the rs and connection even though my list has reference to RS object?
Is there any simple method like jdbcTemplate.queryForResultSet(sql) ?
Now I have implemented my own ResultSet Extractor to process and insert data into downstream systems
sourceJdbcTemplate.query(finalQuery, new CustomResultSetProcessor(targetTable, targetJdbcTemplate));
This CustomResultSetProcessor implements ResultSetExtractor and in extractData method I am calling 3 different methods 1 is get ColumnTypes form rs.getMetaData() and second is getColumnTypes of target metadata by running
SELECT NAME, COLTYPE, TBNAME FROM SYSIBM.SYSCOLUMNS WHERE TBNAME ='TABLENAME' AND TABCREATOR='TABLE CREATOR'
and in 3rd method I am building the insert statement (prepared) form target columntypes and finally calling that using
new BatchPreparedStatementSetter()
{
#Override
public void setValues(PreparedStatement insertStmt, int i) throws SQLException{} }
Hope this helps to others...
Note that the whole point of Spring JDBC Template is that it automatically closes all resources, including ResultSet, after execution of callback method. Therefore it would be better to extract necessary data inside a callback method and allow Spring to close the ResultSet after it.
If result of data extraction is not a List, you can use ResultSetExtractor instead of RowMapper:
SomeComplexResult r = template.query(finalQuery,
new ResultSetExtractor<SomeComplexResult>() {
public SomeResult extractData(ResultSet) {
// do complex processing of ResultSet and return its result as SomeComplexResult
}
});
Something like this would also work:
Connection con = DataSourceUtils.getConnection(dataSource); // your datasource
Statement s = con.createStatement();
ResultSet rs = s.executeQuery(query); // your query
ResultSetMetaData rsmd = rs.getMetaData();
Although I agree with #axtavt that ResultSetExtractor is preferred in Spring environment, it does force you to execute the query.
The code below does not require you to do so, so that the client code is not required to provide the actual arguments for the query parameters:
public SomeResult getMetadata(String querySql) throws SQLException {
Assert.hasText(querySql);
DataSource ds = jdbcTemplate.getDataSource();
Connection con = null;
PreparedStatement ps = null;
try {
con = DataSourceUtils.getConnection(ds);
ps = con.prepareStatement(querySql);
ResultSetMetaData md = ps.getMetaData(); //<-- the query is compiled, but not executed
return processMetadata(md);
} finally {
JdbcUtils.closeStatement(ps);
DataSourceUtils.releaseConnection(con, ds);
}
}

blazeds converts BigDecimal to string

I have a Flex application that uses blazeds to connect to a Java backend. Using remoting, I give a call to an API to run a SELECT statement on a table (using conventional JDBC classes) in a Oracle database.
The table has 2 columns:
PRODUCT_CODE of type NVARCHAR2(32) and
DEMAND of type NUMBER(10, 0)
My Java API is as follows:
public List<?> getQueryResult(String query) {
Connection conn = DriverManager.getConnection(connStr, userName, password);
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery(query);
ArrayList<?> result = new ArrayList<?>();
while(rs.next()) {
Object[] itemArray = new Object[2];
itemArray[0] = rs.getObject(1);
itemArray[1] = rs.getObject(2);
result.add(itemArray);
}
return result;
}
In my Flex side, I have a handler for result event of this remote operation:
private function onResult(e:ResultEvent) : void {
var result:ArrayCollection = (e.result as ArrayCollection);
}
Strangely, the values corresponding to DEMAND column are automatically converted to string (I debugged to find out that in backend, these were BigDecimal)
Any suggestions?
Yes indeed, the BigDecimal from Java is converted to String in ActionScript, as you can read in the developer guide. There is no BigDecimal in ActionScript so it was the only option - you cannot convert any BigDecimal to int or float.
If you are sure that your value represented as NUMBER(10,0) has values in the interval -2,147,483,648 - 2,147,483,647 you can convert it to an int in java - see the code below:
itemArray[1] = ((BigDecimal)rs.getObject(2)).intValue();

Resources