I am trying to find an easy way to deal with Stored Procedures / SQL returning multiple result sets. I have been using the SimpleJdbcOperations#queryForList() method however this will only return the first result set as a List<Map<String, Object>>. I need to be able to get multiple result sets, ideally as a Collection of List<Map<String, Object>> or something. The program I am writing is a middleware component so I don't know what the SQL will be, or the form of the result set.
I think I have to use the JdbcOperations class which gives me access to more methods, including execute(CallableStatementCreator csc, CallableStatementCallback<T> action) but now I am stuck.
CallableStatementCallback<T> callback = new CallableStatementCallback<T>() {
#Override
public T doInCallableStatement(CallableStatement cs) throws SQLException, DataAccessException
{
boolean results = cs.execute(request);
while(results)
{
ResultSet result = cs.getResultSet();
results = cs.getMoreResults();
}
return null;
}
};
I am not really sure how to use the method though, or what to do with the ResultSet to get my generic List<Map<String, Object>>s.
I managed to get a Set<ResultSet> using this code,
private Set<ResultSet> executeProcedure(final String sql)
{
return jdbc.execute(new CallableStatementCreator() {
#Override
public CallableStatement createCallableStatement(Connection con) throws SQLException
{
return con.prepareCall(sql);
}
}, new CallableStatementCallback<Set<ResultSet>>() {
#Override
public Set<ResultSet> doInCallableStatement(CallableStatement cs) throws SQLException
{
Set<ResultSet> results = new HashSet<>();
boolean resultsAvailable = cs.execute();
while (resultsAvailable)
{
results.add(cs.getResultSet());
resultsAvailable = cs.getMoreResults();
}
return results;
}
});
}
Just going to look at translating a ResultSet into List<Map<String, Object>>.
You can use the resultSet.getMetaData() method to work out what columns are in the data:
ResultSetMetaData meta = resultSet.getMetaData();
int colcount = meta.getColumnCount();
for (int i = 1; i <= colcount; i++)
{
String name = meta.getColumnLabel(i); // This is the name of the column
int type = meta.getColumnType(i); // from java.sql.Types
// Maybe add to a Map,List, etc...
}
You can then do as the other commentors have mentioned do a loop through the ResultSet pulling out the data you need:
while (resultSet.hasNext())
{
resultSet.next();
// Find the columns you want to extract (via the above method maybe) and add to your row.
}
This code might be easier to use in most cases:
Map<String,Object> resultSets = new JdbcTemplate(dataSource)
.call(con -> con.prepareCall(query), new ArrayList<>());
I have used below method to get List of ResultSet in form of List<Map<String, Object>>
public List<List<Map<String, Object>>> executeProcedure(final String sql) {
return jdbcTemplate.execute(new CallableStatementCreator() {
#Override
public CallableStatement createCallableStatement(Connection con) throws SQLException {
return con.prepareCall(sql);
}
}, new CallableStatementCallback<List<List<Map<String, Object>>>>() {
#Override
public List<List<Map<String, Object>>> doInCallableStatement(CallableStatement cs) throws SQLException {
boolean resultsAvailable = cs.execute();
List<List<Map<String, Object>>> list = new ArrayList<List<Map<String, Object>>>();
while (resultsAvailable) {
ResultSet resultSet = cs.getResultSet();
List<Map<String, Object>> subList = new ArrayList<Map<String, Object>>();
while (resultSet.next()) {
ResultSetMetaData meta = resultSet.getMetaData();
int colcount = meta.getColumnCount();
Map<String, Object> map = new HashMap<String, Object>();
for (int i = 1; i <= colcount; i++) {
String name = meta.getColumnLabel(i);
map.put(name, resultSet.getString(i));
}
subList.add(map);
}
list.add(subList);
resultsAvailable = cs.getMoreResults();
}
return list;
}
});
}
Related
I'm wondering if is there possibility to map first row from Oracle Cursor to java.util.Map (using column names as keys) in iBatis version 2.3.4.726. I came up to with TypeHandlerCallback:
public class MapResultTypeHandler implements TypeHandlerCallback {
#Override
public void setParameter(final ParameterSetter parameterSetter, final Object o) throws SQLException {
throw new UnsupportedOperationException();
}
#Override
public Map<String, Object> getResult(final ResultGetter resultGetter) throws SQLException {
ResultSet resultSet = resultGetter.getResultSet();
ResultSetMetaData metaData = resultSet.getMetaData();
int columnsNumber = metaData.getColumnCount();
Map<String, Object> result = new HashMap<>(columnsNumber);
do {
for(int i=1; i <= columnsNumber; ++i) {
result.put(metaData.getColumnName(i), resultSet.getObject(i));
}
} while (resultSet.next());
return result;
}
#Override
public Object valueOf(final String s) {
return null;
}
}
Unfortunately method getMetaData throws:
UnsupportedOperationException("CallableStatement does not support this method.");
Is there other possibility to achieve this goal?
I finally managed to map Oracle Cursor to Map! I had to modify first row of getResult method. It should look like:
ResultSet resultSet = (ResultSet) resultGetter.getObject();
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.
I've been working on a web-service that returns an arraylist. How can I add the returning arraylist to jtable and display?
ArrayList customerDetails = new ArrayList();
try {
String sqlQuery = "SELECT * FROM customer WHERE AccountNumber="+accountNumber;
PreparedStatement stmt = DatabaseConnection.dBconn().prepareStatement(sqlQuery);
ResultSet rs = stmt.executeQuery();
while (rs.next()) {
customerDetails.add(rs.getString("Name"));
customerDetails.add(rs.getString("DoB"));
customerDetails.add(rs.getString("Address"));
customerDetails.add(rs.getString("Mobile"));
customerDetails.add(rs.getString("Email"));
customerDetails.add(rs.getString("AccountType"));
customerDetails.add(rs.getString("AccountNumber"));
customerDetails.add(rs.getString("SortCode"));
customerDetails.add(rs.getString("Balance"));
customerDetails.add(rs.getString("Card"));
}
return customerDetails;
} catch (SQLException err) {
System.out.println(err.getMessage());
}
return customerDetails;
Let's start with the fact that your ArrayList is not structured as a row/columns grouping, you will need a List within a List, where the outer list is the rows and the inner list are the column values
While we're at it, let's also make use of the PreparedStatement properly and manage the resources so they are closed properly while we're at it
ArrayList<List<String>> customerDetails = new ArrayList<>(25);
String sqlQuery = "SELECT * FROM customer WHERE AccountNumber=?";
try (PreparedStatement stmt = DatabaseConnection.dBconn().prepareStatement(sqlQuery)) {
stmt.setString(1, accountNumber);
try (ResultSet rs = stmt.executeQuery()) {
while (rs.next()) {
List<String> rowDetails = new ArrayList<>(10);
rowDetails.add(rs.getString("Name"));
rowDetails.add(rs.getString("DoB"));
rowDetails.add(rs.getString("Address"));
rowDetails.add(rs.getString("Mobile"));
rowDetails.add(rs.getString("Email"));
rowDetails.add(rs.getString("AccountType"));
rowDetails.add(rs.getString("AccountNumber"));
rowDetails.add(rs.getString("SortCode"));
rowDetails.add(rs.getString("Balance"));
rowDetails.add(rs.getString("Card"));
customerDetails.add(rowDetails);
}
}
} catch (SQLException err) {
System.out.println(err.getMessage());
}
return customerDetails;
Have a look at Using Prepared Statements and The try-with-resources Statement for more details
Now, we need a TableModel which can support it, at very basic level...
public class ListTableModel extends AbstractTableModel {
private List<List<String>> rows;
private List<String> columnNames;
public ListTableModel(List<String> columnNames, List<List<String>> rows) {
this.rows = new ArrayList<>(rows);
this.columnNames = columnNames;
}
#Override
public int getRowCount() {
return rows.size();
}
#Override
public int getColumnCount() {
return columnNames.size();
}
#Override
public String getColumnName(int column) {
return columnNames.get(column);
}
#Override
public Class<?> getColumnClass(int columnIndex) {
Class type = String.class;
return type;
}
#Override
public Object getValueAt(int rowIndex, int columnIndex) {
List<String> rowData = rows.get(rowIndex);
return rowData.get(columnIndex);
}
}
This takes a List for the column names and a List<List> for the row data.
Personally, I'd prefer to wrap the data into some kind of Plain Old Java Object (POJO) as it encapsulates the data and provides greater flexibility when displaying it (ie, I need to display all the properties of the object if I don't want to)
Take a look at How to Use Tables for more details
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.
I've ran into a problem of having to run a number of different queries on the DB (different return types, different number of columns, etc).
While writing that i started to wonder if there's a proper way of writing a helper function.
It seemed that it's really easy to write a function that returns a ResultSet.
However since it a) doesn't close connection b) doesn't close the result set it seems as a possibly working, but improper solution. Is there any place to dump in all results so that they can be returned safely.
(Only thing i could come up with, is just returning a 2D string array (after converting all data to strings) and then converting it all back)
EDIT : Sorry for not writing clear, was wondering if there's any way to just store the result of the query as is (don't need to modify it) without writing a separate method for every possible return type.
The idea behind a 2d string list is being able to store the query values as is.
Col1 Row1 | Col2 Row1 | Col3 Row1
Col1 Row2 | Col2 Row2 | Col3 Row2
EDIT 2 Thank you for replies, i guess i'll just write a small parser for it.
You shouldn't be returning resultSets, you should read the results from the resultset into some kind of container object. A ResultSet is a wrapper around a database cursor, it goes away when the connection closes. It's something you read from and close right away, not something you can pass around your application.
Look at how spring-jdbc does it. You implement a resultSetMapper that is passed to the method on the JdbcTemplate.
Several observations:
You don't need to use Spring to use spring-jdbc. However, I see very little value in reimplementing this stuff yourself.
It's not the job of the code that reads the ResultSet to open and close connections, that needs to be elsewhere.
I'd recommend looking at Spring JDBC. Don't write such a thing yourself. It's already been done, and quite well.
For example, I don't like your idea of returning a List of Strings. You lose a lot of info that way. I'd return a Map of Lists (column view) or List of Maps (row view).
If you must, here are some database utilities that would get you started.
package persistence;
import java.sql.*;
import java.util.*;
/**
* util.DatabaseUtils
* User: Michael
* Date: Aug 17, 2010
* Time: 7:58:02 PM
*/
public class DatabaseUtils {
/*
private static final String DEFAULT_DRIVER = "oracle.jdbc.driver.OracleDriver";
private static final String DEFAULT_URL = "jdbc:oracle:thin:#host:1521:database";
private static final String DEFAULT_USERNAME = "username";
private static final String DEFAULT_PASSWORD = "password";
*/
/*
private static final String DEFAULT_DRIVER = "org.postgresql.Driver";
private static final String DEFAULT_URL = "jdbc:postgresql://localhost:5432/party";
private static final String DEFAULT_USERNAME = "pgsuper";
private static final String DEFAULT_PASSWORD = "pgsuper";
*/
private static final String DEFAULT_DRIVER = "com.mysql.jdbc.Driver";
private static final String DEFAULT_URL = "jdbc:mysql://localhost:3306/party";
private static final String DEFAULT_USERNAME = "party";
private static final String DEFAULT_PASSWORD = "party";
public static void main(String[] args) {
long begTime = System.currentTimeMillis();
String driver = ((args.length > 0) ? args[0] : DEFAULT_DRIVER);
String url = ((args.length > 1) ? args[1] : DEFAULT_URL);
String username = ((args.length > 2) ? args[2] : DEFAULT_USERNAME);
String password = ((args.length > 3) ? args[3] : DEFAULT_PASSWORD);
Connection connection = null;
try {
connection = createConnection(driver, url, username, password);
DatabaseMetaData meta = connection.getMetaData();
System.out.println(meta.getDatabaseProductName());
System.out.println(meta.getDatabaseProductVersion());
String sqlQuery = "SELECT PERSON_ID, FIRST_NAME, LAST_NAME FROM PERSON ORDER BY LAST_NAME";
System.out.println("before insert: " + query(connection, sqlQuery, Collections.EMPTY_LIST));
connection.setAutoCommit(false);
String sqlUpdate = "INSERT INTO PERSON(FIRST_NAME, LAST_NAME) VALUES(?,?)";
List parameters = Arrays.asList("Foo", "Bar");
int numRowsUpdated = update(connection, sqlUpdate, parameters);
connection.commit();
System.out.println("# rows inserted: " + numRowsUpdated);
System.out.println("after insert: " + query(connection, sqlQuery, Collections.EMPTY_LIST));
} catch (Exception e) {
rollback(connection);
e.printStackTrace();
} finally {
close(connection);
long endTime = System.currentTimeMillis();
System.out.println("wall time: " + (endTime - begTime) + " ms");
}
}
public static Connection createConnection(String driver, String url, String username, String password) throws ClassNotFoundException, SQLException {
Class.forName(driver);
if ((username == null) || (password == null) || (username.trim().length() == 0) || (password.trim().length() == 0)) {
return DriverManager.getConnection(url);
} else {
return DriverManager.getConnection(url, username, password);
}
}
public static void close(Connection connection) {
try {
if (connection != null) {
connection.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
public static void close(Statement st) {
try {
if (st != null) {
st.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
public static void close(ResultSet rs) {
try {
if (rs != null) {
rs.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
public static void rollback(Connection connection) {
try {
if (connection != null) {
connection.rollback();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
public static List<Map<String, Object>> map(ResultSet rs) throws SQLException {
List<Map<String, Object>> results = new ArrayList<Map<String, Object>>();
try {
if (rs != null) {
ResultSetMetaData meta = rs.getMetaData();
int numColumns = meta.getColumnCount();
while (rs.next()) {
Map<String, Object> row = new HashMap<String, Object>();
for (int i = 1; i <= numColumns; ++i) {
String name = meta.getColumnName(i);
Object value = rs.getObject(i);
row.put(name, value);
}
results.add(row);
}
}
} finally {
close(rs);
}
return results;
}
public static List<Map<String, Object>> query(Connection connection, String sql, List<Object> parameters) throws SQLException {
List<Map<String, Object>> results = null;
PreparedStatement ps = null;
ResultSet rs = null;
try {
ps = connection.prepareStatement(sql);
int i = 0;
for (Object parameter : parameters) {
ps.setObject(++i, parameter);
}
rs = ps.executeQuery();
results = map(rs);
} finally {
close(rs);
close(ps);
}
return results;
}
public static int update(Connection connection, String sql, List<Object> parameters) throws SQLException {
int numRowsUpdated = 0;
PreparedStatement ps = null;
try {
ps = connection.prepareStatement(sql);
int i = 0;
for (Object parameter : parameters) {
ps.setObject(++i, parameter);
}
numRowsUpdated = ps.executeUpdate();
} finally {
close(ps);
}
return numRowsUpdated;
}
}
You can write helper functions that parse a ResultSet and convert it into an ArrayList or an array or even the fields of an object. For instance, lets say you have a table of orders and then a query returns all of the rows of that table for a particular user (customer). We could then do something like this:
static List<Order> parseOrder(ResultSet rs) {
ArrayList<Order> orderList = new ArrayList<>();
while(rs.next() ) {
Order order = new Order();
order.setID(rs.getInt(1));
order.setCustomerID(rs.getInt(2));
order.setItemName(rs.getString(3));
orderList.add(order);
}
return orderList;
}
Simply turning the result set into an array of an array of Objects would be more general, but probably less useful.
I would leave it up to the calling function to close this ResultSet and possible the PreparedStatement (or Statement) and database connection.