一:Connection的MetaData操作
@Test
public void testDataBaseMeta() throws Exception{
Connection conn = JdbcUtils.getConnection();
// 获得数据库的元数据
DatabaseMetaData dbmd = conn.getMetaData();
System.out.println(dbmd.getURL());
System.out.println(dbmd.getUserName());
System.out.println(dbmd.getDatabaseMajorVersion());
System.out.println(dbmd.getDatabaseProductName());
System.out.println(dbmd.getDriverName());
System.out.println(dbmd.getDriverVersion());
}
二:Statement的ParameterMetaData操作
@Test
public void testParameterDataBaseMeta() throws Exception{
Connection conn = JdbcUtils.getConnection();
// PreparedStatement stmt = conn.prepareStatement("insert into account values(null,?,?,?,?,?)");
PreparedStatement stmt = conn.prepareStatement("??????????????????????????????????");
// 获得 sql语句的 元数据
ParameterMetaData pmd = stmt.getParameterMetaData();
int count = pmd.getParameterCount(); // 获得 sql语句中 的占位符的 个数
System.out.println(count);
}
三:ResultSet的MetaData操作
@Test
public void testResultSetMeataData() throws Exception{
Connection conn = JdbcUtils.getConnection();
// PreparedStatement stmt = conn.prepareStatement("insert into account values(null,?,?,?,?,?)");
PreparedStatement stmt = conn.prepareStatement("select * from account");
ResultSet rs = stmt.executeQuery();
// 获得 结果集 元数据
ResultSetMetaData rsmd = rs.getMetaData();
int count = rsmd.getColumnCount();
for (int i = 0; i < count; i++) {
String columnName = rsmd.getColumnName(i+1);
int columnType = rsmd.getColumnType(i+1);
String columnTypeName = rsmd.getColumnTypeName(i+1);
System.out.println("columnName : "+ columnName +", columnType : " +columnType +",columnTypeName : " +columnTypeName);
}
}