4

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

当没有返回行(即空的)时,就会出现问题resultSet

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

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

谢谢您的帮助。

4

2 回答 2

16

Finally I found the answer to my question. Below is the code:

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

For a detailed explanation you can visit here

于 2013-06-09T08:40:41.027 回答
1

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

}
于 2019-07-27T08:14:51.953 回答