use Java JDBC Metadata

1. Design

 Design concepts:

Option

Builder

Factory

Service  - Context

Handler - Adept

 

2. Get table

    static  void getDataSet () { 
        Connection CON = JdbcUtil.getConn ();
         the try 
        { 
            // description given directory retrieval tables available. Return only with the catalog, schema, table name and type criteria matching table shows.
            // them in the table type, table type, table plans and table names sorted
             // Cataog directory that contains the name of the String. This parameter provides a Null value indicates that without the use of directory names.
            // Schema contains the schema name String Value mode. Providing this parameter indicates a Null value without the use of schema name, corresponding to: a database
             // tableNamePattern String table contains the name of the model
             // types containing string type array table to be included. Null represents should contain all types of tables, the typical types "TABLE", "the VIEW", "the SYSTEM TABLE", "TEMPORARY the GLOBAL", "TEMPORARY the LOCAL", "ALIAS", "SYNONYM" 
            String Catalog = "Day";  
            The schemaPattern = String "*"; // MySQL no concept sql server name corresponding to a user operation authority 
            String tableNamePattern = "Student"; // a corresponding database name 
            String [] types = { "TABLE"}; // database specific type "TABLE", "VIEW", "SYSTEM TABLE", "GLOBAL TEMPORARY", "LOCAL TEMPORARY", "ALIAS" and "SYNONYM". 
            The metaData = the DatabaseMetaData con.getMetaData (); 
            the ResultSet RET = metaData.getTables ( null , null , null , types);
             the while (ret.next ()) { 


                String catalogName = RET.= ret.getString(2);
                String tableName = ret.getString(3);
                String columName = ret.getString(6);
                System.out.println(tableName);
                System.out.println(catalogName);
                System.out.println(columName);

//                ResultSetMetaData rsmd = ret.getMetaData();
//                int count =rsmd.getColumnCount();
//                for(int i=1;i<=count;i++)
//                {
//                    System.out.print(ret.getString(i)+"\n\t");
//                }
                //System.out.println("  " + username + " " + birthday + " " + sex  + " " + address);
            }
        }catch (SQLException e){
            e.printStackTrace();
        }
    }

 

3. Get colomn

    static  void GetColumn (String TableName) throws SQLException { 
        Connection Conn = JdbcUtil.getConn (); 
        the DatabaseMetaData The metaData = conn.getMetaData ();
         //
         String = Catalog "ucent"; // correspondence database name 
        String the schemaPattern = "*"; / / MySQL no concept sql server name corresponding to a user operation authority 
        String tableNamePattern TableName =; //
         String columnNamePattern = null ; 

        the ResultSet Result = metaData.getColumns (Catalog, the schemaPattern, tableNamePattern, columnNamePattern); 

        the while(result.next ()) {
 //             String columname result.getString = (. 4); // column name
 //             String result.getString data_type = (. 5); // column type
 //             System.out.println (columname) ;
 //             System.out.println (data_type); 

            String columnName = result.getString ( "COLUMN_NAME" ); 
            String columnType = result.getString ( "The TYPE_NAME"); // data type 
            String remark = result.getString ( "REMARKS" ); // data type 
            int the datasize = result.getInt ( "COLUMN_SIZE"); // field length 
            int digits = result.getInt("DECIMAL_DIGITS");//小数位数
            int nullable = result.getInt("NULLABLE");

            System.out.println(columnName+" "+columnType+" "+datasize+" "+digits + " "+ nullable +" " +remark);

//            ResultSetMetaData rsmd = result.getMetaData();
//            int count =rsmd.getColumnCount();
//            for(int i=1;i<=count;i++)
//            {
//                System.out.print(result.getString(i)+"\n\t");
//            }
        }
    }
NOTE: result.getString ( ""); the corresponding fields are named as follows:
TABLE_CAT,
TABLE_SCHEM,
TABLE_NAME,
COLUMN_NAME,
DATA_TYPE,
TYPE_NAME,
COLUMN_SIZE,
BUFFER_LENGTH,
DECIMAL_DIGITS,
NUM_PREC_RADIX,
NULLABLE,
REMARKS,
COLUMN_DEF,
SQL_DATA_TYPE,
SQL_DATETIME_SUB,
CHAR_OCTET_LENGTH,
ORDINAL_POSITION,
IS_NULLABLE,
SCOPE_CATALOG,
SCOPE_SCHEMA,
SCOPE_TABLE,
SOURCE_DATA_TYPE,
IS_AUTOINCREMENT,
IS_GENERATEDCOLUMN,

 

Guess you like

Origin www.cnblogs.com/leolzi/p/12552245.html