JDBC obtains metadata DatabaseMetaData, databasemetadata

Source: Internet
Author: User

JDBC obtains metadata DatabaseMetaData, databasemetadata

Package org. data. jdbc; import java. SQL. connection; import java. SQL. databaseMetaData; import java. SQL. driverManager; import java. SQL. preparedStatement; import java. SQL. resultSet; import java. SQL. resultSetMetaData; import java. SQL. SQLException; public class Jdbc {// database connection address private static final String url = "jdbc: mysql: // localhost: 3306/data "; // username and password private static final String user = "root"; private static final Str Ing pwd = "123456";/***** database query * @ throws ClassNotFoundException */public static void query () throws ClassNotFoundException {Connection conn = null; java. SQL. preparedStatement pst = null; ResultSet re = null;/** DBC provides DatabaseMetaData to encapsulate the database information corresponding to the database Connection, and obtains the object through the getMetaData method of the Connection. * The DatabaseMetaData interface is usually implemented by the database driver provider to help users understand the underlying information of the database. * You can use this interface to understand the underlying implementation of the database and switch between multiple databases. */DatabaseMetaData metadata = null; try {Class. forName ("com. mysql. jdbc. driver "); conn = DriverManager. getConnection (url, user, pwd); metadata = conn. getMetaData (); System. out. println ("known database users:" + metadata. getUserName (); System. out. println ("comma-separated list of database system functions:" + metadata. getSystemFunctions (); System. out. println ("comma-separated list of database time and date functions:" + metadata. getTimeDateFunctions (); System. out. println ("comma-separated list of database string functions: "+ Metadata. getStringFunctions (); System. out. println ("the preferred term for database providers for 'schema':" + metadata. getSchemaTerm (); System. out. println ("Database URL:" + metadata. getURL (); System. out. println ("allow read-only:" + metadata. isReadOnly (); System. out. println ("database product name:" + metadata. getDatabaseProductName (); System. out. println ("database version:" + metadata. getDatabaseProductVersion (); System. out. println ("driver name:" + metadata. g EtDriverName (); System. out. println ("driver version:" + metadata. getDriverVersion (); System. out. println ("driver version:" + metadata. getTableTypes (); System. out. println ("..................................... ....... "); pst = conn. prepareStatement ("select * from customerinfo"); // obtain the database column recyclpst.exe cuteQuery (); // The database column name ResultSetMetaData data = re. getMetaData (); // getColumnCount obtains the number of table columns for (int I = 1; I <= data. getColumnC Ount (); I ++) {// obtain the Data Length of the List index starting from 1, column name, column type, and column. System. out. println (data. getColumnName (I) + "\ t" + data. getColumnTypeName (I) + "\ t" + data. getColumnDisplaySize (I);} catch (SQLException e) {e. printStackTrace () ;}finally {closeConnection (conn, pst, re );}} /*** close the database link * @ param conn * @ param pst * @ param re */public static void closeConnection (Connection conn, PreparedStatement pst, ResultSet re) {if (conn! = Null) try {conn. close ();} catch (SQLException e) {// TODO Auto-generated catch blocke. printStackTrace ();} if (pst! = Null) try {pst. close ();} catch (SQLException e) {// TODO Auto-generated catch blocke. printStackTrace ();} if (re! = Null) try {re. close ();} catch (SQLException e) {// TODO Auto-generated catch blocke. printStackTrace () ;}} public static void main (String [] args) {try {query ();} catch (ClassNotFoundException e) {// TODO Auto-generated catch blocke. printStackTrace ();}}}

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.