Search in sources :

Example 86 with ResultSetMetaData

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++);
}
Also used : ResultSetMetaData(java.sql.ResultSetMetaData)

Example 87 with ResultSetMetaData

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();
}
Also used : ResultSetMetaData(java.sql.ResultSetMetaData) ResultSet(java.sql.ResultSet) SqlReturnResultSet(org.springframework.jdbc.core.SqlReturnResultSet) List(java.util.List) HashMap(java.util.HashMap) Map(java.util.Map) Test(org.junit.Test)

Example 88 with ResultSetMetaData

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;
}
Also used : ResultSetMetaData(java.sql.ResultSetMetaData) ResultSet(java.sql.ResultSet) ArrayList(java.util.ArrayList) PreparedStatement(java.sql.PreparedStatement) SQLException(java.sql.SQLException)

Example 89 with ResultSetMetaData

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");
    }
}
Also used : ResultSetMetaData(java.sql.ResultSetMetaData) SQLException(java.sql.SQLException) BaseObject(com.github.knightliao.apollo.db.bo.BaseObject) Method(java.lang.reflect.Method) SQLException(java.sql.SQLException)

Example 90 with ResultSetMetaData

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, ", ");
}
Also used : ResultSetMetaData(java.sql.ResultSetMetaData) ArrayList(java.util.ArrayList)

Aggregations

ResultSetMetaData (java.sql.ResultSetMetaData)414 ResultSet (java.sql.ResultSet)262 Statement (java.sql.Statement)118 SQLException (java.sql.SQLException)111 Test (org.junit.Test)109 PreparedStatement (java.sql.PreparedStatement)95 Connection (java.sql.Connection)64 ArrayList (java.util.ArrayList)61 DatabaseMetaData (java.sql.DatabaseMetaData)40 HashMap (java.util.HashMap)40 Map (java.util.Map)28 List (java.util.List)17 LinkedHashMap (java.util.LinkedHashMap)15 IOException (java.io.IOException)14 BigDecimal (java.math.BigDecimal)12 HashSet (java.util.HashSet)12 ParameterMetaData (java.sql.ParameterMetaData)10 Properties (java.util.Properties)10 KnownFailure (dalvik.annotation.KnownFailure)9 Test (org.testng.annotations.Test)9