zoukankan      html  css  js  c++  java
  • 【Java】jdbc获取主键的字段名

    /**
         *  获取主键的字段名
         * @param tableName 表名
         * @return  表的主键字段名
         */
        public String getPK(String tableName) {
            String PKName = null;
            try {
                DatabaseMetaData dmd = conn.getMetaData();
                ResultSet rs = dmd.getPrimaryKeys(null, "%", tableName);
                rs.next();
                PKName = rs.getString("column_name");
                rs.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
    //        //方法二
    //        String sql = String.format("show index from %s", tableName);
    //        try {
    //            PreparedStatement ps = conn.prepareStatement(sql);
    //            ResultSet rs = ps.executeQuery();
    //            rs.next();
    //            PKName = rs.getString("column_name");
    //            rs.close();
    //            ps.close();
    //        } catch (SQLException throwables) {
    //            throwables.printStackTrace();
    //        }
            return PKName;
        }
  • 相关阅读:
    linux getch()实现
    cppcheck 下载与安装(Liunx)
    apt-get 命令
    nanopb 文档
    VS调试技术
    c 单元测试 check
    GDB 调试
    GCC选项 –I,-l,-L
    作业66
    zhuoye
  • 原文地址:https://www.cnblogs.com/netyts/p/13952661.html
Copyright © 2011-2022 走看看