Tuesday, February 4, 2014

Jdbc Programme to find the columns which are having values that are length and starts with A and containing number after first letter ?

The following example was tried on Oracle database..I think mysql and other databases
may not support all_tab_columns.... Please check....

package javalearners;

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

public class TableMatcher {
  public void displayTables() {

    System.out.println("-------- Oracle JDBC Connection Testing ------");

    try {

        Class.forName("oracle.jdbc.driver.OracleDriver");

    } catch (ClassNotFoundException e) {

        System.out.println("Where is your Oracle JDBC Driver?");
        e.printStackTrace();
        return;

    }

    System.out.println("Oracle JDBC Driver Registered!");

    Connection connection = null;

    try {

        connection = DriverManager.getConnection(
                "jdbc:oracle:thin:@localhost:1521:XE", "system",
                "oracle");

    } catch (SQLException e) {

        System.out.println("Connection Failed! Check output console");
        e.printStackTrace();
        return;

    }

    if (connection != null) {
        System.out.println("You made it, take control your database now!");
    } else {
        System.out.println("Failed to make connection!");
    }
   
    String allColumns = "select * from all_tab_columns ";
    Statement colSt=null,findSt = null;
    ResultSet colRs=null,findRs = null;
   
    try{
   
       colSt = connection.createStatement();
       colRs = colSt.executeQuery(allColumns);
      
       findSt = connection.createStatement();

       while(colRs.next()){
   
           String columnName = "";
           if(colRs.getString("column_name") != null ){
               columnName = colRs.getString("column_name");
           }
           String dynamicSt = "SELECT count(*) as cnt from "+colRs.getString("owner")+"."+
           colRs.getString("table_name")+" WHERE "+
           colRs.getString("column_name")+" like 'A%' and " +
                   " upper(substr("+columnName+",2))= lower(substr("+columnName+",2)) and length(trim("+colRs.getString("column_name")+")) =7";
     
          
           // System.out.println(colRs.getString("owner"));
           // System.out.println(colRs.getString("table_name"));
           //System.out.println(colRs.getString("column_name"));
           System.out.println(dynamicSt);
           try{
              int ct = 0;
              findRs = findSt.executeQuery(dynamicSt);
              if(findRs.next()){
                  ct = findRs.getInt("cnt");
              }
              //System.out.println("ct is "+ct);
              if(ct > 0 ){
                 System.out.println("Schema: "+colRs.getString("owner")+" Table:***"+colRs.getString("table_name"));
                 System.out.print("ColName "+colRs.getString("column_name"));
              }
     
           }catch(SQLException e ){
              
              
           }
       }
    }catch(SQLException e ){
          System.out.println(e.getMessage());      
    }finally{
        try{
          if(colSt != null )
             colSt.close();
          if(findSt != null)
              findSt.close();
          if(colRs != null)
              colRs.close();
          if(findRs!=null)
              findRs.close();
          if(connection != null )
              connection.close();
        }catch(Exception e ){
           
        }
       
    }
      
      
    }// End of method
   
   
   
   
  }   
   
 
Assume you have 3 tables with data as given below

StudentTable

sid  sname         saddress
1     Anil             Kumar
2     A123456    test

SecondTable

sid  sname         saddress
1     Anil             Kumar
2     ABC            test

ThirdTable

sid  sname         saddress
1     Anil             Kumar
2     ABC            A987654





 My Output should be
 ------------------------
StudentTable -- sname (since sname column contains value with length 7 and first character is A
and rest of the characters are digits....)

ThirdTable--saddress(since saddress column contains value with length 7 and first character is A
and rest of the characters are digits....)


















No comments:

Post a Comment