Java Queries database table structure (field name, type, length, etc.) through JDBC

Source: Internet
Author: User
Tags db2

Java Queries database table structure (field name, type, length, etc.) through JDBCpublished by:The bean of the Mark's hometime:2012-11-20 17:54:02

Java Queries database table structure (field name, type, length, etc.) through JDBC

After Jdbcpreparedstatement.executequery (). GetMetaData (), we can return the source data information of the result set through the ResultSetMetaData object query, which is the table structure information.

The sample code is as follows:

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 Lu Yu * * */public class Dbmetadatautiltest {public static void main (string[] args) throws SQLException, Clas snotfoundexception {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 Type:" +rsd.getcolumnclassname (i + 1)); System.out.print ("Database type:" +rsd.getcolumntypename (i + 1)); System.out.print ("Field name:" +rsd.geTcolumnname (i + 1)); System.out.print ("Field length:" +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);}}  }  }



The above code runs print as follows:

java type: java.lang.String database type: VARCHAR field name: Corp_code Field Name: 20 Java type: java.lang.String database type: varchar field name: Corp_name Field Name: + Java Type: java.lang.String database type: varchar field name: Simply_name Field Name: Java Type: java.lang.String database type: CHAR field name: Corp_level Field Name: 1 Java Type: java.lang.String database type: VARCHAR field name: Sup_corp _code field Name: Java Type: java.lang.String database type: varchar field name: Province Field Name: 8 Java Type: java.lang.String database type: VARCHAR field name: Stat_code Field Name: 8 Java Type: java.lang.String database type: Char field name: Corp_kind Field Name: 4 Java Type: java.lang.String database type: CHAR field name: Is_ LEAF Field Name: 1 Java Type: java.lang.String database type: Char field name: Is_use Field Name: 1 Java Type: java.lang.String database type: CHAR field name: Is_update Field Name: 1 Java Type: Java.lang.Integer database type: SMALLINT field name: Order_code Field Name: 6 Java Type: java.lang.String database type: VARCHAR field name: MAP_ Regie_code field Name: Java Type: java.lang.String database type: CHAR field name: Is_super Field Name: 1 Java Type: Java.lang.Integer database type: SMALLINT Field Name: display_id Field Name: 6 Java Type: java.lang.String database type: CHAR field name: Is_gis Field Name: 1 


More detailed property output you can read the source code of the ResultSetMetaData object.

/* * @ (#) resultsetmetadata.java1.33 05/12/01 * * Copyright 2006 Sun Microsystems, Inc. All rights reserved. * SUN proprietary/confidential. Use are subject to license terms.  */Package java.sql; /** * An object so can be used to get information about the types * and properties of the columns in a <code>resu Ltset</code> object. * The following code fragment creates the <code>ResultSet</code> object RS, * creates the <code>resultse  Tmetadata</code> object Rsmd, and uses RSMD * to find out how many columns Rs have and whether the first column in RS * Can is 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> * Interface ResultSetMetaData extends Wrapper {/** * Returns the number of columns in this &LT ; CODE&GT;RESULTSET&LT;/code> object. * * @return The number of columns * @exception SQLException If a database access error occurs */int getcolumncount () thro  WS 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 ISAUTOINCR  ement (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 Iscasesens  itive (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 &LT;CODE&GT;TRUE&LT;/CODE&GT If so; <code>false</code> otherwise * @exception SQLException if a database access error occurs */Boolean Issearchab  Le (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 colum  n) throws SQLException; /** * The constant indicating 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 are unknown.  */int columnnullableunknown = 2; /** * Indicates whether values in the designated column is 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 (i  NT 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 getcolumndisplays  ize (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 isn't specified, the value returned from * <code>getColumnLabel</code> would 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 Sqlexcepti  On 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 databas  E 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 Sqlexce  ption;  /** * 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, the characters of the length of the String representation (assuming the * maximum allowed P Recision of the fractional seconds component). For binary data, the of 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 columnThe 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 SQ  Lexception If a database access error occurs */String gettablename (int column) throws SQLException; /** * Gets The designated column ' s table ' 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 GetC  Atalogname (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 sqlexcep tion 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 and 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 colum  n) throws SQLException; /** * Indicates whether a write on the designated column would 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 Isdefinite  lywritable (int column) throws SQLException; --------------------------JDBC 2.0-----------------------------------/** * <p>returns the Fully-qualified  Name of the Java class whose instances * is 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 is used by the method * <code>resultset.getobject</code&gt ; 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 S Qlexception;  }


Java Queries database table structure (field name, type, length, etc.) through JDBC

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.