/**
     *  获取主键的字段名
     * @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;
    }

 

相关文章:

  • 2022-12-23
  • 2022-02-09
  • 2021-11-12
  • 2021-06-11
  • 2021-10-02
  • 2022-01-28
  • 2021-11-09
猜你喜欢
  • 2022-12-23
  • 2022-01-13
  • 2021-10-14
  • 2021-07-09
  • 2021-11-24
  • 2021-09-10
  • 2022-12-23
相关资源
相似解决方案