首页 > 代码库 > java查看数据库信息和对应表的信息

java查看数据库信息和对应表的信息

public class DataBaseMessage {

public static void main(String[] args) {

getMessage();

}

public static void getMessage(){

Connection connection = null;

ResultSet rs = null;

try {

connection = 获取connection;

DatabaseMetaData dbMetaData  = connection.getMetaData();

                         System.out.println("数据库相关信息 : ");

System.out.println("已知的用户: "+ dbMetaData.getUserName());    

System.out.println("系统函数的逗号分隔列表: "+ dbMetaData.getSystemFunctions());    

System.out.println("时间和日期函数的逗号分隔列表: "+ dbMetaData.getTimeDateFunctions());    

System.out.println("字符串函数的逗号分隔列表: "+ dbMetaData.getStringFunctions());    

System.out.println("供应商用于 ‘schema‘ 的首选术语: "+ dbMetaData.getSchemaTerm());    

System.out.println("URL: " + dbMetaData.getURL());    

System.out.println("是否允许只读:" + dbMetaData.isReadOnly());    

System.out.println("的产品名称:" + dbMetaData.getDatabaseProductName());    

System.out.println("版本:" + dbMetaData.getDatabaseProductVersion());    

System.out.println("驱动程序的名称:" + dbMetaData.getDriverName());    

System.out.println("驱动程序的版本:" + dbMetaData.getDriverVersion());   


//查询t_file表格的所有信息

rs = dbMetaData.getColumns(null, null,"表名称", null);

//获取t_file表每条记录的信息

while(rs.next()){

System.out.println();System.out.println();

String column_name = rs.getString("COLUMN_NAME");

String type_name = rs.getString("TYPE_NAME");

int column_size = rs.getInt("COLUMN_SIZE");

boolean is_nullable = rs.getBoolean("IS_NULLABLE");

String column_def = rs.getString("COLUMN_DEF");

System.out.println("column_name :"+column_name+"  **** type_name :"+type_name+"  ****   column_size : "+column_size+"  ****   is_nullable : "+is_nullable +"  ****   column_def : "+column_def);

}

} catch (Exception e) {

e.printStackTrace();

}finally{

try {

//connection.close();

//rs.close();

} catch (Exception e2) {

e2.printStackTrace();

}

}

}

}


本文出自 “11472646” 博客,请务必保留此出处http://11482646.blog.51cto.com/11472646/1877913

java查看数据库信息和对应表的信息