美文网首页
2018-02-27 获得数据库元数据(meteData)

2018-02-27 获得数据库元数据(meteData)

作者: 紫杉叶子 | 来源:发表于2018-02-27 20:50 被阅读0次

笔记如下

  • 获得结果集元数据
//获得结果集元数据
    @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

    }

相关文章

网友评论

      本文标题:2018-02-27 获得数据库元数据(meteData)

      本文链接:https://www.haomeiwen.com/subject/tbomxftx.html