元数据:代表数据库、表、列的信息;
主要使用的类是:DatabaseMetaData
获取数据库元信息的方法:
利用Connection对象来获取:
Connection.getMetaData()返回DataBaseMetaData对象;
DataBaseMetaData对象
getURL():返回一个String类对象,代表数据库的URL。
getUserName():返回连接当前数据库管理系统的用户名。
getDriverName():返回驱动驱动程序的名称。
getPrimaryKeys(String catalog, String schema, String table):返回指定表主键的结果集
*主键的结果集中包含的内容有:
每个主键列描述都有以下列:
TABLE_CAT String => 表类别(可为 null)
TABLE_SCHEM String => 表模式(可为 null)
TABLE_NAME String => 表名称
COLUMN_NAME String => 列名称
KEY_SEQ short => 主键中的序列号(值 1 表示主键中的第一列,值 2 表示主键中的第二列)。
PK_NAME String => 主键的名称(可为 null)
getTables():返回表的描述信息;
*表的描述信息中包含:
每个表描述都有以下列:
TABLE_CAT String => 表类别(可为 null)
TABLE_SCHEM String => 表模式(可为 null)
TABLE_NAME String => 表名称
TABLE_TYPE String => 表类型。典型的类型是 "TABLE"、"VIEW"、"SYSTEM TABLE"、"GLOBAL TEMPORARY"、"LOCAL TEMPORARY"、"ALIAS" 和 "SYNONYM"。
REMARKS String => 表的解释性注释
TYPE_CAT String => 类型的类别(可为 null)
TYPE_SCHEM String => 类型模式(可为 null)
TYPE_NAME String => 类型名称(可为 null)
SELF_REFERENCING_COL_NAME String => 有类型表的指定 "identifier" 列的名称(可为 null)
REF_GENERATION String => 指定在 SELF_REFERENCING_COL_NAME 中创建值的方式。这些值为 "SYSTEM"、"USER" 和 "DERIVED"。(可能为 null)
主要使用的类是:ParameterMetaData
PreparedStatement.getParameterMetaData()返回ParameterMetaData对象;
ParameterMetaData对象
getParameterCount() :获得指定参数的个数
getParameterTypeName(int param) :获得指定参数的sql类型//mysql不支持
示例代码为:
主要方法有:
getColumnCount() :返回resultset对象的列数
getColumnName(int column) :获得指定列的名称
getColumnTypeName(int column):获得指定列的类型
示例代码:
一、获取数据库元信息
主要使用的类是:DatabaseMetaData
获取数据库元信息的方法:
利用Connection对象来获取:
Connection.getMetaData()返回DataBaseMetaData对象;
DataBaseMetaData对象
getURL():返回一个String类对象,代表数据库的URL。
getUserName():返回连接当前数据库管理系统的用户名。
getDriverName():返回驱动驱动程序的名称。
getPrimaryKeys(String catalog, String schema, String table):返回指定表主键的结果集
*主键的结果集中包含的内容有:
每个主键列描述都有以下列:
TABLE_CAT String => 表类别(可为 null)
TABLE_SCHEM String => 表模式(可为 null)
TABLE_NAME String => 表名称
COLUMN_NAME String => 列名称
KEY_SEQ short => 主键中的序列号(值 1 表示主键中的第一列,值 2 表示主键中的第二列)。
PK_NAME String => 主键的名称(可为 null)
getTables():返回表的描述信息;
*表的描述信息中包含:
每个表描述都有以下列:
TABLE_CAT String => 表类别(可为 null)
TABLE_SCHEM String => 表模式(可为 null)
TABLE_NAME String => 表名称
TABLE_TYPE String => 表类型。典型的类型是 "TABLE"、"VIEW"、"SYSTEM TABLE"、"GLOBAL TEMPORARY"、"LOCAL TEMPORARY"、"ALIAS" 和 "SYNONYM"。
REMARKS String => 表的解释性注释
TYPE_CAT String => 类型的类别(可为 null)
TYPE_SCHEM String => 类型模式(可为 null)
TYPE_NAME String => 类型名称(可为 null)
SELF_REFERENCING_COL_NAME String => 有类型表的指定 "identifier" 列的名称(可为 null)
REF_GENERATION String => 指定在 SELF_REFERENCING_COL_NAME 中创建值的方式。这些值为 "SYSTEM"、"USER" 和 "DERIVED"。(可能为 null)
示例代码为:
package com.oterman.metadata;
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.ResultSet;
import java.sql.SQLException;
import com.mchange.v2.c3p0.ComboPooledDataSource;
/**
* 演示数据库元数据:
* 元数据:数据库、表、列的定义信息!
*Connection.getMetaData()
*DataBaseMetaData对象
*getURL():返回一个String类对象,代表数据库的URL。
*getUserName():返回连接当前数据库管理系统的用户名。
*getDriverName():返回驱动驱动程序的名称。
*getPrimaryKeys(String catalog, String schema, String table):返回指定表主键的结果集
*getTables():获取给定表的描述;
*
*
*/
public class DBMetaDataDemo {
public static void main(String[] args) {
Connection conn=null;
ResultSet rs=null;
//使用c3p0连接池,注意导入jar包以及建立c3p0-config.xml文件;
/**
* c3p0-config.xml:
<?xml version="1.0"?>
<c3p0-config>
<default-config>
<property name="driverClass">com.mysql.jdbc.Driver</property>
<property name="jdbcUrl">jdbc:mysql://localhost:3306/trans</property>
<property name="user">root</property>
<property name="password">root</property>
</default-config>
<named-config name="intergalactoApp">
<property name="acquireIncrement">50</property>
<property name="initialPoolSize">100</property>
</named-config>
</c3p0-config>
*
*
*/
ComboPooledDataSource pool=new ComboPooledDataSource();
try{
conn=pool.getConnection();
DatabaseMetaData metaData= conn.getMetaData();
//获取url;
String url=metaData.getURL();
System.out.println("url:"+url);
//获取用户名;
String username=metaData.getUserName();
System.out.println("username:"+username);
//获取驱动名;
String driverName=metaData.getDriverName();
System.out.println("driverName:"+driverName);
//获取主键结果集
rs=metaData.getPrimaryKeys(null, null, "account");
/**
* 每个主键列描述都有以下列:
TABLE_CAT String => 表类别(可为 null)
TABLE_SCHEM String => 表模式(可为 null)
TABLE_NAME String => 表名称
COLUMN_NAME String => 列名称
KEY_SEQ short => 主键中的序列号(值 1 表示主键中的第一列,值 2 表示主键中的第二列)。
PK_NAME String => 主键的名称(可为 null)
*
*/
while(rs.next()){
String tableName=rs.getString("TABLE_NAME");
String columnName=rs.getString("COLUMN_NAME");
int index=rs.getShort("KEY_SEQ");
System.out.println("tableName:"+tableName);
System.out.println("columnName:"+columnName);
System.out.println("index:"+index);
}
//获取表的描述;使用%通配符来获取表的全名;
//获取表名以ac开头的表;
//rs=metaData.getTables(null, null, "ac%", new String []{"TABLE"});
//获取所有的表;
rs=metaData.getTables(null, null, "%", new String []{"TABLE"});
/**
* 每个表描述都有以下列:
TABLE_CAT String => 表类别(可为 null)
TABLE_SCHEM String => 表模式(可为 null)
TABLE_NAME String => 表名称
TABLE_TYPE String => 表类型。典型的类型是 "TABLE"、"VIEW"、"SYSTEM TABLE"、"GLOBAL TEMPORARY"、"LOCAL TEMPORARY"、"ALIAS" 和 "SYNONYM"。
REMARKS String => 表的解释性注释
TYPE_CAT String => 类型的类别(可为 null)
TYPE_SCHEM String => 类型模式(可为 null)
TYPE_NAME String => 类型名称(可为 null)
SELF_REFERENCING_COL_NAME String => 有类型表的指定 "identifier" 列的名称(可为 null)
REF_GENERATION String => 指定在 SELF_REFERENCING_COL_NAME 中创建值的方式。这些值为 "SYSTEM"、"USER" 和 "DERIVED"。(可能为 null)
*
*/
while(rs.next()){
String tableName=rs.getString("TABLE_NAME");
System.out.println("tableName:"+tableName);
}
}catch(Exception e){
e.printStackTrace();
}finally{
if(conn!=null){
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}finally{
conn=null;
}
}
if(rs!=null){
try {
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}finally{
rs=null;
}
}
}
}
}
二、获取参数元信息
主要使用的类是:ParameterMetaData
PreparedStatement.getParameterMetaData()返回ParameterMetaData对象;
ParameterMetaData对象
getParameterCount() :获得指定参数的个数
getParameterTypeName(int param) :获得指定参数的sql类型//mysql不支持
示例代码为:
conn=pool.getConnection();
ps=conn.prepareStatement("select * from account where name=? and money=?");
//获取参数元数据;
ParameterMetaData pMetaData= ps.getParameterMetaData();
int count=pMetaData.getParameterCount();//sql语句中问号的个数;
System.out.println("parameter_count:"+count);
三、获取结果集元数据
主要使用对象:ResultSetMetaData,通过ResultSet.getMetaData()来获取;主要方法有:
getColumnCount() :返回resultset对象的列数
getColumnName(int column) :获得指定列的名称
getColumnTypeName(int column):获得指定列的类型
示例代码:
<span style="white-space:pre"> </span>conn=pool.getConnection();
ps=conn.prepareStatement("select * from account ");
rs=ps.executeQuery();
//获取结果集元数据;
ResultSetMetaData metaData=rs.getMetaData();
int count=metaData.getColumnCount();//获取表的列数;
for(int i=1;i<=metaData.getColumnCount();i++){
String columnName=metaData.getColumnName(i);//获取列名;
Object columnType=metaData.getColumnTypeName(i);//获取列类型;
System.out.println(columnName+":"+columnType);
}