use of java.sql.ResultSetMetaData in project spring-framework by spring-projects.
the class RowCountCallbackHandler method processRow.
/**
* Implementation of ResultSetCallbackHandler.
* Work out column size if this is the first row, otherwise just count rows.
* <p>Subclasses can perform custom extraction or processing
* by overriding the {@code processRow(ResultSet, int)} method.
* @see #processRow(java.sql.ResultSet, int)
*/
@Override
public final void processRow(ResultSet rs) throws SQLException {
if (this.rowCount == 0) {
ResultSetMetaData rsmd = rs.getMetaData();
this.columnCount = rsmd.getColumnCount();
this.columnTypes = new int[this.columnCount];
this.columnNames = new String[this.columnCount];
for (int i = 0; i < this.columnCount; i++) {
this.columnTypes[i] = rsmd.getColumnType(i + 1);
this.columnNames[i] = JdbcUtils.lookupColumnName(rsmd, i + 1);
}
// could also get column names
}
processRow(rs, this.rowCount++);
}
use of java.sql.ResultSetMetaData in project spring-framework by spring-projects.
the class StoredProcedureTests method testStoredProcedureWithUndeclaredResults.
@Test
@SuppressWarnings("unchecked")
public void testStoredProcedureWithUndeclaredResults() throws Exception {
ResultSet resultSet1 = mock(ResultSet.class);
given(resultSet1.next()).willReturn(true, true, false);
given(resultSet1.getString(2)).willReturn("Foo", "Bar");
ResultSetMetaData resultSetMetaData = mock(ResultSetMetaData.class);
given(resultSetMetaData.getColumnCount()).willReturn(2);
given(resultSetMetaData.getColumnLabel(1)).willReturn("spam");
given(resultSetMetaData.getColumnLabel(2)).willReturn("eggs");
ResultSet resultSet2 = mock(ResultSet.class);
given(resultSet2.getMetaData()).willReturn(resultSetMetaData);
given(resultSet2.next()).willReturn(true, false);
given(resultSet2.getObject(1)).willReturn("Spam");
given(resultSet2.getObject(2)).willReturn("Eggs");
given(callableStatement.execute()).willReturn(true);
given(callableStatement.getUpdateCount()).willReturn(-1);
given(callableStatement.getResultSet()).willReturn(resultSet1, resultSet2);
given(callableStatement.getMoreResults()).willReturn(true, false, false);
given(callableStatement.getUpdateCount()).willReturn(-1, -1, 0, -1);
given(connection.prepareCall("{call " + StoredProcedureWithResultSetMapped.SQL + "()}")).willReturn(callableStatement);
StoredProcedureWithResultSetMapped sproc = new StoredProcedureWithResultSetMapped(dataSource);
Map<String, Object> res = sproc.execute();
assertEquals("incorrect number of returns", 3, res.size());
List<String> rs1 = (List<String>) res.get("rs");
assertEquals(2, rs1.size());
assertEquals("Foo", rs1.get(0));
assertEquals("Bar", rs1.get(1));
List<Object> rs2 = (List<Object>) res.get("#result-set-2");
assertEquals(1, rs2.size());
Object o2 = rs2.get(0);
assertTrue("wron type returned for result set 2", o2 instanceof Map);
Map<String, String> m2 = (Map<String, String>) o2;
assertEquals("Spam", m2.get("spam"));
assertEquals("Eggs", m2.get("eggs"));
Number n = (Number) res.get("#update-count-1");
assertEquals("wrong update count", 0, n.intValue());
verify(resultSet1).close();
verify(resultSet2).close();
}
use of java.sql.ResultSetMetaData in project eweb4j-framework by laiweiwei.
the class JdbcUtil method getResultSetAsMap.
/**
*
* @param con
* @param sql
* @param args
* @return
* @throws JdbcUtilException
*/
public static Map<String, Object> getResultSetAsMap(Connection con, String sql, Object[] args) throws JdbcUtilException {
Map<String, Object> result = null;
if (sql == null) {
return null;
}
PreparedStatement pstmt = null;
ResultSet rs = null;
ResultSetMetaData rsmd = null;
if (con != null) {
try {
sql = sql.replace(";", "");
pstmt = con.prepareStatement(sql);
if (args != null && args.length > 0) {
for (int i = 0; i < args.length; ++i) {
pstmt.setObject(i + 1, args[i]);
}
}
rs = pstmt.executeQuery();
rsmd = rs.getMetaData();
List<String> columns = new ArrayList<String>();
for (int i = 1; i <= rsmd.getColumnCount(); ++i) {
columns.add(rsmd.getColumnName(i));
}
while (rs.next()) {
result = new HashMap<String, Object>();
for (int i = 1; i <= columns.size(); ++i) {
String name = columns.get(i - 1);
result.put(name, rs.getObject(name));
}
}
logOrm(sql, args, result);
} catch (Exception e) {
logException(sql, args, e);
throw new JdbcUtilException(sql + ", args->" + CommonUtil.toJson(args) + " exception ", e);
} finally {
close(rs, pstmt, null);
if (!ConThreadLocal.isTrans()) {
close(con);
}
}
}
return result;
}
use of java.sql.ResultSetMetaData in project disconf by knightliao.
the class Setter method mapRow.
public ENTITY mapRow(ResultSet rs, int rowIndex) throws SQLException {
if (setters == null) {
ResultSetMetaData meta = rs.getMetaData();
setters = getSetters(meta);
}
try {
ENTITY entity = entityClass.newInstance();
K key = isComplexKey ? keyClass.newInstance() : null;
// FIXME
if (key != null) {
entity.setId(key);
}
for (Setter setter : setters) {
String column = setter.columnName;
Method setMethod = setter.method;
Class<?> paramType = setter.setterParamType;
if (setMethod.getName().equals("setId")) {
paramType = keyClass;
}
Object value = MapperUtils.getValue4Type(rs, column, paramType);
// FIXME
if (orMapping.isKeyColumn(column) && key != null) {
load2Entity(key, setMethod, value);
} else {
load2Entity(entity, setMethod, value);
}
}
return entity;
} catch (Exception e) {
e.printStackTrace();
throw new SQLException("error in loadEntity");
}
}
use of java.sql.ResultSetMetaData in project syncany by syncany.
the class TestAssertUtil method getFormattedColumn.
private static String getFormattedColumn(ResultSet resultSet) throws SQLException {
ResultSetMetaData metaData = resultSet.getMetaData();
List<String> formattedColumnLine = new ArrayList<String>();
for (int i = 0; i < metaData.getColumnCount(); i++) {
formattedColumnLine.add(metaData.getColumnName(i + 1) + "=" + resultSet.getString(i + 1));
}
return StringUtil.join(formattedColumnLine, ", ");
}
Aggregations