Java 通过JDBC查询数据库表结构(字段名称,类型,长度等)

时间:2023-02-20 15:09:33

Java 通过JDBC查询数据库表结构(字段名称,类型,长度等)

发布者:唛唛家的豆子
 

时间:2012-11-20 17:54:02

 

Java 通过JDBC查询数据库表结构(字段名称,类型,长度等)

在JDBCPreparedStatement.executeQuery().getMetaData();后,我们可以通过ResultSetMetaData对象查询返回结果集的源数据信息,也就是表结构信息。

示例代码如下:

package astar.sutil.db;  import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSetMetaData; import java.sql.SQLException; import java.util.ArrayList; import java.util.List;  /**  * @author 鲁炬 * */ public class DbMetaDataUtilTest {  public static void main(String[] args) throws SQLException, ClassNotFoundException { String user = "user"; String password = "pwd"; String jdbcDriver = "com.ibm.db2.jcc.DB2Driver"; String jdbcUrl = "jdbc:db2://localhost:50000/DBNAME"; Connection conn = null; Class.forName(jdbcDriver); conn = DriverManager.getConnection(jdbcUrl, user, password);  PreparedStatement pst = null; try { pst = conn.prepareStatement("select * from t_table where 1=2"); ResultSetMetaData rsd = pst.executeQuery().getMetaData(); for(int i = 0; i < rsd.getColumnCount(); i++) { System.out.print("java类型:"+rsd.getColumnClassName(i + 1)); System.out.print(" 数据库类型:"+rsd.getColumnTypeName(i + 1)); System.out.print(" 字段名称:"+rsd.getColumnName(i + 1)); System.out.print(" 字段长度:"+rsd.getColumnDisplaySize(i + 1)); System.out.println(); } } catch(SQLException e) { throw new RuntimeException(e); } finally { try { pst.close(); pst = null; } catch(SQLException e) { throw new RuntimeException(e); } }  }  } 

以上代码运行打印如下:

java类型:java.lang.String 数据库类型:VARCHAR 字段名称:CORP_CODE 字段名称:20 java类型:java.lang.String 数据库类型:VARCHAR 字段名称:CORP_NAME 字段名称:100 java类型:java.lang.String 数据库类型:VARCHAR 字段名称:SIMPLY_NAME 字段名称:50 java类型:java.lang.String 数据库类型:CHAR 字段名称:CORP_LEVEL 字段名称:1 java类型:java.lang.String 数据库类型:VARCHAR 字段名称:SUP_CORP_CODE 字段名称:20 java类型:java.lang.String 数据库类型:VARCHAR 字段名称:PROVINCE 字段名称:8 java类型:java.lang.String 数据库类型:VARCHAR 字段名称:STAT_CODE 字段名称:8 java类型:java.lang.String 数据库类型:CHAR 字段名称:CORP_KIND 字段名称:4 java类型:java.lang.String 数据库类型:CHAR 字段名称:IS_LEAF 字段名称:1 java类型:java.lang.String 数据库类型:CHAR 字段名称:IS_USE 字段名称:1 java类型:java.lang.String 数据库类型:CHAR 字段名称:IS_UPDATE 字段名称:1 java类型:java.lang.Integer 数据库类型:SMALLINT 字段名称:ORDER_CODE 字段名称:6 java类型:java.lang.String 数据库类型:VARCHAR 字段名称:MAP_REGIE_CODE 字段名称:20 java类型:java.lang.String 数据库类型:CHAR 字段名称:IS_SUPER 字段名称:1 java类型:java.lang.Integer 数据库类型:SMALLINT 字段名称:DISPLAY_ID 字段名称:6 java类型:java.lang.String 数据库类型:CHAR 字段名称:IS_GIS 字段名称:1 

更多详细的属性输出可以读一下ResultSetMetaData对象的源码。

