JDBC_通过JDBC获得的元数据

1.DatabaseMetaData

作用:
(1) DatabaseMetaData实例的获取
(2) 获得当前数据库以及驱动的信息
(3) 获得当前数据库中表的信息
(4)获得某个表的列信息
(5)获得表的关键字信息
(6)获取指定表的外键信息
(7)反向设计表
DatabaseMetaData是描述数据库的元数据对象
由Connection 得到
可查询到关于数据库的相关信息,例如
数据库版本号
数据库连接用户名
得到连接的oracle中有哪些数据库等等
首先定义三个变量
Connection connection = null;
ResultSet resultset = null;
DatabaseMetaData data = null;
连接数据库
connection = JDBCTools.getConnection();
JDBCTools类中定义了连接数据库以及关闭数据库的方法
实例化对象
data = connection.getMetaData();
得到数据库版本号
int version = data.getDatabaseMajorVersion();
System.out.println(version);
得到连接数据库的用户名
String user = data.getUserName();
System.out.println(user);
得到oracle中有哪些数据库
注意这里得到的是结果集,所以需要循环输出
resultset = data.getCatalogs();
while(resultset.next()){
System.out.println(resultset.getString(1));
}

testDatabaseMetaData()方法如下
public void testDatabaseMetaData() throws SQLException{
        /**
     * @author chance
     * DatabaseMetaData是描述数据库的元数据对象
     * 由Connection 得到
     * @throws SQLException 
     */
        Connection connection = null;
        ResultSet resultset = null;
        DatabaseMetaData data = null;

        try {
            connection = JDBCTools.getConnection();
            data = connection.getMetaData();

            //可以得到oracle数据库的相关信息
            //得到数据库版本号
            int version = data.getDatabaseMajorVersion();
            System.out.println(version);

            //得到连接数据库的用户名
            String user = data.getUserName();
            System.out.println(user);

            //得到oracle中有哪些数据库
            resultset = data.getCatalogs();
            while(resultset.next()){
                System.out.println(resultset.getString(1));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }finally{
            JDBCTools.release(resultset, null, connection);
        }
    }

2.ResultSetMetaData

方法摘要

String getCatalogName(int column) 获取指定列的表目录名称。
String getColumnClassName(int column) 如果调用方法 ResultSet.getObject 从列中检索值,则返回构造其实例的 Java 类的完全限定名称。
int getColumnCount() 返回此 ResultSet 对象中的列数。
int getColumnDisplaySize(int column) 指示指定列的最大标准宽度,以字符为单位。
String getColumnLabel(int column) 获取用于打印输出和显示的指定列的建议标题。
String getColumnName(int column) 获取指定列的名称。
int getColumnType(int column) 检索指定列的 SQL 类型。
String getColumnTypeName(int column) 检索指定列的数据库特定的类型名称。
int getPrecision(int column) 获取指定列的小数位数。
int getScale(int column) 获取指定列的小数点右边的位数。
String getSchemaName(int column) 获取指定列的表模式。
String getTableName(int column) 获取指定列的名称。
boolean isAutoIncrement(int column) 指示是否自动为指定列进行编号,这样这些列仍然是只读的。
boolean isCaseSensitive(int column) 指示列的大小写是否有关系。
boolean isCurrency(int column) 指示指定的列是否是一个哈希代码值。
boolean isDefinitelyWritable(int column) 指示在指定的列上进行写操作是否明确可以获得成功。
int isNullable(int column) 指示指定列中的值是否可以为 null。
boolean isReadOnly(int column) 指示指定的列是否明确不可写入。
boolean isSearchable(int column) 指示是否可以在 where 子句中使用指定的列。
boolean isSigned(int column) 指示指定列中的值是否带正负号。
boolean isWritable(int column) 指示在指定的列上进行写操作是否可以获得成功。
resultSetMetaData:描述结果集的元数据


实例代码步骤:
定义三个变量
Connection connection = null;
PreparedStatement preparedStatement = null;
ResultSet resultset = null;
连接并创建sql语句
connection = JDBCTools.getConnection();
String sql = “select * from student”;
preparedStatement = connection.prepareStatement(sql);
resultset = preparedStatement.executeQuery();
1.得到ResultSetMetaData 对象
ResultSetMetaData rsmd = resultset.getMetaData();
2.得到列的个数

int colunmcount = rsmd.getColumnCount();

            for(int i = 0;i < colunmcount;i++){
                //3.得到列名
                String colunmName = rsmd.getColumnName(i+1);
                //4.得到列的别名
                String colunmLabel = rsmd.getColumnLabel(i+1);

                System.out.println(colunmName + "," + colunmLabel);
            }

其他方法未一一测试

testResultSetMetaData()方法代码如下

    public void testResultSetMetaData() throws SQLException{
        /**
         * resultSetMetaData:描述结果集的元数据
         * 可以得到结果集中的基本信息:结果集中有哪些列,列名,列的别名等
         */
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        ResultSet resultset = null;

        try {
            connection = JDBCTools.getConnection();
            String sql = "select * from student";
            preparedStatement = connection.prepareStatement(sql);
            resultset = preparedStatement.executeQuery();

            //1.得到ResultSetMetaData 对象
            ResultSetMetaData rsmd = resultset.getMetaData();

            //2.得到列的个数
            int colunmcount = rsmd.getColumnCount();

            for(int i = 0;i < colunmcount;i++){
                //3.得到列名
                String colunmName = rsmd.getColumnName(i+1);
                //4.得到列的别名
                String colunmLabel = rsmd.getColumnLabel(i+1);

                System.out.println(colunmName + "," + colunmLabel);
            }   
        } catch (Exception e) {
            e.printStackTrace();
        }finally{
            JDBCTools.release(resultset, preparedStatement, connection);
        }
    }

JDBCTools.java

package com.atchance.jdbc;

import java.sql.Connection;
import java.sql.Driver;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;
/**
 * JDBCTools类中定义了:
 * 1,数据库连接方法
 * 2.数据库进行增删改的方法
 * 3.数据库相关连接的释放
 * 1)重载了release函数 :传两个参数 以及传三个函数
 * @author 吴承潜
 *
 */
public class JDBCTools {

    public static void release(ResultSet rs,Statement statement, Connection con) throws SQLException{
        /**
         * 释放ResultSet Statement Connection三个连接的方法
         */
        try {
            if(rs != null)
                rs.close();
        } catch (Exception e1) {
            e1.printStackTrace();
        }
        try {
            if(statement != null)
                statement.close();
        } catch (Exception e) {
            e.printStackTrace();
        }finally{
            if(con != null)
                con.close();
        }
    }

    public static void release(Statement statement, Connection con) throws SQLException{
        /**
         * Statement Connection两个连接的方法
         */
        try {
            if(statement != null)
                statement.close();
        } catch (Exception e) {
            e.printStackTrace();
        }finally{
            if(con != null)
                con.close();
        }
    }
    public static Connection getConnection() throws Exception{
            /*
             * 创建数据库的连接
             * */
            String driverClass = "oracle.jdbc.driver.OracleDriver";
            String jdbcUrl = "jdbc:oracle:thin:@localhost:1521:ORCL";
            String user = "scott";
            String password = "tiger";

            Driver driver = (Driver)Class.forName(driverClass).newInstance();

            Properties info = new Properties();
            info.put("user", user);
            info.put("password", password);
            Connection connection = driver.connect(jdbcUrl,info);
            return connection;
        }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值