首页 > 代码库 > 元数据案例

元数据案例

package wuerer;
import java.security.interfaces.RSAKey;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.Statement;

import com.mysql.jdbc.DatabaseMetaData;

public class YuanShu {

    public static void main(String[] args) throws Exception {
        Class.forName("com.mysql.jdbc.Driver");
        Connection conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/boooks","root","tiger");
//    java.sql.DatabaseMetaData dmd= conn.getMetaData();
//        System.out.println(dmd.getDriverName());
//        System.out.println(dmd.getDatabaseProductName());
//        System.out.println(dmd.getDatabaseProductVersion());
//        System.out.println(dmd.getURL());
//        System.out.println(dmd.getUserName());
        
        Statement st=conn.createStatement();
        ResultSet rs=st.executeQuery("select * from book");
        ResultSetMetaData rmd=rs.getMetaData();
        System.out.println("列数:"+rmd.getColumnCount());
        int size=rmd.getColumnCount();
        for(int i=1;i<=size;i++){
            System.out.println(rmd.getColumnName(i)+","+rmd.getColumnTypeName(i)+",");
        }
        while(rs.next()){
            for(int i=1;i<=size;i++){
                System.out.print(rs.getObject(i)+"\t");
                
            }
        System.out.println();
        }

      conn.close();

    }

}

 

元数据案例