Obtain table information --- ResultSetMetaData

Source: Internet
Author: User
In this demo, the database is accessed, and the table name in the database is STUDENTINFO, the IDNO text type contains fields. The 8NAME text type is 8. The 8SEX text type is 6 age. The 8BIRTHDT text type is 8. This table is operated mainly on. The driver selected is ODBC. The program is as follows: importjava.

In this demo, the database is accessed, and the table name in the database is STUDENTINFO, contains the field IDNO text type length 8 NAME text type length 8 SEX text type length 6 AGE numeric type length 8 BIRTHDT text type length 8 program is mainly to operate on this table, the driver is ODBC. The program is as follows: import java.

In this demo, the database is accessed, and the table name in the database is STUDENTINFO.

The IDNO text size is 8.

The NAME text is 8 characters long.

SEX text size: 6

AGE value: 8

The BIRTHDT text length is 8.

The program mainly operates on this table, and the driver selected is ODBC. The procedure is as follows:

Import java. SQL .*;

Public class DBAccess {

Public static java. SQL. Connection conn = null;

Private String sqlStr = "";

Public DBAccess ()

{

// New sun. jdbc. odbc. JdbcOdbcDriver ();

Try {

Class. forName ("sun. jdbc. odbc. JdbcOdbcDriver ");

Conn = DriverManager. getConnection ("jdbc: odbc: TestDB", "admin ","");

}

Catch (ClassNotFoundException ex ){

System. out. println (ex. toString ());

}

Catch (SQLException sqlEx ){

System. out. println (sqlEx. toString ());

}

}

Public ResultSet Search ()

{

ResultSet rset = null;

SqlStr = "SELECT * from studentinfo ";

Statement smt = null;

Try {

Smt = conn. createStatement ();

Rset = smt.exe cuteQuery (sqlStr );

}

Catch (SQLException ex ){

System. out. println ("Exception:" + ex. toString ());

}

Return rset;

}

Public void getResultSetMetaData ()

{

ResultSet rs = null;

Try {

String [] tp = {"TABLE "};

Rs = this. Search ();

ResultSetMetaData rsmd = rs. getMetaData ();

/*

Obtain the ResultSetMeataData object. The parameters of all methods are the index number of the column, that is, the column number, starting from 1.

*/

System. out. println ("the following methods are the methods in ResultSetMetaData ");

System. out. println ("Get the Catalog name of Column 1:" + rsmd. getCatalogName (1 ));

System. out. println ("Get the class of 1 column corresponding to the Data Type" + rsmd. getColumnClassName (1 ));

System. out. println ("Get the number of all columns of the ResultSet" + rsmd. getColumnCount ());

System. out. println ("1 Maximum number of characters in the database type" + rsmd. getColumnDisplaySize (1 ));

System. out. println ("the default Column Title of 1 column" + rsmd. getColumnLabel (1 ));

System. out. println ("1-column mode" + rsmd. GetSchemaName (1 ));

System. out. println ("1 column type, return number in SqlType" + rsmd. getColumnType (1 ));

System. out. println ("1 column type in the database, return type full name" + rsmd. getColumnTypeName (1 ));

System. out. println ("1 column type precision (type length):" + rsmd. getPrecision (1 ));

System. out. println ("number of decimal places in the 1 column" + rsmd. getScale (1 ));

System. out. println ("Name of the schema corresponding to column 1 (should be used for Oracle)" + rsmd. getSchemaName (1 ));

System. out. println ("table name corresponding to 1 column" + rsmd. getTableName (1 ));

System. out. println ("Whether the column 1 is auto-incrementing" + rsmd. isAutoIncrement (1 ));

System. out. println ("1 column in the database is a currency type" + rsmd. isCurrency (1 ));

System. out. println ("Whether the column 1 is empty" + rsmd. isNullable (1 ));

System. out. println ("Whether the column 1 is read-only" + rsmd. isReadOnly (1 ));

System. out. println ("Can 1 column appear in where" + rsmd. isSearchable (1 ));

}

Catch (SQLException ex ){

Ex. printStackTrace ();

}

}

Public static void main (String args [])

{

DBAccess dbAccess = new DBAccess ();

DbAccess. getResultSetMetaData ();

}

}

The method in the above example can be used to obtain the table information in the ResultSet. I am not very clear about the usage of the two methods, so I did not write it, I figured it out, and then I made a supplement.

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.