DataBase中metadata用法
@说明:
利用db的metadata可以获取数据库表的结构,从而利用xml生成技术生成hibernate文件。
package com.chenhuan.test;
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.DriverManager;
import java.sql.ResultSet;
public class metaData {
public static String url = "jdbc:mysql://localhost/pro?Unicode=true&characterEncoding=gb2312";
public static String driver = "com.mysql.jdbc.Driver";
public static String username = "root";
public static String password = "123";
public static Connection conn;
public static void main(String agrs[]) throws Exception{
initConnect();
DatabaseMetaData dmd = conn.getMetaData();
/*获取表的名称*/
ResultSet rs = dmd.getTables(null, null, null, new String[]{"TABLE"});
while(rs.next()){
System.out.println("table name:"+rs.getString("TABLE_NAME"));
}
/*获取特定表的字段名称和类型*/
ResultSet rs1 = dmd.getColumns(null, null, "admin", null);
while(rs1.next()){
System.out.println("column name:"+rs1.getString("COLUMN_NAME"));
System.out.println("column type:"+rs1.getString("DATA_TYPE"));
}
}
public static void initConnect(){
try {
Class.forName(driver);
conn = DriverManager.getConnection(url,username,password);
} catch (Exception e) {
e.printStackTrace();
}
}
}