Search in sources :

Example 81 with ResultSetMetaData

use of java.sql.ResultSetMetaData in project jdk8u_jdk by JetBrains.

the class CommonCachedRowSetTests method compareRowSets.

/*
     * Utility method to compare two rowsets
     */
private void compareRowSets(CachedRowSet crs, CachedRowSet crs1) throws Exception {
    int rows = crs.size();
    assertTrue(rows == crs1.size());
    ResultSetMetaData rsmd = crs.getMetaData();
    compareMetaData(rsmd, crs1.getMetaData());
    int cols = rsmd.getColumnCount();
    for (int row = 1; row <= rows; row++) {
        crs.absolute((row));
        crs1.absolute(row);
        for (int col = 1; col <= cols; col++) {
            compareColumnValue(JDBCType.valueOf(rsmd.getColumnType(col)), crs, crs1, col);
        }
    }
}
Also used : ResultSetMetaData(java.sql.ResultSetMetaData)

Example 82 with ResultSetMetaData

use of java.sql.ResultSetMetaData in project jdk8u_jdk by JetBrains.

the class RowSetMetaDataTests method test99.

/*
     * Validate isWrapperFor and unwrap work correctly
     */
@SuppressWarnings("unchecked")
@Test
public void test99() throws Exception {
    RowSetMetaData rsmd1 = rsmd;
    ResultSetMetaData rsmd2 = rsmd;
    Class clzz = rsmd.getClass();
    assertTrue(rsmd1.isWrapperFor(clzz));
    assertTrue(rsmd2.isWrapperFor(clzz));
    RowSetMetaDataImpl rsmdi = (RowSetMetaDataImpl) rsmd2.unwrap(clzz);
    // False should be returned
    assertFalse(rsmd1.isWrapperFor(this.getClass()));
    assertFalse(rsmd2.isWrapperFor(this.getClass()));
}
Also used : ResultSetMetaData(java.sql.ResultSetMetaData) RowSetMetaData(javax.sql.RowSetMetaData) RowSetMetaDataImpl(javax.sql.rowset.RowSetMetaDataImpl) Test(org.testng.annotations.Test) BaseTest(util.BaseTest)

Example 83 with ResultSetMetaData

use of java.sql.ResultSetMetaData in project druid by alibaba.

the class ResultSetProxyImpl method getMetaData.

@Override
public ResultSetMetaData getMetaData() throws SQLException {
    FilterChainImpl chain = createChain();
    ResultSetMetaData value = chain.resultSet_getMetaData(this);
    recycleFilterChain(chain);
    return value;
}
Also used : ResultSetMetaData(java.sql.ResultSetMetaData) FilterChainImpl(com.alibaba.druid.filter.FilterChainImpl)

Example 84 with ResultSetMetaData

use of java.sql.ResultSetMetaData in project druid by alibaba.

the class JdbcUtils method printResultSet.

