JDBC MetaData有两种: 
• 结果集的MetaData 
• 数据库的MetaData
#####################Michael分割线#########################
结果集的MetaData 
• ResultSet的getMetaData方法返回结果集元数据对象ResultSetMetaData
image
ConnectionUtil.java
package com.michael.jdbc;    

import java.sql.Connection;    
import java.sql.DriverManager;    
import java.util.Properties;    

public class ConnectionUtil {    
        //第一种方法    
        public Connection getConnection(){    
                Connection conn = null;    
                try {    
                        //Class.forName加载驱动    
                        Class.forName("com.mysql.jdbc.Driver");    
                        //DriverManager获得连接    
                        conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/jdbc_db","root","mysqladmin");    
                        return conn;    
                } catch (Exception e) {    
                        e.printStackTrace();    
                }    
                return null;    
        }    
        //第二种方法    
        public Connection getConnection(String driver,String url,String user,String password){    
                Connection conn = null;    
                try {    
                        //Class.forName加载驱动    
                        Class.forName(driver);    
                        //DriverManager获得连接    
                        conn = DriverManager.getConnection(url,user,password);    
                        return conn;    
                } catch (Exception e) {    
                        e.printStackTrace();    
                }    
                return null;    
        }    
        //第三种方法    
        public Connection openConnection(){    
                String driver = "";    
                String url = "";    
                String user = "";    
                String password = "";    
                Properties prop = new Properties();    
                Connection conn = null;    
                try {    
                        //加载属性文件    
                        prop.load(this.getClass().getClassLoader().getResourceAsStream("DBConfig.properties"));    
                        driver = prop.getProperty("driver");    
                        url = prop.getProperty("url");    
                        user = prop.getProperty("user");    
                        password = prop.getProperty("password");    
                        //Class.forName加载驱动    
                        Class.forName(driver);    
                        //DriverManager获得连接    
                        conn = DriverManager.getConnection(url,user,password);    
                        return conn;    
                } catch (Exception e) {    
                        e.printStackTrace();    
                }    
                return null;    
        }    


TestMetaData.java
package com.michael.jdbc;    

import java.sql.Connection;    
import java.sql.ResultSet;    
import java.sql.ResultSetMetaData;    
import java.sql.SQLException;    
import java.sql.Statement;    

public class TestMetaData {    
        public static void test(){    
                Connection conn = new ConnectionUtil().openConnection();    
                try {    
                        Statement stmt = conn.createStatement();    
                        String sql = "select * from CustomerTbl";    
                        ResultSet rs = stmt.executeQuery(sql);    
                        ResultSetMetaData rsmd = rs.getMetaData();    
                        int columns = rsmd.getColumnCount();    
                        for (int i=1;i<=columns;i++){    
                                String name = rsmd.getColumnName(i);    
                                int type = rsmd.getColumnType(i);    
                                System.out.println(name+":"+type);    
                        }    
                } catch (SQLException e) {    
                        e.printStackTrace();    
                }finally{    
                                try {    
                                        conn.close();    
                                } catch (SQLException e) {    
                                        e.printStackTrace();    
                                }    
                }    
        }    

Main.java
package com.michael.main;    

import com.michael.jdbc.TestMetaData;    

public class Main {    

        /**    
         * @param args    
         */
    
        public static void main(String[] args) {    
                TestMetaData.test();    
        }    

 
image
数据库的MetaData 
• 通过数据库连接的getMetaData方法可以获得数据库连接的对象实例DatabaseMetaData
image
TestMetaData.java
package com.michael.jdbc;    

import java.sql.Connection;    
import java.sql.DatabaseMetaData;    
import java.sql.SQLException;    

public class TestMetaData {    
        public static void test(){    
                Connection conn = new ConnectionUtil().openConnection();    
                try {    
                        DatabaseMetaData dbmd = conn.getMetaData();    
                        System.out.println(dbmd.getDatabaseProductName());    
                        System.out.println(dbmd.getDatabaseMajorVersion());    
                } catch (SQLException e1) {    
                        e1.printStackTrace();    
                }finally{    
                        try {    
                                conn.close();    
                        } catch (SQLException e) {    
                                e.printStackTrace();    
                        }    
                }    
                /*    
                try {    
                        Statement stmt = conn.createStatement();    
                        String sql = "select * from CustomerTbl";    
                        ResultSet rs = stmt.executeQuery(sql);    
                        ResultSetMetaData rsmd = rs.getMetaData();    
                        int columns = rsmd.getColumnCount();    
                        for (int i=1;i<=columns;i++){    
                                String name = rsmd.getColumnName(i);    
                                int type = rsmd.getColumnType(i);    
                                System.out.println(name+":"+type);    
                        }    
                } catch (SQLException e) {    
                        e.printStackTrace();    
                }finally{    
                                try {    
                                        conn.close();    
                                } catch (SQLException e) {    
                                        e.printStackTrace();    
                                }    
                }    
                */
    
        }    

image
#####################Michael分割线#########################