DatabaseMetaData
数据库的描述对象;了解即可
/**
* DatabaseMetaData元数据;
*/
public void test(){
try {
Connection connection = JDBCTools.connectDb();
DatabaseMetaData metaData = connection.getMetaData();
int databaseMajorVersion = metaData.getDatabaseMajorVersion();//数据库的版本号
String userName = metaData.getUserName();//连接数据库的用户名;
System.out.println(databaseMajorVersion+"---"+userName);
} catch (Exception e) {
e.printStackTrace();
}
}
ResultSetMetaData
/**
* ResultSetMetaData 元数据;
*/
public void test2(){
try {
Connection connection = JDBCTools.connectDb();
String sql="select * from CLASSES";
PreparedStatement preparedStatement = connection.prepareStatement(sql);
ResultSet resultSet = preparedStatement.executeQuery();
ResultSetMetaData metaData = resultSet.getMetaData();
int columnCount = metaData.getColumnCount();//得到列的个数
for(int i=0;i<columnCount;i++){
String columnName = metaData.getColumnName(i + 1);//得到列名
String columnLabel = metaData.getColumnLabel(i + 1);//列的别名;
}
} catch (Exception e) {
e.printStackTrace();
}
}