获取表的字段

时间:2021-03-03 12:17:22
方法一:通过connection来获得
Java代码 获取表的字段 获取表的字段获取表的字段
  1. private String getIdName(Connection conn,String tableName){ 
  2.     String idName = ""
  3.     DatabaseMetaData  metaData = null
  4.     try
  5.          metaData = conn.getMetaData(); 
  6.          ResultSet rs = metaData.getColumns(conn.getCatalog(), "%", tableName, "%ID"); 
  7.          if(rs.next()){ 
  8.              idName = rs.getString("COLUMN_NAME");  
  9.          } 
  10.     } catch (Exception e) { 
  11.         logger.error(e); 
  12.     } 
  13.     return idName; 


方法二:通过结果集ResultSet获得
Java代码 获取表的字段 获取表的字段获取表的字段
  1. private String getIdName(ResultSet rs){ 
  2.     String idName = ""
  3.     ResultSetMetaData metaData = null
  4.     try
  5.          metaData = rs.getMetaData(); 
  6.          idName = metaData.getColumnName(1); 
  7.     } catch (Exception e) { 
  8.         logger.error("查询表的主键名出错! ",e); 
  9.     } 
  10.     return idName;