如何获得表主键ID的字段名
1个回答
展开全部
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("查询表的主键名出错!
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("查询表的主键名出错!
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询