获取数据库中的元数据
元数据(MetaData),是指定义数据结构的数据。那么数据库元数据就是指定义数据库各类对象结构的数据。 例
如数据库中的数据库名,表明, 列名、用户名、版本名以及从SQL语句得到的结果中的大部分字符串是元数据
使用JDBC来处理数据库的接口主要有三个,即Connection,PreparedStatement和ResultSet这三个,
而对于这三个接口,还可以获取不同类型的元数据,通过这些元数据类获得一些数据库的信息。
依赖
<dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.6</version> </dependency>
获取数据库中的元数据信息
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
package com.zhao.metadata; import org.junit.Before; import org.junit.Test; import java.sql.*; import java.util.Properties; /** * 数据库 元数据 练习 */ public class metaDataTest { private Connection connection; @Before public void init() throws Exception { String driver ="com.mysql.jdbc.Driver"; String url="jdbc:mysql://127.0.0.1:3306/?useUnicode=true&characterEncoding=utf8"; String username="root"; String password="123456"; Properties props = new Properties(); props.put("remarksReporting","true");//获取数据库的备注信息 props.put("user",username); props.put("password",password); //1.获取连接 Class.forName(driver);//注册驱动 connection = DriverManager.getConnection(url,props); } //获取数据库基本信息 @Test public void test01() throws Exception { //2.获取元数据 DatabaseMetaData metaData = connection.getMetaData(); //3.获取数据库基本信息 System.out.println(metaData.getUserName()); System.out.println(metaData.supportsTransactions());//是否支持事务 System.out.println(metaData.getDatabaseProductName()); } //获取数据库列表 @Test public void test02() throws SQLException { //1.获取元数据 DatabaseMetaData metaData = connection.getMetaData(); //2.获取所有数据库列表 ResultSet rs = metaData.getCatalogs(); while (rs.next()) { System.out.println(rs.getString(1)); } rs.close(); connection.close(); } //获取指定数据库中的表信息 @Test public void test03() throws Exception { String driver ="com.mysql.jdbc.Driver"; String url="jdbc:mysql://127.0.0.1:3306/saas?useUnicode=true&characterEncoding=utf8"; String username="root"; String password="123456"; Properties props = new Properties(); props.put("remarksReporting","true");//获取数据库的备注信息 props.put("user",username); props.put("password",password); //1.获取连接 Class.forName(driver);//注册驱动 connection = DriverManager.getConnection(url,props); //1.获取元数据 DatabaseMetaData metaData = connection.getMetaData(); //2.获取数据库中表信息 /** * String catalog, String schemaPattern,String tableNamePattern, String types[] * * catalog:当前操作的数据库 * mysql: * :ihrm * catalog * oralce: * xxx:1521:orcl * catalog * schema: * mysql: * :null * oracle: * :用户名(大写) * * tableNamePattern: * null:查询所有表 * 为空:查询目标表 * * types:类型 * TABLE:表 * VIEW:视图 * */ ResultSet rs = metaData.getTables(null, null, null, new String[]{"TABLE"}); while (rs.next()) { System.out.println(rs.getString("TABLE_NAME")); } } //获取指定表中的字段信息 @Test public void test04() throws Exception { String driver ="com.mysql.jdbc.Driver"; String url="jdbc:mysql://127.0.0.1:3306/saas?useUnicode=true&characterEncoding=utf8"; String username="root"; String password="123456"; Properties props = new Properties(); props.put("remarksReporting","true");//获取数据库的备注信息 props.put("user",username); props.put("password",password); //1.获取连接 Class.forName(driver);//注册驱动 connection = DriverManager.getConnection(url,props); DatabaseMetaData metaData = connection.getMetaData(); //获取表中的字段信息 /** * catalog * schema * tableName * columnName */ ResultSet rs = metaData.getColumns(null, null, "pe_user", null); while (rs.next()) { System.out.println(rs.getString(4)); //因为他 fields 中第四个的值是 COLUMN_NAME 所以写COLUMN_NAME 和4 都可以获取到 //System.out.println(rs.getString("COLUMN_NAME")); } } }
获取参数元数据
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
package com.zhao.metadata; import org.junit.Before; import org.junit.Test; import java.sql.*; import java.util.Properties; /** * 测试参数元数据(ParameterMetaData) * 通过PreparedStatement获取 * 获取sql参数中的属性信息 */ public class ParameterMetaDataTest { private Connection connection; @Before public void init() throws Exception { String driver ="com.mysql.jdbc.Driver"; String url="jdbc:mysql://127.0.0.1:3306/saas?useUnicode=true&characterEncoding=utf8"; String username="root"; String password="123456"; Properties props = new Properties(); props.put("remarksReporting","true");//获取数据库的备注信息 props.put("user",username); props.put("password",password); //1.获取连接 Class.forName(driver);//注册驱动 connection = DriverManager.getConnection(url,props); } //sql = SELECT * FROM `pe_user` WHERE id=1 @Test public void test() throws Exception { // String sql = "select ? from pe_user where id=?"; String sql = "select * from pe_user where id=?"; PreparedStatement pstmt = connection.prepareStatement(sql); /** * parameterIndex 第几个参数 * x :参数值 */ pstmt.setString(1, "2"); // pstmt.setString(1, "id"); // pstmt.setString(2, "2"); //获取参数元数据 ParameterMetaData metaData = pstmt.getParameterMetaData(); //一共有几个参数 int count = metaData.getParameterCount(); System.out.println(count); } }
获取结果集元数据
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
package com.zhao.metadata; import org.junit.Before; import org.junit.Test; import java.sql.*; import java.util.Properties; /** * 测试结果集元数据(ResultSetMetaData) * 通过ResultSet获取 * 获取查询结果的信息 */ public class ResultSetMetaDataTest { private Connection connection; @Before public void init() throws Exception { String driver ="com.mysql.jdbc.Driver"; String url="jdbc:mysql://127.0.0.1:3306/saas?useUnicode=true&characterEncoding=utf8"; String username="root"; String password="123456"; Properties props = new Properties(); props.put("remarksReporting","true");//获取数据库的备注信息 props.put("user",username); props.put("password",password); //1.获取连接 Class.forName(driver);//注册驱动 connection = DriverManager.getConnection(url,props); } @Test public void test() throws Exception { //sql 注:pe_user这个是我的数据库表名 String sql = "select * from pe_user where id=?"; //PreparedStatement PreparedStatement pstmt = connection.prepareStatement(sql); pstmt.setString(1, "2"); //查询 ResultSet rs = pstmt.executeQuery(); //获取结果集元数据 ResultSetMetaData metaData = rs.getMetaData(); //获取查询字段个数 int count = metaData.getColumnCount(); for(int i =1;i<=count ;i++) { //获取列名 String columnName = metaData.getColumnName(i); //获取字段类型 sql类型 String columnType = metaData.getColumnTypeName(i); //获取java类型 String columnClassName = metaData.getColumnClassName(i); System.out.println(columnName+"--"+columnType+"---"+columnClassName); } } }
代码地址:https://gitee.com/richOne/freemarkerAndMetaData/tree/master