Java – Database – Get DB Metadata

Background

Wanted to quickly review our DataSource Connection.

Code


   
    private String getClassName(Object obj)
    {

        Class  objClass;
        String strClassName;    
    
        objClass = obj.getClass();
        
        strClassName = objClass.getName();
         
        return (strClassName);
                    
    }


    private void fetchDBMetadata
    (
        PrintWriter out
    )
    {
        
        String strURL;
        String[] arrayOfToken  = null;
        
        int iLen = 0;
        int i =0;
        String strData = null;      
        
        try 
        {
            
            /*
                Create InitialContext
            */          
            objCTX = new InitialContext();

            Object obj = null;
       
            /*
                Lookup specific context
            */                 
            obj = objCTX.lookup
                    (
                        strDBID
                    );

            if (obj instanceof DataSource)
            {               
                objDS = (DataSource) obj;
            }
            else
            {
                
                strClassName = getClassName(obj);
                 
                strLog = "Unexpected Object type returned from context "
                            + "ID " + strDBID
                            + " Object Type " 
                            + strClassName
                            ;
                            
                throw new Exception(strLog);
                
            }     
            
            try
            {

                objDBConn = objDS.getConnection();
                
                objDBMetadata = objDBConn.getMetaData();
                
                strURL = objDBMetadata.getURL();
                
                out.println("URL :- " + strURL + CHAR_CRLF);

                out.println(CHAR_CRLF + CHAR_CRLF);             
                
                int iProp = 0;
                
                String strURLProp;
                
                iProp = strURL.indexOf(";");
                
                //out.println("iProp :- " + iProp + CHAR_CRLF);             
                
                strURLProp = strURL.substring(iProp + 1);
                
                //out.println("URLProp :- " + strURLProp + CHAR_CRLF);
                
                if (strURLProp != null)
                {
                    
                    // get token    
                    arrayOfToken = strURLProp.split(";");
                    
                    // get token's length                       
                    if (arrayOfToken == null)
                    {
                        iLen = -1;
                    }
                    else
                    {
                        iLen = arrayOfToken.length;                 
                    }   
                    
                    strLog = "Number of Properties " 
                                + iLen
                                + CHAR_CRLF
                                ;
                    
                    out.println(strLog);
                    
                    out.println(CHAR_CRLF);                                 
                
                    for (i = 0; i < iLen; i++)
                    {
                        strData = arrayOfToken[i];
                        
                        if (strData != null)
                        {
                            
                            strLog = CHAR_TAB
                                        + i 
                                        + ")"
                                        + " " 
                                        + strData
                                        ;
                            
                            out.println(strLog + CHAR_CRLF);                    
                            
                        }   
                        
                    }   
                        
                } // for i
                
            }
            catch (Exception ex)
            {
                          
                out.println("Exception in fetchMetadata ");
                    
                ex.printStackTrace();
                
                out.println(ex.getMessage());
            
            }
        
      
            
        } 
        catch (SQLException ex) 
        {
            
           System.out.println(ex.getMessage());         
           
           ex.printStackTrace();
                    
           out.println(ex.getMessage());
           
        }
        catch (NamingException ex) 
        {
           System.out.println(ex.getMessage());         
           
           ex.printStackTrace();
           
           out.println(ex.getMessage());
           
        }
        catch (Exception ex) 
        {
           System.out.println(ex.getMessage());         
           
           ex.printStackTrace();
                    
           out.println(ex.getMessage());
           
        }       
        finally 
        {
            
           if (objDBConn != null)
           {
                try
                {
                    objDBConn.close();
                }
                catch (SQLException ex) 
                {
							
					System.out.println(ex.getMessage());         

                    ex.printStackTrace();
					
					out.println(ex.getMessage());

                } 
                
           }
           
       } // finally
   
    } //fetchDBMetaData

Output

Output – StackOverflow2010

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s