Read the table information from the database
Packagecom.cloud.smartreport.utils;Importjava.sql.Connection;ImportJava.sql.DatabaseMetaData;Importjava.sql.PreparedStatement;ImportJava.sql.ResultSet;ImportJava.sql.ResultSetMetaData;Importjava.util.ArrayList;Importjava.util.List;ImportOrg.smart4j.framework.dao.DatabaseHelper; Public classdbtoword{ Public Static voidMain (string[] args) {Try{Connection conn=databasehelper.getconnection (); DatabaseMetaData metadata=Conn.getmetadata (); System.out.println ("Database-known users:" +metadata.getusername ()); System.out.println ("Comma-separated list of system functions for the database:" +metadata.getsystemfunctions ()); System.out.println ("A comma-separated list of time and date functions for the database:" +metadata.gettimedatefunctions ()); System.out.println ("Comma-separated list of string functions for the database:" +metadata.getstringfunctions ()); System.out.println ("Database vendor's preferred term for ' schema ':" +metadata.getschematerm ()); System.out.println ("Database URL:" +Metadata.geturl ()); System.out.println ("Allow Read only:" +metadata.isreadonly ()); System.out.println ("Product name for database:" +metadata.getdatabaseproductname ()); System.out.println ("Version of database:" +metadata.getdatabaseproductversion ()); System.out.println ("Name of driver:" +metadata.getdrivername ()); System.out.println ("Version of Driver:" +metadata.getdriverversion ()); System.out.println (); System.out.println ("Table types used in the database"); ResultSet RS=metadata.gettabletypes (); while(Rs.next ()) {System.out.println (rs.getstring (1)); } rs.close (); System.out.println (); /*** Gets the type of all tables for the specified database, the first parameter of Gettables () is the database name because it is not specified when connected to MySQL, plus, the remaining parameters can be null. * The second parameter is the pattern name pattern , but the output is nothing. Who knows, tell me.*/System.out.println ("Gets the type of all tables for the specified database"); ResultSet rs1= Metadata.gettables ("Stock",NULL,NULL,NULL); while(Rs1.next ()) {System.out.println (); System.out.println ("Database name:" + rs1.getstring (1)); System.out.println ("Table name:" + rs1.getstring (3)); System.out.println ("Type:" + rs1.getstring (4)); String TableName= Rs1.getstring (3); List<String> keylist =NewArraylist<string>(); ResultSet Rs3= Metadata.getprimarykeys ("Stock",NULL, TableName); while(Rs3.next ()) {Keylist.add (rs3.getstring (4)); } rs3.close (); PreparedStatement PS= Conn.preparestatement ("SELECT * from" +tablename); ResultSet rs2=Ps.executequery (); ResultSetMetaData Rsme=Rs2.getmetadata (); intColumnCount =Rsme.getcolumncount (); //System.out.println ("Number of columns in the ResultSet object" + columnCount);SYSTEM.OUT.PRINTLN ("Column Definition | Auto-Numbering | is empty | Primary Key "); for(inti = 1; i < ColumnCount; i++) {System.out.print (Rsme.getcolumnname (i)+ "" + rsme.getcolumntypename (i) + "(" + rsme.getprecision (i) + ")"); System.out.print ("| " +rsme.isautoincrement (i)); System.out.print ("| " +rsme.isnullable (i)); System.out.println ("| " +Keylist.contains (Rsme.getcolumnname (i))); } System.out.println (); Rs2.close (); Ps.close (); } rs1.close (); } Catch(Exception e) {e.printstacktrace (); } }}
Output Result:
Database name: Stock table Name: Trd_daily_volume type: Table column definition | autonumber | empty | PRIMARY Key ID BIGINT | true| 0| truestock_code VARCHAR (6) | f alse| 0| Truetrade_date DATETIME (19) | false| 1| Falsetrade_status INT (10) | false| 1| Falseopen_price DOUBLE (9) | false| 1| Falsehigh_price DOUBLE (9) | false| 1| Falselow_price DOUBLE (9) | false| 1| Falseclose_price DOUBLE (9) | false| 1| Falsednshr_trade BIGINT (12) | false| 1| False
Java read table definition in database