从Spring JdbcTemplate中提取ResultSetMetaData



我试图使用Spring jdbc模板获取resultsetmeta数据。如果至少返回一行,则正常工作。

问题出现时,没有返回的行,即一个空的resultSet

我已经尝试了很多,但仍然坚持不变。如果有什么解决办法,请帮助我。

同时,我发现ResultSetWrappingSqlRowSetMetaData类在春天。这在我的语境中有用吗?

谢谢你的帮助

我终于找到了问题的答案。下面是代码:

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;
}
});

详细说明请访问

您也可以使用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;
}

for result class

 /**
 * @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;
}

相关内容

  • 没有找到相关文章

最新更新