笔记如下
//获得结果集元数据
@Test
public void testResultSetMetaData() throws Exception {
Connection conn = JdbcUtils.getConnection();
PreparedStatement stmt = conn.prepareStatement("select * from account");
ResultSet rs = stmt.executeQuery();
//获得查询结果集元数据.
ResultSetMetaData rsmd = rs.getMetaData();
//列的数量
int columnCount = rsmd.getColumnCount();
for (int i = 0; i < columnCount; 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);
}
}
@Test
public void testParameterMetaData() throws Exception {
Connection conn = JdbcUtils.getConnection();
PreparedStatement stmt = conn.prepareStatement("insert into account values(?,?,?,?,?,?,?,?,?,?)");
ParameterMetaData pmd = stmt.getParameterMetaData();
//用于获得占位符的个数(非常有用)
int count = pmd.getParameterCount();
//替换占位符
for (int i = 0; i < count; i++) {
//stmt.setObject(i, x);
}
}
//获得数据库元数据
@Test
public void testDataBaseMetaData() throws Exception {
Connection conn = JdbcUtils.getConnection();
//获得数据库元数据
DatabaseMetaData metaData = conn.getMetaData();
String url = metaData.getURL();//jdbc:mysql:///transaction
String userName = metaData.getUserName();//root@localhost
String databaseProductName = metaData.getDatabaseProductName();//MySQL
String databaseProductVersion = metaData.getDatabaseProductVersion();//5.7.17-log
String driverName = metaData.getDriverName();//MySQL Connector Java
String driverVersion = metaData.getDriverVersion();//mysql-connector-java-5.1.45 ( Revision: 9131eefa398531c7dc98776e8a3fe839e544c5b2 )
boolean readOnly = metaData.isReadOnly();//false
}
网友评论