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