D:\JavaFrameworks\InsuranceFramework\src\java\db_adapter\LIKE_Percentage.java
  1 /*
  2  * To change this template, choose Tools | Templates
  3  * and open the template in the editor.
  4  */
  5 
  6 package db_adapter;
  7 
  8 /**
  9  *
 10  * @author sameldin
 11  */
 12 import java.io.*;
 13 import java.sql.Connection;
 14 import java.sql.DriverManager;
 15 import java.sql.PreparedStatement;
 16 import java.sql.ResultSet;
 17 import java.sql.SQLException;
 18 import java.util.ArrayList;
 19 import java.util.Hashtable;
 20 import java.util.MissingResourceException;
 21 import java.util.ResourceBundle;
 22 import java.util.Calendar;
 23 import java.util.Date;
 24 import java.text.SimpleDateFormat;
 25 
 26 
 27 //import org.apache.log4j.Logger;
 28 //import org.apache.log4j.PropertyConfigurator;
 29 
 30 import utils.*;
 31 import constants.*;
 32 
 33 public class LIKE_Percentage
 34 {
 35     Connection  localConnection = null;
 36 
 37     public LIKE_Percentage()
 38     {
 39         BD_Connector localBD_Connector = new BD_Connector();
 40         localConnection = localBD_Connector.getConnection();
 41     }
 42     /*
 43      *
 44      */
 45      public int selectMatchingWithStartingSubtring (String passedTableName_1, String matchSunString, Enum_LIKE_Search passedEnum_LIKE_Search)
 46      {
 47         if(null == localConnection)
 48             return(Constants.ERROR_RUN);
 49         PreparedStatement   localPreparedStatement      = null;
 50         ResultSet           localResultSet              = null;
 51         //==========================
 52         try
 53         {
 54             // The first example that we'll take a look at involves using % in the where clause of a select statement.
 55             // We are going to try to find all of the Tbale_1 whose name begins with 'matchSunString'.
 56             //  SELECT * FROM suppliers
 57             //  WHERE tableName_1 like 'matchSunString%';
 58 
 59                 String qryString        = "";
 60                 String tableName_1        = DatabaseKeysPropertyManager.getParameter(passedTableName_1);
 61                 qryString       =   "SELECT * FROM " + tableName_1 + " WHERE CLIENT_LAST_NAME  ";
 62 
 63                 //==========================
 64                 switch(passedEnum_LIKE_Search)
 65                 {
 66                         case START_OF_STRING_ENUM_LIKE:
 67                         // SELECT * FROM table_1
 68                         // WHERE CLIENT_LAST_NAME LIKE 'Eldin%';
 69                             qryString   +=  "LIKE "
 70                                         +   "'"
 71                                         +   matchSunString
 72                                         +   "%"
 73                                         +   "';";
 74                             break;
 75                         case DOES_NOT_START_OF_STRING_ENUM_LIKE:
 76                         // SELECT * FROM table_1
 77                         // WHERE CLIENT_LAST_NAME NOT LIKE 'Eldin%';
 78                             qryString   +=  "NOT LIKE "
 79                                         +   "'"
 80                                         +   matchSunString
 81                                         +   "%"
 82                                         +   "';";
 83                             break;
 84                         case SUB_STRING_ENUM_LIKE:
 85                         // SELECT * FROM table_1
 86                         // WHERE CLIENT_LAST_NAME LIKE '%Eldin%';
 87                             qryString   +=  "LIKE "
 88                                         +   "'"
 89                                         +   "%"
 90                                         +   matchSunString
 91                                         +   "%"
 92                                         +   "';";
 93                             break;
 94                         case LOOKING_FOR_ONE_CHAR_ENUM_LIKE:
 95                         // SELECT * FROM table_1
 96                         // WHERE CLIENT_LAST_NAME LIKE 'Eldin_';
 97                             qryString   +=  "LIKE "
 98                                         +   "'"
 99                                         +   matchSunString
100                                         +   "_"
101                                         +   "';";
102                             break;
103                         case LOOK_FOR_ESCAPE_CHAR_ENUM_LIKE:
104                         // SELECT * FROM table_1
105                         // WHERE CLIENT_LAST_NAME LIKE '!%' escape '!';
106                             qryString   +=  "LIKE "
107                                         +   "'!%"
108                                         +   matchSunString
109                                         +   "'"
110                                         +   " ESCAPE '"
111                                         +   "!';";
112                             break;
113                         case LOOK_FOR_ESCAPE_UNDERSCORE_CHAR_ENUM_LIKE:
114                         // SELECT * FROM table_1
115                         // WHERE CLIENT_LAST_NAME LIKE LIKE 'Eldin%!_' escape '!';
116                             qryString   +=  "LIKE "
117                                         +   "'!%"
118                                         +   matchSunString
119                                         +   "'"
120                                         +   " ESCAPE '"
121                                         +   "!';";
122                             break;
123                         default:
124                  }
125 
126                 localPreparedStatement = localConnection.prepareStatement(qryString);
127                 localPreparedStatement.executeQuery();
128                 //=================================
129                 ArrayList localXYZArrayList = new ArrayList();
130                 while(localResultSet.next())
131                 {
132                     String clientLastName = localResultSet.getString("CLIENT_LAST_NAME");
133                     // ...
134                 }
135                 //=================================
136                 localPreparedStatement.close();
137         }
138         catch(MissingResourceException eMissingResourceException)
139         {
140             eMissingResourceException.printStackTrace();
141             return(Constants.ERROR_RUN);
142         }
143         catch(SQLException eSQLException)
144         {
145             eSQLException.printStackTrace();
146             return(Constants.ERROR_RUN);
147         }
148         return(Constants.NORMAL_RUN);
149      }
150 }
151 
152