D:\JavaFrameworks\InsuranceFramework\src\java\db_adapter\OrderBy.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 OrderBy
 34 {
 35     Connection  localConnection = null;
 36 
 37     public OrderBy()
 38     {
 39         BD_Connector localBD_Connector = new BD_Connector();
 40         localConnection = localBD_Connector.getConnection();
 41     }
 42     /*
 43      *
 44      */
 45      public int select(String passedTableName, long passedID, boolean case_1)
 46      {
 47         if(null == localConnection)
 48             return(Constants.ERROR_RUN);
 49         PreparedStatement   localPreparedStatement      = null;
 50         ResultSet           localResultSet              = null;
 51         //==========================
 52         try
 53         {
 54             // SELECT * FROM TBL_1, TBL_2 WHERE TBK_1.LOCATION_NUMBER = TBL_2.LOCATION_NUMBER AND TBK_1.ID_NUMBER = TBL_2.ID_NUMBER
 55             // ORDER BY digits(TBL_1.ID_NUMBER) DESC;
 56 
 57                 String qryString        = "";
 58                 String tableName        = DatabaseKeysPropertyManager.getParameter(passedTableName);
 59                 //==========================
 60                 if(case_1)
 61                 {
 62                     qryString   =       "SELECT MAX(ITEM_ID) FROM "
 63                                                 + tableName
 64                                                 + " WHERE "
 65                                                 + "USER_ID"
 66                                                 + " = "
 67                                                 + "'"
 68                                                 + passedID
 69                                                 + "'";
 70                 }
 71                 else
 72                 {
 73                         qryString       =       "SELECT ITEM_ID FROM "
 74                                                 + tableName
 75                                                 + " WHERE "
 76                                                 + "USER_ID"
 77                                                 + " = "
 78                                                 + "'"
 79                                                 + passedID
 80                                                 + "' order by digits(ITEM_ID) desc";
 81 //                                              + "' order by digits(ITEM_ID) ASC";
 82 //                                              + " ORDER BY ITEM_ID";
 83 
 84                 }
 85                 localPreparedStatement = localConnection.prepareStatement(qryString);
 86                 localResultSet = localPreparedStatement.executeQuery();
 87                 //===========================================
 88                 int itemID = 1;
 89                 String tempItem_ID_String  = "";
 90                 int maxItemID = 0;
 91                 while(localResultSet.next())
 92                 {
 93                         tempItem_ID_String  = localResultSet.getString("ITEM_ID_STRING");
 94                         if(             (tempItem_ID_String != null)
 95                                 &&      (tempItem_ID_String.length() > 0)
 96                         )
 97                         {
 98                                 tempItem_ID_String = tempItem_ID_String.trim();
 99                                 try
100                                 {
101                                         itemID = Integer.parseInt(tempItem_ID_String) + 1;
102                                 }
103                                 catch(NumberFormatException e)
104                                 {
105                                         itemID = 1;
106                                 }
107                         }
108                         if(maxItemID < itemID)
109                             maxItemID = itemID;
110                 }
111                 localPreparedStatement.close();
112         }
113         catch(MissingResourceException eMissingResourceException)
114         {
115             eMissingResourceException.printStackTrace();
116             return(Constants.ERROR_RUN);
117         }
118         catch(SQLException eSQLException)
119         {
120             eSQLException.printStackTrace();
121             return(Constants.ERROR_RUN);
122         }
123         return(Constants.NORMAL_RUN);
124      }
125 
126 }
127 
128