Get database table structure information using JDBC Connect

Source: Internet
Author: User

1, this is the key to generate code

Introducing MAVEN Dependencies

   <dependency>            <groupId>mysql</groupId>            <artifactid>mysql-connector-java</ artifactid>            <version>5.1.30</version>        </dependency>

2. Writing Java classes

 PackageCom.yyj.mybties.jdbc;Importjava.sql.Connection;ImportJava.sql.DatabaseMetaData;ImportJava.sql.DriverManager;ImportJava.sql.ResultSet;Importjava.sql.SQLException;Importjava.util.ArrayList;Importjava.util.List;/*** Get the structure of tables in the database*/ Public classJdbcdemo { Public Static voidMain (string[] args) {String driver= "Com.mysql.jdbc.Driver"; String URL= "Jdbc:mysql://localhost:3306/mybase"; Connection Con=NULL; List<String> Tablenames =NewArrayList (); Try{con= Drivermanager.getconnection (URL, "root", "" "); GetTableInfo (Con,"Users"); } Catch(Exception e) {System.out.println ("Connect fail:" +e.getmessage ()); }}    Private Static voidGetTableInfo (Connection con,string tableName)throwsSQLException {DatabaseMetaData metaData=Con.getmetadata (); ResultSet Colret= Metadata.getcolumns (NULL, "%", tableName, "%");        String ColumnName;        String ColumnType;  while(Colret.next ()) {ColumnName= Colret.getstring ("column_name"); ColumnType= Colret.getstring ("Type_name"); intDataSize = Colret.getint ("Column_size"); intdigits = Colret.getint ("Decimal_digits"); intNullable = Colret.getint ("Nullable"); System.out.println (ColumnName+ "+columntype+" "+datasize+" "+digits+" "+nullable); } System.out.println ("\ n"); }}

Execution results

Get database table structure information using JDBC Connect

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.