I'm using Spring and Oracle database in my solution and i need to execute script
select count(1) from ELEMENTS, table(cast(? as arrayofnumbers)) session_ids
where root_session_id in session_ids.VALUE
but i have a problem with passing input parameter.
i try to pass List or array of BigInteger into
JdbcTemplate.queryForObject("select count(1) from ELEMENTS, table(cast(? as arrayofnumbers)) session_ids
where root_session_id in session_ids.VALUE", Integer.class, INPUT_PARAMS)
but has an Exception:
java.sql.SQLException: Invalid column type
at oracle.jdbc.driver.OraclePreparedStatement.setObjectCritical(OraclePreparedStatement.java:8861)
at oracle.jdbc.driver.OraclePreparedStatement.setObjectInternal(OraclePreparedStatement.java:8338)
at oracle.jdbc.driver.OraclePreparedStatement.setObjectInternal(OraclePreparedStatement.java:9116)
at oracle.jdbc.driver.OraclePreparedStatement.setObject(OraclePreparedStatement.java:9093)
at oracle.jdbc.driver.OraclePreparedStatementWrapper.setObject(OraclePreparedStatementWrapper.java:234)
at weblogic.jdbc.wrapper.PreparedStatement.setObject(PreparedStatement.java:357)
Does anyone have the same problem?
EDIT:
Forget to describe arrayofnumber. It's custom type:
TYPE arrayofnumbers as table of number(20)
Found the solution:
final BigInteger[] ids = new BigInteger[]{BigInteger.valueOf(9137797712513092132L)};
int count = jdbc.query("select count(1) from NC_DATAFLOW_ELEMENTS\n" +
" where root_session_id in (select /*+ cardinality(t 10) */ * from table(cast (? as arrayofnumbers)) t)"
, new PreparedStatementSetter() {
public void setValues(PreparedStatement preparedStatement) throws SQLException {
Connection conn = preparedStatement.getConnection();
OracleConnection oraConn = conn.unwrap(OracleConnection.class);
oracle.sql.ARRAY widgets = oraConn.createARRAY("ARRAYOFNUMBERS", ids);
preparedStatement.setArray(1, widgets);
}
}, new ResultSetExtractor<Integer>() {
public Integer extractData(ResultSet resultSet) throws SQLException, DataAccessException {
resultSet.next();
return resultSet.getInt(1);
}
});
out.println(count);
should note that type of array (ARRAYOFNUMBER) should be in upper case
Related
My problem is that I want to call PL/SQL function in #PostConstruct method in Spring Boot.
This function returns information about tables in a database as you can see above:
CREATE OR REPLACE FUNCTION dbINFO
return sys_refcursor AS
table_info sys_refcursor;
begin
open table_info
for select table_name from all_tables where owner = 'HOMEUSER';
return table_info;
end;
I tried to use #NamedStoreProcedureQuery annotation but I don't have entity to use it with.
So I decided to try doing it with JDBC and so far I've got something like this.
private EntityManager entityManager;
public HospitalApplication(EntityManager entityManager) {
this.entityManager = entityManager;
}
#PostConstruct
private void init() throws SQLException {
System.out.println("\n\n\n\n\n\n");
EntityManagerFactoryInfo info = (EntityManagerFactoryInfo) entityManager.getEntityManagerFactory();
Connection connection = info.getDataSource().getConnection();
CallableStatement stmt = connection.prepareCall("BEGIN dbINFO(); END;");
stmt.registerOutParameter(1, OracleTypes.CURSOR);
stmt.execute();
ResultSet rs = ((OracleCallableStatement)stmt).getCursor(1);
while (rs.next()){
System.out.println(rs.getString(1));
}
rs.close();
stmt.close();
System.out.println("\n\n\n\n\n\n\n\n\n\n\n\n\n\n");
}
Problem is, my program is throwing java.sqlException about column has incorrect index.
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'hospitalApplication': Invocation of init method failed; nested exception is java.sql.SQLException: Incorrect column index
***EDIT****
I tried something like that and I am receiving error:
PLS-00905: obiekt HOMEUSER.DBINFO is invalid
ORA-06550: line 1, column 7:
PL/SQL: Statement ignored
#PostConstruct
private void init() throws SQLException {
System.out.println("\n\n\n\n\n\n");
EntityManagerFactoryInfo info = (EntityManagerFactoryInfo) entityManager.getEntityManagerFactory();
Connection connection = Objects.requireNonNull(info.getDataSource()).getConnection();
CallableStatement stmt = connection.prepareCall("{ ? = call dbINFO()}");
stmt.registerOutParameter(1, OracleTypes.CURSOR);
stmt.execute();
ResultSet rs = (ResultSet) stmt.getObject(1);
while (rs.next()){
System.out.println(rs.getCursorName());
}
rs.close();
stmt.close();
System.out.println("\n\n\n\n\n\n\n\n\n\n\n\n\n\n");
}
I am using queryForList to get a list from DB ,
my code looks like,
List<RoleIdBean> role = jdbcTemplate.queryForList(query , new Object[] {userId},RoleIdBean.class);
query = select * from role where userid=?
role table has two coloumns and roleIdBean has two variables .
When I am running this code it is saying expected 1, actual 2
Could someone please check where i am going wrong and assist how to use this method.
As M. Deinum mentions, you have to provide implementation of RowMapper interface so that Spring knows which columns from your table to map to which properties of your object (RoleIdBean). For instance like this:
List<RoleIdBean> list = jdbcTemplate.query("SELECT * FROM role_id", new Object[]{ userId }, new RowMapper<RoleIdBean>() {
#Override
public RoleIdBean mapRow(ResultSet rs, int rowNum) throws SQLException {
RoleIdBean bean = new RoleIdBean();
// Set properties from the ResultSet, e.g:
// bean.setRole(rs.getString(1));
return bean;
}
});
I'm try to insert byte[] into blob field with stored procedure, and get an Exception :
Request processing failed; nested exception is org.springframework.jdbc.BadSqlGrammarException: PreparedStatementCallback; bad SQL grammar [SELECT ID FROM sp_NEWFILE(?,?,?)]; nested exception is org.firebirdsql.jdbc.field.TypeConversionException: Error converting to object.
Model:
public class fileBody {
private int ID;
private byte[] BODY;
private String FILENAME; //getters an setters}
Insert it to database
public class FileBodyDaoImpl implements FileBodyDao {
public int insertData(final FileBody fileBody) throws IOException {
JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
LobHandler lobHandler = new DefaultLobHandler();
final InputStream in = new ByteArrayInputStream(fileBody.getBODY());
final int fileSize = in.available();
Map<String, Object> out = jdbcTemplate.queryForMap("SELECT ID FROM sp_NEWFILE(?,?,?)",
new AbstractLobCreatingPreparedStatementCallback(lobHandler) {
protected void setValues(PreparedStatement ps, LobCreator lobCreator) throws SQLException,
DataAccessException {
ps.setString(1, fileBody.getFILENAME());
lobCreator.setBlobAsBinaryStream(ps, 2, in, fileSize);
ps.setNull(3, java.sql.Types.INTEGER);
}
});
int last_inserted = Integer.parseInt(String.valueOf(out.get("ID")));
return last_inserted;
}
And my stored procedure
create or alter procedure sp_NEWFILE (
FILENAME varchar(255),
BODY blob sub_type 0 segment size 80,
USEID integer)
returns (
ID integer)
as
begin
if (useid is not null) then ID=USEID;
else ID=GEN_ID(gen_filebody_id,1);
if ((FILENAME is NULL) or (FILENAME='')) then FILENAME='UNDEFINED';
INSERT INTO t_filebody(ID,BODY,FILENAME) VALUES(:ID,:BODY,:FILENAME);
suspend;
end^
and i get an Exception:
Request processing failed; nested exception is org.springframework.jdbc.BadSqlGrammarException:
PreparedStatementCallback;
bad SQL grammar [SELECT ID FROM sp_NEWFILE(?,?,?)]; nested exception is org.firebirdsql.jdbc.field.TypeConversionException: Error converting to object.
Versions:
jaybird-jdk17-2.2.5;
Source: firebird2.5 Version: 2.5.1.26351.ds4-2ubuntu0.1;
The problem is that queryForMap does not support a PreparedStatementCallback (contrary to for example execute), instead your anonymous object is considered a normal parameter for the query to execute, and Jaybird does not support this object type. And if Jaybird had supported it, you would have received an error for missing parameters 2 and 3.
Your code can be greatly simplified by passing the byte array:
Map<String, Object> out = jdbcTemplate.queryForMap("SELECT ID FROM sp_NEWFILE(?,?,?)",
fileBody.getFILENAME(), fileBody.getBODY(), null);
This works as Jaybird considers a BLOB SUB_TYPE 0 as a java.sql.Types.LONGVARBINARY and JDBC 4.2 appendix B declares that byte[] is the default type for that (although you can also use it as a java.sql.Types.BLOB).
As a side note, your stored procedure does not need to be selectable (removing SUSPEND makes it executable), and the procedure could also be replaced by using a TRIGGER to generate the primary key and retrieving the value either by using INSERT .. RETURNING .. or through the JDBC generated keys facility (which in turn is implemented in Jaybird through INSERT .. RETURNING ..).
For those seeking non-Jaybird solution to insert BLOB using spring jdbctemplate, the following syntax worked for me to use stored procedures which is different compared to insert via queries.
Insert via Queries
ByteArrayInputStream inputStream = new ByteArrayInputStream(file.getBytes());
ps.setBlob(1, inputStream);
Insert via Stored Procedure Call
Map<String, Object> inParams = new HashMap<>();
inParams.put("pi_some_id", id);
inParams.put("pi_file_blob", new SqlLobValue(file.getBytes()));
SqlParameterSource sqlParameterSource = new MapSqlParameterSource(inParams);
SqlParameter[] sqlParameters = {
new SqlParameter("pi_some_id", Types.VARCHAR),
new SqlParameter("pi_file_blob", Types.BLOB),
new SqlOutParameter("po_error_flag", Types.VARCHAR),
new SqlOutParameter("po_message", Types.VARCHAR)};
SimpleJdbcCall simpleJdbcCall = new SimpleJdbcCall(jdbcTemplate).withoutProcedureColumnMetaDataAccess().
withProcedureName(storedProcName).withCatalogName(packageName).
declareParameters(sqlParameters);
Map<String, Object> storedProcResult = simpleJdbcCall.execute(sqlParameterSource);
On the same data sometimes throws the exception java.sql.SQLException: ORA-01002: fetch out of sequence, but in most attempts all working fine.
Java app running on Glassfish 3.1.2.2. Can anybody explain me, where is the problem?
#Singleton
#LocalBean
#Startup
#ConcurrencyManagement(ConcurrencyManagementType.BEAN)
public class MarketCodesSingleton {
#Resource(mappedName="jdbc/sss")
private DataSource source;
private volatile static Map<Interval, String> marketCodes;
#PostConstruct
#Schedule(minute="*/10", hour="*")
public void fillMarketCodes() {
try(Connection conn = source.getConnection()) {
Map<Interval, String> marketCodesInt = new TreeMap<>();
DaoFactory.getMarketCodesDao().fillMarketCodes(marketCodesInt, conn);
marketCodes = Collections.unmodifiableMap(marketCodesInt);
Logger.getLogger(getClass().getName()).log(Level.FINE, "MarketCodes updated");
} catch (SQLException e) {
Logger.getLogger(getClass().getName()).log(Level.SEVERE, "fillMarketCodes exception",e);
throw new EJBException("fillMarketCodes exception",e);
}
}
public String getMarketCode(Long msisdn) {
Interval interval = new Interval(msisdn);
return marketCodes.get(interval);
}
}
DaoFactory.getMarketCodesDao().fillMarketCodes:
private static final String getMarketCodes_SQL = "CALL SERVICE_PKG.GET_MARKET_CODES(?)";
#Override
public void fillMarketCodes(Map<Interval, String> intervals, Connection conn) throws SQLException {
try (CallableStatement cs = conn.prepareCall(getMarketCodes_SQL)) {
//-10 is a OracleTypes.CURSOR
cs.registerOutParameter(1, -10);
cs.execute();
try (ResultSet rs = (ResultSet) cs.getObject(1)) {
//*******Exception throws on the rs.next() in this method*******
while (rs.next()) {
Interval interval = new Interval(rs.getLong("from_no"), rs.getLong("to_no"));
intervals.put(interval, rs.getString("market_code"));
}
}
}
}
Procedure:
procedure GET_MARKET_CODES(
c_cursor OUT SYS_REFCURSOR
) AS
BEGIN
OPEN c_cursor FOR
SELECT from_no, to_no, market_code
FROM market_codes;
END GET_MARKET_CODES;
Connection properties:
<jdbc-connection-pool
connection-creation-retry-interval-in-seconds="5"
datasource-classname="oracle.jdbc.xa.client.OracleXADataSource"
max-pool-size="200"
max-connection-usage-count="1000"
res-type="javax.sql.XADataSource"
steady-pool-size="0"
name="sss_pool"
connection-creation-retry-attempts="5">
<property name="URL" value="jdbc:oracle:thin:#(DESCRIPTION =(ADDRESS_LIST =(ADDRESS = (PROTOCOL = TCP)(HOST = xxx.xxx.xxx.xxx)(PORT = xx)))(CONNECT_DATA =(SERVER = DEDICATED)(SERVICE_NAME = xx)))"></property>
<property name="Password" value="***"></property>
<property name="User" value="***"></property>
</jdbc-connection-pool>
The code is incomplete, so I can only guess:
the cursor was closed and you tried to fetch again
you did select for update and committed and then tried to fetch the next row.
Is your connection set to auto commit? A fetch across a commit or rollback is likely to cause this exception.
I also noticed that your SQL isn't surrounded by begin/end as in the Oracle docs or {} as in this example and the Oracle Javadoc.
To make certain whether when hitting exception all the rows are processed
Modify the below code to include a counter i to get processed rows and find actual count of rows
int i=0;
try{
while (rs.next()) {
Interval interval = new Interval(rs.getLong("from_no"), rs.getLong("to_no"));
intervals.put(interval, rs.getString("market_code"));
i=i+1;
}
}
catch (Exception e)
{
Logger.getLogger(getClass().getName()).log(Level.FINE, "the total rows processed"+ i);
Statement stmt = null;
String query = "select count(1) count_rows
from market_codes";
stmt = con.createStatement();
ResultSet rs1 = stmt.executeQuery(query);
rs1.next();
String countRows = rs1.getString("count_rows");
Logger.getLogger(getClass().getName()).log(Level.FINE,"Actual count of rows"+ countRows);
}
It's very strange, but problem has been solved by removing annotation #ConcurrencyManagement(ConcurrencyManagementType.BEAN)
Can anyone explain it?
I use the Spring JDBCTemplate to call a stored procedure ,like this
Map receive10PrmtBill = (Map) getJdbcTemplate().execute(sql, new CallableStatementCallback() {
#Override
public Object doInCallableStatement(CallableStatement cs) throws SQLException,
DataAccessException {
cs.setString(1, Constants.ADD_PROC_TYPE);
Map<String,PrmtBillInfoDatagram> tempMap = new HashMap();
cs.execute();
but the execute method return false and didn't throw any exception ,so I don't know what's wrong with my program , how to catch the Exception? Any help?
This is the proc
create proc sp_xx ( #userid int)
as
begin
select personid, personname from person where personid = #userid
select teamid, teamname from team
end
Try setting "IGNORE_DONE_IN_PROC" property to "true". See at:
http://javabob64.wordpress.com/2011/04/12/sybase-and-the-jdbc-driver/