package util.sqlparse.visitor.mysql.test;

import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;

public class test {
   public static void main(String[] args) throws SQLException {
      Connection connection = DriverManager.getConnection("jdbc:mysql://192.168.2.229:3306/ry", "root", "root");
      DatabaseMetaData metaData = connection.getMetaData();
      getColumns(metaData);
   }

   static void getPks(DatabaseMetaData metaData) throws SQLException {
      System.out.println("--------get pks-----------------");
      ResultSet rs = metaData.getPrimaryKeys(null, null, "SYS_USER");

      while(rs.next()) {
         String schemaName = rs.getString("TABLE_CAT");
         String tableName = rs.getString("TABLE_NAME");
         String columnName = rs.getString("COLUMN_NAME");
         System.out.println(String.format("%s\t%s\t%s", schemaName, tableName, columnName));
         System.out.println("===============");
      }

   }

   static void getFks(DatabaseMetaData metaData) throws SQLException {
      System.out.println("--------get fks-----------------");
      ResultSet rs = metaData.getImportedKeys(null, null, "SYS_USER");

      while(rs.next()) {
         String schemaName = rs.getString("TABLE_CAT");
         String tableName = rs.getString("TABLE_NAME");
         String columnName = rs.getString("FKCOLUMN_NAME");
         System.out.println(String.format("%s\t%s\t%s", schemaName, tableName, columnName));
         System.out.println("===============");
      }

   }

   static void getColumns(DatabaseMetaData metaData) throws SQLException {
      System.out.println("--------get columns-----------------");
      ResultSet rs = metaData.getColumns(null, "`ry`,`test`", null, null);

      while(rs.next()) {
         String tableCat = rs.getString("TABLE_CAT");
         String tableName = rs.getString("TABLE_NAME");
         String columnName = rs.getString("COLUMN_NAME");
         String dataType = rs.getString("DATA_TYPE");
         String typeName = rs.getString("TYPE_NAME");
         String columnSize = rs.getString("COLUMN_SIZE");
         String bufferLength = rs.getString("BUFFER_LENGTH");
         String decimalDigits = rs.getString("DECIMAL_DIGITS");
         String numPrecRadix = rs.getString("NUM_PREC_RADIX");
         String nullable = rs.getString("NULLABLE");
         String remarks = rs.getString("REMARKS");
         String columnDef = rs.getString("COLUMN_DEF");
         String sqlDataType = rs.getString("SQL_DATA_TYPE");
         String sqlDatetimeSub = rs.getString("SQL_DATETIME_SUB");
         String charOctetLength = rs.getString("CHAR_OCTET_LENGTH");
         String ordinalPosition = rs.getString("ORDINAL_POSITION");
         String isNullable = rs.getString("IS_NULLABLE");
         System.out.println(String.format("%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t", tableCat, tableName, columnName, dataType, typeName, columnSize, bufferLength, decimalDigits, numPrecRadix, nullable, remarks, columnDef, sqlDataType, sqlDatetimeSub, charOctetLength, ordinalPosition, isNullable, columnDef, sqlDatetimeSub));
         System.out.println("===============");
      }

   }
}