/* * @(#)ResultSetMetaData.java	1.33 05/12/01 * * Copyright 2006 Sun Microsystems, Inc. All rights reserved. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms. */  package java.sql;  /** * An object that can be used to get information about the types  * and properties of the columns in a <code>ResultSet</code> object. * The following code fragment creates the <code>ResultSet</code> object rs, * creates the <code>ResultSetMetaData</code> object rsmd, and uses rsmd * to find out how many columns rs has and whether the first column in rs * can be used in a <code>WHERE</code> clause. * <PRE> * * ResultSet rs = stmt.executeQuery("SELECT a, b, c FROM TABLE2"); * ResultSetMetaData rsmd = rs.getMetaData(); * int numberOfColumns = rsmd.getColumnCount(); * boolean b = rsmd.isSearchable(1); * * </PRE> */  public interface ResultSetMetaData extends Wrapper {  /** * Returns the number of columns in this <code>ResultSet</code> object. * * @return the number of columns * @exception SQLException if a database access error occurs */ int getColumnCount() throws SQLException;  /** * Indicates whether the designated column is automatically numbered. * * @param column the first column is 1, the second is 2, ... * @return <code>true</code> if so; <code>false</code> otherwise * @exception SQLException if a database access error occurs */ boolean isAutoIncrement(int column) throws SQLException;  /** * Indicates whether a column's case matters. * * @param column the first column is 1, the second is 2, ... * @return <code>true</code> if so; <code>false</code> otherwise * @exception SQLException if a database access error occurs */ boolean isCaseSensitive(int column) throws SQLException;	  /** * Indicates whether the designated column can be used in a where clause. * * @param column the first column is 1, the second is 2, ... * @return <code>true</code> if so; <code>false</code> otherwise * @exception SQLException if a database access error occurs */ boolean isSearchable(int column) throws SQLException;  /** * Indicates whether the designated column is a cash value. * * @param column the first column is 1, the second is 2, ... * @return <code>true</code> if so; <code>false</code> otherwise * @exception SQLException if a database access error occurs */ boolean isCurrency(int column) throws SQLException;  /** * Indicates the nullability of values in the designated column.	 * * @param column the first column is 1, the second is 2, ... * @return the nullability status of the given column; one of <code>columnNoNulls</code>, * <code>columnNullable</code> or <code>columnNullableUnknown</code> * @exception SQLException if a database access error occurs */ int isNullable(int column) throws SQLException;  /** * The constant indicating that a * column does not allow <code>NULL</code> values. */ int columnNoNulls = 0;  /** * The constant indicating that a * column allows <code>NULL</code> values. */ int columnNullable = 1;  /** * The constant indicating that the * nullability of a column's values is unknown. */ int columnNullableUnknown = 2;  /** * Indicates whether values in the designated column are signed numbers. * * @param column the first column is 1, the second is 2, ... * @return <code>true</code> if so; <code>false</code> otherwise * @exception SQLException if a database access error occurs */ boolean isSigned(int column) throws SQLException;  /** * Indicates the designated column's normal maximum width in characters. * * @param column the first column is 1, the second is 2, ... * @return the normal maximum number of characters allowed as the width * of the designated column * @exception SQLException if a database access error occurs */ int getColumnDisplaySize(int column) throws SQLException;  /** * Gets the designated column's suggested title for use in printouts and * displays. The suggested title is usually specified by the SQL <code>AS</code>  * clause. If a SQL <code>AS</code> is not specified, the value returned from  * <code>getColumnLabel</code> will be the same as the value returned by the  * <code>getColumnName</code> method. * * @param column the first column is 1, the second is 2, ... * @return the suggested column title * @exception SQLException if a database access error occurs */ String getColumnLabel(int column) throws SQLException;	  /** * Get the designated column's name. * * @param column the first column is 1, the second is 2, ... * @return column name * @exception SQLException if a database access error occurs */ String getColumnName(int column) throws SQLException;  /** * Get the designated column's table's schema. * * @param column the first column is 1, the second is 2, ... * @return schema name or "" if not applicable * @exception SQLException if a database access error occurs */ String getSchemaName(int column) throws SQLException;  /** * Get the designated column's specified column size.  * For numeric data, this is the maximum precision. For character data, this is the length in characters.  * For datetime datatypes, this is the length in characters of the String representation (assuming the  * maximum allowed precision of the fractional seconds component). For binary data, this is the length in bytes. For the ROWID datatype,  * this is the length in bytes. 0 is returned for data types where the * column size is not applicable. * * @param column the first column is 1, the second is 2, ... * @return precision * @exception SQLException if a database access error occurs */ int getPrecision(int column) throws SQLException;  /** * Gets the designated column's number of digits to right of the decimal point. * 0 is returned for data types where the scale is not applicable. * * @param column the first column is 1, the second is 2, ... * @return scale * @exception SQLException if a database access error occurs */ int getScale(int column) throws SQLException;	  /** * Gets the designated column's table name.  * * @param column the first column is 1, the second is 2, ... * @return table name or "" if not applicable * @exception SQLException if a database access error occurs */ String getTableName(int column) throws SQLException;  /** * Gets the designated column's table's catalog name. * * @param column the first column is 1, the second is 2, ... * @return the name of the catalog for the table in which the given column * appears or "" if not applicable * @exception SQLException if a database access error occurs */ String getCatalogName(int column) throws SQLException;  /** * Retrieves the designated column's SQL type. * * @param column the first column is 1, the second is 2, ... * @return SQL type from java.sql.Types * @exception SQLException if a database access error occurs * @see Types */ int getColumnType(int column) throws SQLException;  /** * Retrieves the designated column's database-specific type name. * * @param column the first column is 1, the second is 2, ... * @return type name used by the database. If the column type is * a user-defined type, then a fully-qualified type name is returned. * @exception SQLException if a database access error occurs */ String getColumnTypeName(int column) throws SQLException;  /** * Indicates whether the designated column is definitely not writable. * * @param column the first column is 1, the second is 2, ... * @return <code>true</code> if so; <code>false</code> otherwise * @exception SQLException if a database access error occurs */ boolean isReadOnly(int column) throws SQLException;  /** * Indicates whether it is possible for a write on the designated column to succeed. * * @param column the first column is 1, the second is 2, ... * @return <code>true</code> if so; <code>false</code> otherwise * @exception SQLException if a database access error occurs */ boolean isWritable(int column) throws SQLException;  /** * Indicates whether a write on the designated column will definitely succeed.	 * * @param column the first column is 1, the second is 2, ... * @return <code>true</code> if so; <code>false</code> otherwise * @exception SQLException if a database access error occurs */ boolean isDefinitelyWritable(int column) throws SQLException;  //--------------------------JDBC 2.0-----------------------------------  /** * <p>Returns the fully-qualified name of the Java class whose instances  * are manufactured if the method <code>ResultSet.getObject</code> * is called to retrieve a value  * from the column. <code>ResultSet.getObject</code> may return a subclass of the * class returned by this method. * * @param column the first column is 1, the second is 2, ... * @return the fully-qualified name of the class in the Java programming * language that would be used by the method  * <code>ResultSet.getObject</code> to retrieve the value in the specified * column. This is the class name used for custom mapping. * @exception SQLException if a database access error occurs * @since 1.2 */ String getColumnClassName(int column) throws SQLException; } 
 
 

Java 通过JDBC查询数据库表结构(字段名称,类型,长度等)的更多相关文章

  1. 通过jdbc取得数据库表结构信息

    做制作开发平台时,首要的一点是如何取得数据库表结构信息.一般通用的做法就是通过JDBC中的ResultSetMetaData类来进行操作,当你取得了数据库表结构信息后,比如说表的每个字段名称,字段类型 ...

  2. Oracle查询数据表结构&lpar;字段&comma;类型&comma;大小&comma;备注&rpar;

    作用:想要生成整个Oracle数据库所有表结构WORD文档(数据库设计说明书) Oracle数据库字典介绍    Oracle数据字典是有表和视图组成的,存储有关数据库结构信息的一些数据库对象.数据库 ...

  3. mssqlserver 查询数据库表结构语句

    查询指定表结构的表名.列名.类型.说明.字段长度 select o.name as tableName,c.name as columnName,t.name as columnType,p.valu ...

  4. MySQL数据库中查询数据库表、字段总数量,查询数据总量

    最近要查询一些数据库的基本情况,由于以前用oracle数据库比较多,现在换了MySQL数据库,就整理了一部分语句记录下来. 1.查询数据库表数量 #查询MySQL服务中数据库表数据量 SELECT C ...

  5. sql语句查询数据库表结构信息

    开发中经常用到查询指定表及其字段的信息,以下是我整理的SQL语句查询方法,供自己平时使用也提供给大家参考! 1.适用MS SQL SERVER: SELECT 表名 = then d.name els ...

  6. Mysql查询数据库表结构以及字段类型并展示

    1.建表语句sys_user CREATE TABLE `sys_user` ( `id` varchar(32) NOT NULL COMMENT '编号', `dept_id` varchar(3 ...

  7. 获取mssqlserver数据库表的字段名称,字段说明,数据类型,主键等表的信息

    sql脚本: SELECT TableName then d.name else '' end,---表名 TableShowsThat then isnull(f.value,'') else '' ...

  8. Oracle 查询对应表所有字段名称&comma;可排除不想要的字段

    由于Oracle varchar类型长度限制为4000 ,以下方法仅支持查询字段拼接后长度小于等于4000 第一种方法 select wm_concat(column_name) from user_ ...

  9. mysql 查询数据库表结构

    1. mysql> describe tmp_log; +----------+------------------+------+-----+---------+--------------- ...

随机推荐

  1. &lbrack;nRF51822&rsqb; 1、一个简单的nRF51822驱动的天马4线SPI-1&period;77寸LCD彩屏DEMO

    最近用nRF51822写了个天马4线SPI的1.77寸LCD彩屏驱动,效果如下: 屏幕的规格资料为:http://pan.baidu.com/s/1gdfkr5L 屏幕的驱动资料为:http://pa ...

  2. Generator 函数学习笔记

    // 使用 function* 定义一个 generator 函数 function* helloWorldGenerator() { yield 'hello'; // yield 关键字作为暂停的 ...

  3. C&plus;&plus;重要知识点小结---2

    C++重要知识点小结--1 :http://www.cnblogs.com/heyonggang/p/3246631.html 1.C++允许程序员声明一个不能有实例对象的类,这样的类惟一的用途是被继 ...

  4. mybatis 处理数组类型及使用Json格式保存数据 JsonTypeHandler and ArrayTypeHandler

    mybatis 比 ibatis 改进了很多,特别是支持了注解,支持了plugin inteceptor,也给开发者带来了更多的灵活性,相比其他ORM,我还是挺喜欢mybatis的. 闲言碎语不要讲, ...

  5. Qt入门(4)——Qt常见控件

    Qt提供了大量的内建控件及通用对话框可满足程序员的绝大部分要求.我们将对这些控件和对话框作一个大概的介绍. 1. QLabel 定义 QLabel* m_labelOrdered = newQLabe ...

  6. 【转】ubuntu14&period;04 trusty的源

    原文网址:http://blog.chinaunix.net/uid-15041-id-4821715.html 一.编辑更新源文件:/etc/apt/sources.list二.更新源索引文件:ap ...

  7. C&plus;&plus; 多源码文件简单组织

    C++ 多源码文件简单组织 基本上和C的是一样的,只不过C++的方法要在类中声明.看一个简单实例.ainimal.h  类里面对外公开的信息. 点击(此处)折叠或打开 #ifndef _ANIMAL_ ...

  8. ps通道抠章

    1. 打开图片 2. 使用椭圆形选框工具,选择章所在范围(ALT+SHITF+鼠标左键) 3.复制图层(CTRL+J)为图层1,隐藏背景 4.进入通道,选择对比度最大的通道,复制通道副本 5.反选(C ...

  9. RESTful 架构基础

    源自:https://mp.weixin.qq.com/s/wEr2jAVphzB1G_MISlLU0w REST(Representational State Transfer)架构风格是一种世界观 ...

  10. python接口自动化(二十七)--html 测试报告——上(详解)

    简介 上一篇我们批量执行完用例后,生成的测试报告是文本形式的,不够直观,而且报告一般都是发给leader的,所以最好是直观一目了然,为了更好的展示测试报告,最好是生成 HTML 格式的.unittes ...