JDBC的元数据
可以由Connection得到
@Test public void test1(){ Connection conn = null; ResultSet resultSet = null; try { conn = Methods.getConnection(); DatabaseMetaData databaseMetaData = conn.getMetaData(); //得到数据库的基本信息 //1.得到数据库的版本号 int version = databaseMetaData.getDatabaseMajorVersion(); System.out.println(version); //2.得到数据库的用户名 String user = databaseMetaData.getUserName(); System.out.println(user); //3.MysqL有哪些数据库 resultSet = databaseMetaData.getCatalogs(); while(resultSet.next()){ System.out.println(resultSet.getString(1)); } } catch (Exception e) { e.printstacktrace(); } finally { Methods.release(null,conn,resultSet); }
ResultSetMetaData是描述结果集的元数据。
可以得到结果集中的基本信息:结果集中有哪些列,列名,列的别名等。
@Test public void test2(){ Connection conn = null; PreparedStatement preparedStatement = null; ResultSet resultSet = null; String sql = "select * from student"; try { preparedStatement = conn.prepareStatement(sql); resultSet = preparedStatement.executeQuery(); //1.得到ResultSetMetaData对象 ResultSetMetaData rsmd = resultSet.getMetaData(); //2.得到列的个数 int columnCount = rsmd.getColumnCount(); for (int i = 0; i < columnCount; i++) { //3.得到列名 String columnName = rsmd.getColumnName(i+1); System.out.println(columnName); String columnLabel = rsmd.getColumnLabel(i+1); System.out.println(columnLabel); } } catch (sqlException e) { e.printstacktrace(); } finally { } }