Java中獲取MySQL表注釋的方法可以通過查詢information_schema數據庫中的表來實現。
以下是一個示例代碼:
import java.sql.*; public class GetTableComment {????public?static?void?main(String[]?args)?{
????????Connection?conn?=?null;
????????Statement?stmt?=?null;
????????ResultSet?rs?=?null;
????????try?{
????????????//?創建數據庫連接
????????????conn?=?DriverManager.getConnection(“jdbc:mysql://localhost:3306/mydatabase”,?
????????????“username”,?“password”);
????????????//?創建Statement對象
????????????stmt?=?conn.createStatement();
????????????//?查詢表的注釋
????????????rs?=?stmt.executeQuery("SELECT?table_comment?FROM?information_schema.tables?WHERE?
????????????table_schema?=?'your_database_name'?AND?table_name?=?'your_table_name'");
????????????//?獲取注釋
????????????if?(rs.next())?{
????????????????String?comment?=?rs.getString(“table_comment”);
????????????????System.out.println("Table?Comment:?"?+?comment);
????????????}
????????}?catch?(SQLException?e)?{
????????????e.printStackTrace();
????????}?finally?{
????????????//?關閉連接和資源
????????????try?{
????????????????if?(rs?!=?null)?rs.close();
????????????????if?(stmt?!=?null)?stmt.close();
????????????????if?(conn?!=?null)?conn.close();
????????????}?catch?(SQLException?e)?{
????????????????e.printStackTrace();
????????????}
????????}
????} }
在上面的代碼中,需要將your_database_name
和your_table_name
替換為你要查詢的數據庫名和表名。同時,需要將username
和password
替換為你的MySQL數據庫的用戶名和密碼。
運行上述代碼,即可獲取指定表的注釋。