1. 程式人生 > >獲取表格的所有欄位名稱,型別,註釋

獲取表格的所有欄位名稱,型別,註釋

public class ColumnVo {
    private String columnName;//欄位名稱
    private String columnType;//欄位型別
    private String remarks;//欄位註釋

}

 

/**
 * Description : 獲取表格的所有欄位名稱,型別,註釋
 *
 * @param jdbcTemplate
 * @param tableName
 * @return : java.util.List<com.hiynn.core.workflow.vo.ColumnVo>
 * @date : 2018-11-02   16:25
 * @author : PZG
 */
public static List<ColumnVo> getColumnNames(JdbcTemplate jdbcTemplate, String tableName) {
    try {
        Connection conn = jdbcTemplate.getDataSource().getConnection();
        DatabaseMetaData dbMetaData = conn.getMetaData();
        ResultSet resultSet = dbMetaData.getColumns(null, "%", tableName, "%");
        List<ColumnVo> columnVos = new ArrayList<>();
        while (resultSet.next()) {
            ColumnVo columnVo = new ColumnVo();
            //欄位名稱
            String columnName = resultSet.getString("COLUMN_NAME");
            columnVo.setColumnName(columnName);
            //註釋
            String remarks = resultSet.getString("REMARKS");
            columnVo.setRemarks(remarks);
            //欄位型別
            String typeName = resultSet.getString("TYPE_NAME");
            columnVo.setColumnType(typeName);
            columnVos.add(columnVo);
        }
        return columnVos;
    } catch (Exception e) {
        e.printStackTrace();
        return null;
    }
}