日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > 数据库 >内容正文

数据库

转: databasemetadata 无法获取数据库表备注的解决方法

發布時間:2023/12/3 数据库 53 豆豆
生活随笔 收集整理的這篇文章主要介紹了 转: databasemetadata 无法获取数据库表备注的解决方法 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

轉自: https://blog.csdn.net/10km/article/details/77389038

?

mysql/jdbc:設置useInformationSchema=true讀取表注釋信息(table_comment)

?

問題描述

今天在讀取表的注釋信息(COMMENT)時,發現返回的REMARKS字段返回居然是null.
以下是代碼示例:

DatabaseMetaData meta = this.pConnection.getMetaData(); // 獲取所有表信息 ResultSet resultSet = this.meta.getTables(this.catalog, tableSchema, pattern, this.tableTypes); while (resultSet.next()) {Table table = new Table();# 返回nullString comment=resultSet.getString("REMARKS"); } resultSet.close();
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

原因分析

google找了半天,總算知道原因:
Connector/J 5.0.0以后的版本有一個名為useInformationSchema的數據庫連接參數,
在默認連接參數情況下,useInformationSchema=false,導致Connection.getMetaData()方法返回的DatabaseMetaData 對象是com.mysql.jdbc.DatabaseMetaData,而不是com.mysql.jdbc。DatabaseMetaDataUsingInfoSchema,
DatabaseMetaDataUsingInfoSchema是DatabaseMetaData是的子類,看名稱就能聯想到是通過?INFORMATION_SCHEMA?數據庫獲取數據庫的metadata,可以正確返回table_comment字段。

下面是useInformationSchema的官方說明

useInformationSchema

When connected to MySQL-5.0.7 or newer, should the driver use the INFORMATION_SCHEMA to derive information used by DatabaseMetaData?

Default: false
Since version: 5.0.0
摘自《5.1 Driver/Datasource Class Names, URL Syntax and Configuration Properties for Connector/J》

而父類DatabaseMetaData并不一定能正常返回table_comment字段.

關于INFORMATION_SCHEMA 這里不深入探討,參見《Chapter 24 INFORMATION_SCHEMA Tables》

解決方法

解決的方法也很簡單:
數據庫連接時設置useInformationSchema=true
如何設置數據庫連接參數呢?有兩個途徑

方法一:java代碼實現

# 將所有參數裝入java.util.Properties 對象 Properties props = new Properties(); props.setProperty("username",this.username); props.setProperty("password",this.password); props.setProperty("useInformationSchema", "true"); # 調用getConnection(String,Properties)方法創建連接 this.pConnection = java.sql.DriverManager.getConnection(this.url, props);
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

方法二:連接url參數

直接將參數加到數據庫連接url,如下代碼中在數據連接url中添加了兩個參數characterEncoding=utf8和useInformationSchema=true

String url="jdbc:mysql://localhost:3306/test?characterEncoding=utf8&&useInformationSchema=true" this.pConnection = DriverManager.getConnection(this.url, this.username,this.password);
  • 1
  • 2

關于mysql 連接URL的語法參見:
《5.1 Driver/Datasource Class Names, URL Syntax and Configuration Properties for Connector/J》

參考資料

《Connector/J does not retrieve the table comment in a InnoDB table》
《Retrieve mysql table comment using DatabaseMetaData》
《Chapter 24 INFORMATION_SCHEMA Tables》
《5.1 Driver/Datasource Class Names, URL Syntax and Configuration Properties for Connector/J》

總結

以上是生活随笔為你收集整理的转: databasemetadata 无法获取数据库表备注的解决方法的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。