- private String getIdName(Connection conn,String tableName){
- String idName = "";
- DatabaseMetaData metaData = null;
- try {
- metaData = conn.getMetaData();
- ResultSet rs = metaData.getColumns(conn.getCatalog(), "%", tableName, "%ID");
- if(rs.next()){
- idName = rs.getString("COLUMN_NAME");
- }
- } catch (Exception e) {
- logger.error(e);
- }
- return idName;
- }
方法二:通过结果集ResultSet获得
- private String getIdName(ResultSet rs){
- String idName = "";
- ResultSetMetaData metaData = null;
- try {
- metaData = rs.getMetaData();
- idName = metaData.getColumnName(1);
- } catch (Exception e) {
- logger.error("查询表的主键名出错! ",e);
- }
- return idName;
- }