D:\JavaFrameworks\InsuranceFramework\src\java\db_adapter\Alter.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 /** example of Java Doc
 13  * This method
 14 * <b>Note:</b>
 15 * <code>getRequestFunctionName</code>
 16 * @see  image
 17 * {@link URL} // absolute
 18 * @return Boolean
 19 * @throws CorpAdException In case of error, the exception is raised
 20 * @param req javax.servlet.http.HttpServletRequest
 21 * @param res javax.servlet.http.HttpServletResponse
 22 * @exception javax.servlet.ServletException The exception description.
 23 * @exception java.io.IOException The exception description.
 24 */
 25 
 26 import java.io.*;
 27 import java.sql.Connection;
 28 import java.sql.DriverManager;
 29 import java.sql.PreparedStatement;
 30 import java.sql.ResultSet;
 31 import java.sql.SQLException;
 32 import java.util.ArrayList;
 33 import java.util.Hashtable;
 34 import java.util.MissingResourceException;
 35 import java.util.ResourceBundle;
 36 import java.util.Calendar;
 37 import java.util.Date;
 38 import java.text.SimpleDateFormat;
 39 //import org.apache.log4j.Logger;
 40 //import org.apache.log4j.PropertyConfigurator;
 41 
 42 import utils.*;
 43 import constants.*;
 44 
 45 public class Alter
 46 {
 47     Connection  localConnection = null;
 48 
 49     public Alter()
 50     {
 51         BD_Connector localBD_Connector = new BD_Connector();
 52         localConnection = localBD_Connector.getConnection();
 53     }
 54     /*
 55      * ALTER TABLE Persons
 56      * ADD DateOfBirth date
 57      */
 58      public int alterByAddColumn(String passedTableName_1, String columnName, String typeField)
 59      {
 60         if(null == localConnection)
 61             return(Constants.ERROR_RUN);
 62         PreparedStatement   localPreparedStatement      = null;
 63         //==========================
 64         try
 65         {
 66                 String qryString        = "";
 67                 String tableName_1        = DatabaseKeysPropertyManager.getParameter(passedTableName_1);
 68                 //==========================
 69                 qryString       =       "ALTER TABLE "
 70                                 + tableName_1
 71                                 + "ADD "
 72                                 + columnName
 73                                 + " "
 74                                 + typeField;
 75 
 76                 localPreparedStatement = localConnection.prepareStatement(qryString);
 77                 localPreparedStatement.executeQuery();
 78                 localPreparedStatement.close();
 79          }
 80         catch(MissingResourceException eMissingResourceException)
 81         {
 82             eMissingResourceException.printStackTrace();
 83             return(Constants.ERROR_RUN);
 84         }
 85         catch(SQLException eSQLException)
 86         {
 87             eSQLException.printStackTrace();
 88             return(Constants.ERROR_RUN);
 89         }
 90         return(Constants.NORMAL_RUN);
 91      }
 92     /*
 93      * ALTER TABLE Persons
 94      * ADD PRIMARY KEY (P_Id)
 95      *
 96      */
 97      public int alterByAddingPrimayKey(String passedTableName_1)
 98      {
 99         if(null == localConnection)
100             return(Constants.ERROR_RUN);
101         PreparedStatement   localPreparedStatement      = null;
102         //==========================
103         try
104         {
105                 String qryString        = "";
106                 String tableName_1        = DatabaseKeysPropertyManager.getParameter(passedTableName_1);
107                 //==========================
108                 qryString       =       "ALTER TABLE "
109                                 + tableName_1
110                                 + "ADD PRIMARY KEY (P_Id) ";
111 
112                 localPreparedStatement = localConnection.prepareStatement(qryString);
113                 localPreparedStatement.executeQuery();
114                 localPreparedStatement.close();
115          }
116         catch(MissingResourceException eMissingResourceException)
117         {
118             eMissingResourceException.printStackTrace();
119             return(Constants.ERROR_RUN);
120         }
121         catch(SQLException eSQLException)
122         {
123             eSQLException.printStackTrace();
124             return(Constants.ERROR_RUN);
125         }
126         return(Constants.NORMAL_RUN);
127      }
128 
129     /*
130      * ALTER TABLE Persons
131       * DROP PRIMARY KEY
132      *
133      */
134      public int alterByDroppingPrimayKey(String passedTableName_1)
135      {
136         if(null == localConnection)
137             return(Constants.ERROR_RUN);
138         PreparedStatement   localPreparedStatement      = null;
139         //==========================
140         try
141         {
142                 String qryString        = "";
143                 String tableName_1        = DatabaseKeysPropertyManager.getParameter(passedTableName_1);
144                 //==========================
145                 qryString       =       "ALTER TABLE "
146                                 + tableName_1
147                                 + "DROP PRIMARY KEY";
148 
149                 localPreparedStatement = localConnection.prepareStatement(qryString);
150                 localPreparedStatement.executeQuery();
151                 localPreparedStatement.close();
152          }
153         catch(MissingResourceException eMissingResourceException)
154         {
155             eMissingResourceException.printStackTrace();
156             return(Constants.ERROR_RUN);
157         }
158         catch(SQLException eSQLException)
159         {
160             eSQLException.printStackTrace();
161             return(Constants.ERROR_RUN);
162         }
163         return(Constants.NORMAL_RUN);
164      }
165     /*
166      * ALTER TABLE supplier
167      * RENAME COLUMN supplier_name to sname;
168      */
169      public int alterNewColumnName(String passedTableName_1, String oldName, String newName)
170      {
171         if(null == localConnection)
172             return(Constants.ERROR_RUN);
173         PreparedStatement   localPreparedStatement      = null;
174         //==========================
175         try
176         {
177             // ALTER TABLE table_name
178             // RENAME COLUMN old_name to new_name;
179                 String qryString        = "";
180                 String tableName_1        = DatabaseKeysPropertyManager.getParameter(passedTableName_1);
181                 //==========================
182                 qryString       =       "ALTER TABLE "
183                                 + tableName_1
184                                 + " RENAME COLUMN "
185                                 + oldName
186                                 + " to "
187                                 + newName;
188 
189                 localPreparedStatement = localConnection.prepareStatement(qryString);
190                 localPreparedStatement.executeQuery();
191                 localPreparedStatement.close();
192          }
193         catch(MissingResourceException eMissingResourceException)
194         {
195             eMissingResourceException.printStackTrace();
196             return(Constants.ERROR_RUN);
197         }
198         catch(SQLException eSQLException)
199         {
200             eSQLException.printStackTrace();
201             return(Constants.ERROR_RUN);
202         }
203         return(Constants.NORMAL_RUN);
204      }
205 
206 }
207 
208