JDBC解析3_獲取數據連接信息

 

  1. package book.database;  
  2.  
  3. import java.sql.Connection;  
  4. import java.sql.DatabaseMetaData;  
  5. import java.sql.ResultSet;  
  6. import java.sql.ResultSetMetaData;  
  7. import java.sql.SQLException;  
  8. import java.sql.Statement;  
  9.  
  10. /**  
  11.  * 獲取數據庫和表的元數據  
  12.  * 使用DatabaseMetaData獲得數據庫的元數據  
  13.  * 使用ResultSetMetaData獲得表的元數據  
  14.  */  
  15. public class GetMetadata {  
  16.     /**  
  17.      * 獲得數據庫的元數據  
  18.      * @param con   與數據庫的連接  
  19.      */  
  20.     public static void showDatabaseMetadata(Connection con){  
  21.         try {  
  22.             // 得到數據庫的元數據  
  23.             DatabaseMetaData md = con.getMetaData();  
  24.             System.out.println("數據庫" + md.getURL() + "的元數據如下:");  
  25.               
  26.             // 顯示元數據信息  
  27.             System.out.println("驅動: " + md.getDriverName());  
  28.             System.out.println("驅動版本號: " + md.getDriverVersion());  
  29.             System.out.println("登陸用戶名: " + md.getUserName());  
  30.             System.out.println("數據庫產品名: " + md.getDatabaseProductName());  
  31.             System.out.println("數據庫產品版本號: " + md.getDatabaseProductVersion());  
  32.             System.out.println("支持的SQL關鍵字: ");  
  33.             System.out.println(md.getSQLKeywords());  
  34.             System.out.println("操作數字的函數: ");  
  35.             System.out.println(md.getNumericFunctions());  
  36.             System.out.println("操作字符串的函數: ");  
  37.             System.out.println(md.getStringFunctions());  
  38.             System.out.println("系統函數: ");  
  39.             System.out.println(md.getSystemFunctions());  
  40.             System.out.println("時間和日期函數: ");  
  41.             System.out.println(md.getTimeDateFunctions());  
  42.               
  43.         } catch (SQLException e) {  
  44.             e.printStackTrace();  
  45.         }  
  46.     }  
  47.       
  48.     /**  
  49.      * 顯示數據表的元數據,主要是列的信息  
  50.      * @param con   與數據庫的連接  
  51.      * @param tableName  數據表名  
  52.      */  
  53.     public static void showTableMetadata(Connection con, String tableName){  
  54.         String sql = "SELECT * FROM " + tableName;  
  55.         Statement sm = null;  
  56.         try {  
  57.             // 首先獲得表的所有數據  
  58.             sm = con.createStatement();  
  59.             ResultSet rs = sm.executeQuery(sql);  
  60.               
  61.             // 得到結果集的元數據  
  62.             ResultSetMetaData md = rs.getMetaData();  
  63.               
  64.             System.out.println("數據表" + tableName + "的元數據如下:");  
  65.             // 表的列數  
  66.             int columnCount = md.getColumnCount();  
  67.             System.out.println("column count: " + columnCount);  
  68.             System.out.println();  
  69.             StringBuffer sb = new StringBuffer("");  
  70.             sb.append("sn\tname\t\t").append("type\t\t");  
  71.             sb.append("scale\t").append("isNullable");  
  72.             System.out.println(sb);  
  73.             sb.delete(0, sb.length());  
  74.             // 輸出列的屬性信息  
  75.             for (int i=1; i<=columnCount; i++){  
  76.                 sb.append(i).append("\t");  
  77.                 sb.append(md.getColumnName(i)).append("\t\t");  
  78.                 sb.append(md.getColumnTypeName(i)).append("\t\t");  
  79.                 sb.append(md.getScale(i)).append("\t");  
  80.                 sb.append(md.isNullable(i));  
  81.                 System.out.println(sb);  
  82.                 sb.delete(0, sb.length());  
  83.             }  
  84.             rs.close();  
  85.         } catch (SQLException e) {  
  86.             e.printStackTrace();  
  87.         } finally {  
  88.             // 關閉Statement  
  89.             if (sm != null){  
  90.                 try {  
  91.                     sm.close();  
  92.                 } catch (SQLException e1) {  
  93.                     e1.printStackTrace();  
  94.                 }  
  95.             }  
  96.         }  
  97.     }  
  98.  
  99.     public static void main(String[] args) throws ClassNotFoundException,  
  100.             SQLException {  
  101.         String dbName = "studentdb";  
  102.         String tableName = "student_basic";  
  103.         String userName = "test";  
  104.         String password = "test";  
  105.  
  106.         Connection con = null;  
  107.         try {  
  108.             // 獲得數據庫連接  
  109.             con = DBConnector.getMySQLConnection(null, null, null, dbName,  
  110.                     userName, password);  
  111.             // 顯示數據庫的元信息  
  112.             GetMetadata.showDatabaseMetadata(con);  
  113.             System.out.println();  
  114.             // 顯示數據表的元信息  
  115.             GetMetadata.showTableMetadata(con, tableName);  
  116.         } catch (ClassNotFoundException e1) {  
  117.             throw e1;  
  118.         } catch (SQLException e2) {  
  119.             throw e2;  
  120.         } finally {  
  121.             // 關閉數據庫連接  
  122.             if (con != null) {  
  123.                 try {  
  124.                     con.close();  
  125.                 } catch (SQLException e1) {  
  126.                     e1.printStackTrace();  
  127.                 }  
  128.             }  
  129.         }  
  130.     }  

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章