where is the error in this RowMapper - java

I have this Request SQL :
private List<IWsResponse> getBPartnerDetails(String valueNetwork, String reportLevel2) {
JdbcTemplate tm = new JdbcTemplate(ds.getDataSource());
StringBuffer sql = new StringBuffer("SELECT * FROM XRV_BPARTNERDETAILS order by BPartner_ID");
ArrayList<Object> params = new ArrayList<Object>();
response = tm.query(sql.toString(), new BPartnerMapper());
return response;
}
i create a new RowMapper (BPartnerMapper) like this
public class BPartnerMapper implements RowMapper<IWsResponse> {
#Override
public List<IWsResponse> mapRow(ResultSet rs, int rowNum) throws SQLException {
List<IWsResponse> bps = new ArrayList<IWsResponse>();
while (rs.next()) {
bp = new BPartner();
bp.setBPartnerValue(rs.getString("BPartnerValue"));
//adress
adr = new Adress();
adr.setBPartnerLocation_ID(BPartner_Location_ID);
bp.getAdress().add(adr);
//user
usr = new User();
usr.setUser_ID(User_ID);
bp.getUsers().add(usr);
bps.add(bp)
}
return bps;
}
the Class BPartner is
public class BPartner implements IWsResponse {
private String BPartnerValue;
private ArrayList<Adress> adress = new ArrayList<Adress>();
private ArrayList<User> users = new ArrayList<User>();
}
so i get this Error
The return type is incompatible with RowMapper<IWsResponse>.mapRow(ResultSet, int)

Take a look at the documentation. A row mapper maps objects for a single row.
This method should not call next() on the ResultSet; it is only supposed to map values of the current row.
So your return type should be IWsResponse rather than List. Additionally you must only map the current row. Not all of them.

Related

How to return a List in Apache Axis

On tomcat, I have a class that return a list of objects from the database, something like this:
List<Entity> list = null;
Connection con = null;
PreparedStatement pstm = null;
ResultSet rs = null;
try {
con = Connector.getConexion();
String sql = "SELECT * FROM some_table LIMIT 10;";
pstm = con.prepareStatement(sql);
rs = pstm.executeQuery();
list = new ArrayList<>();
EntityCrimesChicago ecc = null;
while (rs.next()) {
e = new Entity();
e.setID(rs.getString(1));
e.setCase_Number(rs.getString(2));
list.add(e);
}
return list;
catch { ... }
finally { ... }
The entity and table have all fields as String/varchar, even the ID.
The service gets the list with (this is a web method):
public List<Entity> someThing(String q) {
DAOThings controller = new DAOThings();
List<Entity> things = controller.getSomeThings(Integer.parseInt(q));
return things;
}
This return
java.io.IOException: No serializer found for class Entity in registry org.apache.axis.encoding.TypeMappingDelegate#26a0e5e4
But, If I change the web method to return a String and I use return things.toString(); that return the objects, but doesn't the his content.
What can I do to return the list correctly?
I try returning a manueally added string list and works fine.
I solve my problem passing the object to a String array.
When I get the object list:
public List<Entity> someThing(String q) {
DAOThings controller = new DAOThings();
List<Entity> things = controller.getSomeThings(Integer.parseInt(q));
return things;
}
I only need to do this:
public List<String[]> someThing(String q) {
DAOThings controller = new DAOThings();
List<Entity> things = controller.getSomeThings(Integer.parseInt(q));
List<String[]> list = new ArrayList<>;
for (Entity value : things) {
String[] array = new String[obj_fields];
array[0] = value.getAny();
...
array[n] = value.getAny();
list.add(array);
}
return list;
}
And Apache Axis does the magic.

How to read Blob data into String Object using Spring JDBCTemplate

I am trying to use Spring JDBCTemplate to read blob data from a table.
List<Map<String, Object>> rows = jdbcTemplate.queryForList(sql);
for(Map<String, Object> row:rows){
row.get("OPERATION_NAME");
row.get("REQUEST_MESSAGE"); // this is blob
}
How can I read blob into a Java String object?
This seemed to work fine -
LobHandler lobHandler = new DefaultLobHandler();
List<FrontendData> frontEndDataList = jdbcTemplate.query(getResponseQuery(sessionId), new RowMapper() {
#Override
public Object mapRow(ResultSet rs, int rowNum) throws SQLException {
// TODO Auto-generated method stub
FrontendData frontEndData = new FrontendData();
String operationName = rs.getString("OPERATION_NAME");
frontEndData.setApiName(operationName);
byte[] requestData = lobHandler.getBlobAsBytes(rs,"RESPONSE_MESSAGE");
frontEndData.setResponse(new String(requestData));
return frontEndData;
}});
You can try retrieving the blob from database as below.
String retrieveBlobAsString = null;
Blob b = row.get("REQUEST_MESSAGE");//cast with (Blob) if required. Blob from resultSet as rs.getBlob(index).
InputStream bis = b.getBinaryStream();
ObjectInputStream ois = new ObjectInputStream(bis);
retrieveBlobAsString = (String) ois.readObject();
Another approach is by using java.sql.ResultSet getBytes() to convert BLOB column to String object,
List<ModelClass> hulaList = jdbcTemplate.query(sql,
new RowMapper<ModelClass>() {
#Override
public ModelClass mapRow(ResultSet rs, int rowNum) throws SQLException {
ModelClass model = new ModelClass();
model.setOperationName(rs.getString("OPERATION_NAME"));
byte[] byteArr = rs.getBytes("REQUEST_MESSAGE");
model.setRequestMessage(new String(byteArr));
return model;
}
});
Java 8+ syntax:
List<MyObject> results = jdbcTemplate.query(
"SELECT x,y FROM ... ",
(rs, n) -> {
MyObject o = new MyObject();
o.setX(rs.getString("x"));
o.setY(rs.getBytes("y")); // <-- a BLOB
return o;
}
);
Or if you query only a BLOB:
List<byte[]> b = jdbc.query("SELECT y FROM ...", (rs, n) -> rs.getBytes(1));

Why does the new element added into my ArrayList overwrite all previous values?

I retrieve values from a database, create a new Transaction Object and add it to an ArrayList<Transaction>, which I then return.
The problem is that everytime returnList.add(t); is called, instead of just adding the Transaction, it also replaces all old Transactions with the new one.
Where is the error that causes this behaviour?
public ArrayList<Transaction> getTransactions(long intervall, Map<String, String> transactionFields) {
connect();
ArrayList<Transaction> returnList = new ArrayList<Transaction>();
Statement sta;
ZonedDateTime now = ZonedDateTime.now(ZoneOffset.UTC);
now = now.minusSeconds(intervall);
try {
sta = conn.createStatement();
String Sql = "...";
ResultSet rs = sta.executeQuery(Sql);
while (rs.next()) {
Transaction t = new Transaction(rs.getString("No"), transactionFields);
t.set("AgentName", rs.getString("cname"));
returnList.add(t);
}
} catch (SQLException e) {
...
}
disconnect();
return returnList;
}
Here is the Transaction class:
public class Transaction {
private Map<String, String> fields;
public Transaction(String number, Map<String, String> transactionFields) {
fields = transactionFields;
fields.put("Number", number);
}
public void set(String field, String value) {
fields.put(field, value);
}
public String get(String field) {
return fields.get(field);
}
public Map<String, String> getFieldMap() {
return fields;
}
#Override
public String toString() {
return fields.toString();
You are using the same Map in all your Transaction instances.
Instead, pass in a new one each time:
Transaction t = new Transaction(rs.getString("No"), new HashMap<String, String>());
or just create the Map inside your Transaction class.

Calling Oracle procedure that returns rows using SimpleJdbcCall in Spring

I have written the following code
MapSqlParameterSource in = new MapSqlParameterSource();
in.addValue("V_OPP_ID", bean.getOpportunityId());
in.addValue("V_NAME",bean.getName());
in.addValue("V_FROM_DATE", bean.getStdate());
in.addValue("V_TO_DATE", bean.getEddate());
in.addValue("V_USERTYPE", bean.getUserType());
jdbcCall.execute(in);
Here the jdbcCall.execute(in) returns me resultset/table corresponding to Arraylist. How do i extract this ArrayList
Is using jdbcCall a correct Approach ? If not what is Adviced ?
This is the code I use for a call to a function:
RowMapper<String> rm = new ParameterizedRowMapper<String>() {
#Override
public String mapRow(ResultSet rs, int rowNum) throws SQLException {
return rs.getString(1);
}
};
myStoredProcedure = new SimpleJdbcCall(DataSourceConnection.getDataSource())
.withCatalogName("PACKAGE")
.withFunctionName("GET_ALIAS")
.returningResultSet("return", rm);
MapSqlParameterSource params = new MapSqlParameterSource();
params.addValue("P_ID",userStr);
params.addValue("P_DOMAIN_ALIAS", domain[0]);
List<String> list = myStoredProcedure.executeFunction(List.class,params);
and if you are not able to use the metadata then this is the code:
RowMapper<String> rm = new ParameterizedRowMapper<String>() {
#Override
public String mapRow(ResultSet rs, int rowNum) throws SQLException {
return rs.getString(1);
}
};
SqlParameter emailParam = new SqlParameter("P_ID", OracleTypes.VARCHAR);
SqlParameter domainParam = new SqlParameter("P_DOMAIN_ALIAS", OracleTypes.VARCHAR);
SqlOutParameter resultParam = new SqlOutParameter("return", OracleTypes.CURSOR);
myStoredProcedure = new SimpleJdbcCall(DataSourceConnection.getDataSource())
.withCatalogName("PACKAGE")
.withFunctionName("GET_ALIAS")
.withoutProcedureColumnMetaDataAccess()
.returningResultSet("return", rm)
.declareParameters(resultParam, emailParam, domainParam);
MapSqlParameterSource params = new MapSqlParameterSource();
params.addValue("P_ID",userStr);
params.addValue("P_DOMAIN_ALIAS", domain[0]);
List<String> list = myStoredProcedure.executeFunction(List.class,params);

How to intercept and change sql query dynamically in mybatis

I use mybatis to perform sql queries in my project. I need to intercept sql query before executing to apply some changed dynamically. I've read about #Interseptors like this:
#Intercepts({#Signature(type= Executor.class, method = "query", args = {...})})
public class ExamplePlugin implements Interceptor {
public Object intercept(Invocation invocation) throws Throwable {
return invocation.proceed();
}
public Object plugin(Object target) {
return Plugin.wrap(target, this);
}
public void setProperties(Properties properties) {
}
}
And it really intercepts executions, but there is no way to change sql query since appropriate field is not writable. Should I build new instance of whole object manually to just replace sql query? Where is the right place to intercept query execution to change it dynamically? Thank.
I hope it will help you:
#Intercepts( { #Signature(type = Executor.class, method = "query", args = {
MappedStatement.class, Object.class, RowBounds.class,
ResultHandler.class
})
})
public class SelectCountSqlInterceptor2 implements Interceptor
{
public static String COUNT = "_count";
private static int MAPPED_STATEMENT_INDEX = 0;
private static int PARAMETER_INDEX = 1;
#Override
public Object intercept(Invocation invocation) throws Throwable
{
processCountSql(invocation.getArgs());
return invocation.proceed();
}
#SuppressWarnings("rawtypes")
private void processCountSql(final Object[] queryArgs)
{
if (queryArgs[PARAMETER_INDEX] instanceof Map)
{
Map parameter = (Map) queryArgs[PARAMETER_INDEX];
if (parameter.containsKey(COUNT))
{
MappedStatement ms = (MappedStatement) queryArgs[MAPPED_STATEMENT_INDEX];
BoundSql boundSql = ms.getBoundSql(parameter);
String sql = ms.getBoundSql(parameter).getSql().trim();
BoundSql newBoundSql = new BoundSql(ms.getConfiguration(),
getCountSQL(sql), boundSql.getParameterMappings(),
boundSql.getParameterObject());
MappedStatement newMs = copyFromMappedStatement(ms,
new OffsetLimitInterceptor.BoundSqlSqlSource(newBoundSql));
queryArgs[MAPPED_STATEMENT_INDEX] = newMs;
}
}
}
// see: MapperBuilderAssistant
#SuppressWarnings({ "unchecked", "rawtypes" })
private MappedStatement copyFromMappedStatement(MappedStatement ms,
SqlSource newSqlSource)
{
Builder builder = new MappedStatement.Builder(ms.getConfiguration(), ms
.getId(), newSqlSource, ms.getSqlCommandType());
builder.resource(ms.getResource());
builder.fetchSize(ms.getFetchSize());
builder.statementType(ms.getStatementType());
builder.keyGenerator(ms.getKeyGenerator());
// setStatementTimeout()
builder.timeout(ms.getTimeout());
// setParameterMap()
builder.parameterMap(ms.getParameterMap());
// setStatementResultMap()
List<ResultMap> resultMaps = new ArrayList<ResultMap>();
String id = "-inline";
if (ms.getResultMaps() != null)
{
id = ms.getResultMaps().get(0).getId() + "-inline";
}
ResultMap resultMap = new ResultMap.Builder(null, id, Long.class,
new ArrayList()).build();
resultMaps.add(resultMap);
builder.resultMaps(resultMaps);
builder.resultSetType(ms.getResultSetType());
// setStatementCache()
builder.cache(ms.getCache());
builder.flushCacheRequired(ms.isFlushCacheRequired());
builder.useCache(ms.isUseCache());
return builder.build();
}
private String getCountSQL(String sql)
{
String lowerCaseSQL = sql.toLowerCase().replace("\n", " ").replace("\t", " ");
int index = lowerCaseSQL.indexOf(" order ");
if (index != -1)
{
sql = sql.substring(0, index);
}
return "SELECT COUNT(*) from ( select 1 as col_c " + sql.substring(lowerCaseSQL.indexOf(" from ")) + " ) cnt";
}
#Override
public Object plugin(Object target)
{
return Plugin.wrap(target, this);
}
#Override
public void setProperties(Properties properties)
{
}
}
You may consider using a string template library (eg Velocity, Handlebars, Mustache) to help you
As of to date, there is even MyBatis-Velocity (http://mybatis.github.io/velocity-scripting/) to help you to do scripting for the sql.
Depending on the changes you want to make, you may want to use the dynamic sql feature of mybatis 3

Categories