Notes database Java_jdbc fourteen basis of connection (metadata) information and link information database

 

public  class MetaDatatest {
     / ** 
     * is metadata describing the DatabaseMetaData object database can be obtained by the Connection 
     * / 
    @Test 
    public  void testDatabaseMetaData () { 
        Connection Conn = null ; 
        the ResultSet RS = null ;
         the try { 
            Conn = JDBCTools.getConnection (); 
            Data the DatabaseMetaData = conn.getMetaData ();
             // get some basic information database itself
             // 1, to obtain the version number of the database 
            int version = data.getDatabaseMajorVersion ();
            System.out.println (Version); 
            // User name database Rodeo 2. 
            String User = data.getUserName (); 
            System.out.println (User); 
            // which databases 3, there is obtained MySQL 
            RS = Data .getCatalogs ();
             the while (rs.next ()) { 
                System.out.println (rs.getString ( . 1 )); 
            } 

        } the catch (Exception E) { 
            e.printStackTrace (); 
        } the finally { 
            JDBCTools.close (RS , null , Conn); 
        } 

    } 

    // ================================================= ======= 
    / ** 
     * the ResultSetMetaData: describe the result set metadata may obtain basic information about the result set: result set which columns, column names, aliases column. . . 
     * 
     * / 
    @Test 
    public  void testResultSetMetaData () { 
        Connection Conn = null ; 
        the PreparedStatement PS = null ; 
        the ResultSet RS = null ;
         the try { 
            Conn = JDBCTools.getConnection (); 
            String SQL = "the SELECT ID, name customersName, CustEmail In Email, Birth the Customers the FROM " ; 
            PS =conn.prepareStatement (SQL); 
            RS = );ps.executeQuery ();
             // . 1 ResultSetMetaData object obtained 
            ResultSetMetaData RMSD = rs.getMetaData ();
             // 2 to obtain the number of columns 
            int the columnCount = rmsd.getColumnCount (); 
            System.out.println (the columnCount); 
            for ( int 0 = I; I <the columnCount; I ++ ) {
                 // . 3 to give the column name 
                String rmsd.getColumnName columnName = (I +. 1 ); 

                // 4. get the column aliases 
                String columnLabel rmsd.getColumnLabel = (I +. 1 
                the System.out .println (columnName + "" + columnLabel); 
            }

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            JDBCTools.close(rs, ps, conn);

        }

    }

}

 

 

 

Turn: https://blog.csdn.net/YL1214012127/article/details/48374599

Guess you like

Origin www.cnblogs.com/fps2tao/p/12027406.html