public static void printResultSet(ResultSet rs, PrintStream out, boolean printHeader, String seperator) throws SQLException {
    ResultSetMetaData metadata = rs.getMetaData();
    int columnCount = metadata.getColumnCount();
    if (printHeader) {
        for (int columnIndex = 1; columnIndex <= columnCount; ++columnIndex) {
            if (columnIndex != 1) {
                out.print(seperator);
            }
            out.print(metadata.getColumnName(columnIndex));
        }
    }
    out.println();
    while (rs.next()) {
        for (int columnIndex = 1; columnIndex <= columnCount; ++columnIndex) {
            if (columnIndex != 1) {
                out.print(seperator);
            }
            int type = metadata.getColumnType(columnIndex);
            if (type == Types.VARCHAR || type == Types.CHAR || type == Types.NVARCHAR || type == Types.NCHAR) {
                out.print(rs.getString(columnIndex));
            } else if (type == Types.DATE) {
                Date date = rs.getDate(columnIndex);
                if (rs.wasNull()) {
                    out.print("null");
                } else {
                    out.print(date.toString());
                }
            } else if (type == Types.BIT) {
                boolean value = rs.getBoolean(columnIndex);
                if (rs.wasNull()) {
                    out.print("null");
                } else {
                    out.print(Boolean.toString(value));
                }
            } else if (type == Types.BOOLEAN) {
                boolean value = rs.getBoolean(columnIndex);
                if (rs.wasNull()) {
                    out.print("null");
                } else {
                    out.print(Boolean.toString(value));
                }
            } else if (type == Types.TINYINT) {
                byte value = rs.getByte(columnIndex);
                if (rs.wasNull()) {
                    out.print("null");
                } else {
                    out.print(Byte.toString(value));
                }
            } else if (type == Types.SMALLINT) {
                short value = rs.getShort(columnIndex);
                if (rs.wasNull()) {
                    out.print("null");
                } else {
                    out.print(Short.toString(value));
                }
            } else if (type == Types.INTEGER) {
                int value = rs.getInt(columnIndex);
                if (rs.wasNull()) {
                    out.print("null");
                } else {
                    out.print(Integer.toString(value));
                }
            } else if (type == Types.BIGINT) {
                long value = rs.getLong(columnIndex);
                if (rs.wasNull()) {
                    out.print("null");
                } else {
                    out.print(Long.toString(value));
                }
            } else if (type == Types.TIMESTAMP) {
                out.print(String.valueOf(rs.getTimestamp(columnIndex)));
            } else if (type == Types.DECIMAL) {
                out.print(String.valueOf(rs.getBigDecimal(columnIndex)));
            } else if (type == Types.CLOB) {
                out.print(String.valueOf(rs.getString(columnIndex)));
            } else if (type == Types.JAVA_OBJECT) {
                Object object = rs.getObject(columnIndex);
                if (rs.wasNull()) {
                    out.print("null");
                } else {
                    out.print(String.valueOf(object));
                }
            } else if (type == Types.LONGVARCHAR) {
                Object object = rs.getString(columnIndex);
                if (rs.wasNull()) {
                    out.print("null");
                } else {
                    out.print(String.valueOf(object));
                }
            } else if (type == Types.NULL) {
                out.print("null");
            } else {
                Object object = rs.getObject(columnIndex);
                if (rs.wasNull()) {
                    out.print("null");
                } else {
                    if (object instanceof byte[]) {
                        byte[] bytes = (byte[]) object;
                        String text = HexBin.encode(bytes);
                        out.print(text);
                    } else {
                        out.print(String.valueOf(object));
                    }
                }
            }
        }
        out.println();
    }
}
Also used : ResultSetMetaData(java.sql.ResultSetMetaData) Date(java.sql.Date)

Example 85 with ResultSetMetaData

use of java.sql.ResultSetMetaData in project druid by alibaba.

the class JdbcUtils method executeQuery.

public static List<Map<String, Object>> executeQuery(Connection conn, String sql, List<Object> parameters) throws SQLException {
    List<Map<String, Object>> rows = new ArrayList<Map<String, Object>>();
    PreparedStatement stmt = null;
    ResultSet rs = null;
    try {
        stmt = conn.prepareStatement(sql);
        setParameters(stmt, parameters);
        rs = stmt.executeQuery();
        ResultSetMetaData rsMeta = rs.getMetaData();
        while (rs.next()) {
            Map<String, Object> row = new LinkedHashMap<String, Object>();
            for (int i = 0, size = rsMeta.getColumnCount(); i < size; ++i) {
                String columName = rsMeta.getColumnLabel(i + 1);
                Object value = rs.getObject(i + 1);
                row.put(columName, value);
            }
            rows.add(row);
        }
    } finally {
        JdbcUtils.close(rs);
        JdbcUtils.close(stmt);
    }
    return rows;
}
Also used : ResultSetMetaData(java.sql.ResultSetMetaData) ArrayList(java.util.ArrayList) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) LinkedHashMap(java.util.LinkedHashMap)

Aggregations

ResultSetMetaData (java.sql.ResultSetMetaData)449 ResultSet (java.sql.ResultSet)279 SQLException (java.sql.SQLException)125 Statement (java.sql.Statement)123 Test (org.junit.Test)109 PreparedStatement (java.sql.PreparedStatement)107 ArrayList (java.util.ArrayList)78 Connection (java.sql.Connection)68 HashMap (java.util.HashMap)44 DatabaseMetaData (java.sql.DatabaseMetaData)41 Map (java.util.Map)31 LinkedHashMap (java.util.LinkedHashMap)22 List (java.util.List)18 IOException (java.io.IOException)15 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 Field (java.lang.reflect.Field)9