从Spring JdbcTemplate中提取ResultSetMetaData

时间:2013-06-01 08:13:24

标签: resultset spring-jdbc

我正在尝试使用Spring resultsetmeta模板获取jdbc数据。如果返回至少一行,它可以正常工作。

如果没有返回行,即空resultSet,则会出现问题。

我已经尝试了很多但仍然坚持相同。如果有任何解决方案,请帮助我。

另外,我在春天找到了ResultSetWrappingSqlRowSetMetaData课。这在我的背景下有用吗?

感谢您的帮助。

2 个答案:

答案 0 :(得分:12)

最后我找到了问题的答案。以下是代码:

template.query(builder.toString(),new ResultSetExtractor<Integer>() {

    @Override
    public Integer extractData(ResultSet rs) throws SQLException, DataAccessException {

        ResultSetMetaData rsmd = rs.getMetaData();
    int columnCount = rsmd.getColumnCount();
    for(int i = 1 ; i <= columnCount ; i++){
        SQLColumn column = new SQLColumn();
    column.setName(rsmd.getColumnName(i));
    column.setAutoIncrement(rsmd.isAutoIncrement(i));
    column.setType(rsmd.getColumnTypeName(i));
    column.setTypeCode(rsmd.getColumnType(i));
    column.setTableName(sqlTable.getName().toUpperCase());
    columns.add(column);
    }

    return columnCount;
}
});

有关详细说明,请访问here

答案 1 :(得分:0)

您也可以使用JdbcUtils

@SuppressWarnings("unchecked")
public static List<TableColumnTypeMap> getTableColumns(DataSource dataSource,
    String tableName) {
    try {

        return (List<TableColumnTypeMap>) JdbcUtils.extractDatabaseMetaData(dataSource,
            new DatabaseMetaDataCallback() {
                @Override
                public Object processMetaData(DatabaseMetaData dbmd)
                    throws SQLException, MetaDataAccessException {
                    ResultSet rs = dbmd
                        .getColumns("", "%", tableName + "%", null);
                    List<TableColumnTypeMap> list = new ArrayList();
                    while (rs.next()) {

                        String tableCat = rs.getString("TABLE_CAT");
                        String tableSchem = rs.getString("TABLE_SCHEM");
                        String tableName = rs.getString("TABLE_NAME");
                        String columnName = rs.getString("COLUMN_NAME");
                        String typeName = rs.getString("TYPE_NAME");
                        String columnSize = rs.getString("COLUMN_SIZE");
                        String nullable = rs.getString("NULLABLE");
                        String remarks = rs.getString("REMARKS");
                        int dataType = rs.getInt("DATA_TYPE");

                        System.out.println(tableName);
                        TableColumnTypeMap tableColumnTypeMap = new TableColumnTypeMap()
                            .setColumnName(columnName)
                            .setColumnType(typeName)
                            .setJavaClassName(getColumnCLassName(dataType))
                            .setRemarks(remarks);
                        list.add(tableColumnTypeMap);
                    }
                    return list;
                }
            });
    } catch (MetaDataAccessException ex) {
        throw new RuntimeException("get table list failed", ex);
    }
}

对于Java类映射:

private static String getColumnCLassName(int sqlType) {
    String className = String.class.getName();

    switch (sqlType) {

        case Types.NUMERIC:
        case Types.DECIMAL:
            className = java.math.BigDecimal.class.getName();
            break;

        case Types.BIT:
            className = java.lang.Boolean.class.getName();
            break;

        case Types.TINYINT:
            className = java.lang.Byte.class.getName();
            break;

        case Types.SMALLINT:
            className = java.lang.Short.class.getName();
            break;

        case Types.INTEGER:
            className = java.lang.Integer.class.getName();
            break;

        case Types.BIGINT:
            className = java.lang.Long.class.getName();
            break;

        case Types.REAL:
            className = java.lang.Float.class.getName();
            break;

        case Types.FLOAT:
        case Types.DOUBLE:
            className = java.lang.Double.class.getName();
            break;

        case Types.BINARY:
        case Types.VARBINARY:
        case Types.LONGVARBINARY:
            className = "byte[]";
            break;

        case Types.DATE:
            className = java.sql.Date.class.getName();
            break;

        case Types.TIME:
            className = java.sql.Time.class.getName();
            break;

        case Types.TIMESTAMP:
            className = java.sql.Timestamp.class.getName();
            break;

        case Types.BLOB:
            className = java.sql.Blob.class.getName();
            break;

        case Types.CLOB:
            className = java.sql.Clob.class.getName();
            break;
        default:
            break;
    }

    return className;
}

对于结果类

 /**
 * @author ryan
 * @date 19-7-15 下午6:05
 */
@Data
@Accessors(chain = true)
public class TableColumnTypeMap {

    private String columnName;
    private String columnType;
    private String javaClassName;
    private String remarks;

}