D:\JavaFrameworks\InsuranceFramework\src\java\db_adapter\DeleteRowsFromTables.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 
 23 //import org.apache.log4j.Logger;
 24 //import org.apache.log4j.PropertyConfigurator;
 25 
 26 import utils.*;
 27 import constants.*;
 28 
 29 public class DeleteRowsFromTables
 30 {
 31     Connection  localConnection = null;
 32 
 33     public DeleteRowsFromTables()
 34     {
 35         BD_Connector localBD_Connector = new BD_Connector();
 36         localConnection = localBD_Connector.getConnection();
 37     }
 38     /*
 39      *
 40      */
 41      public int deleteOneTableRow(String passedTableName, String name, String email)
 42      {
 43         if(null == localConnection)
 44             return(Constants.ERROR_RUN);
 45         PreparedStatement   localPreparedStatement      = null;
 46         //==========================
 47         // SQL DELETE Syntax
 48         // DELETE FROM table_name WHERE some_column=some_value;
 49         // Notice the WHERE clause in the SQL DELETE statement!
 50         // The WHERE clause specifies which record or records that should be deleted.
 51         // If you omit the WHERE clause, all records will be deleted!
 52 
 53         try
 54         {
 55                 String qryString        = "";
 56                 String tableName        = DatabaseKeysPropertyManager.getParameter(passedTableName);
 57                 //==========================
 58                 qryString       =       "DELETE FROM  "
 59                             +   passedTableName
 60                             +   " WHERE some_column="
 61                             +   name
 62                             +   " AND "
 63                             +   "EMAIL="
 64                             +   email;
 65                 localPreparedStatement = localConnection.prepareStatement(qryString);
 66                 localPreparedStatement.executeQuery();
 67                 localPreparedStatement.close();
 68         }
 69         catch(MissingResourceException eMissingResourceException)
 70         {
 71             eMissingResourceException.printStackTrace();
 72             return(Constants.ERROR_RUN);
 73         }
 74         catch(SQLException eSQLException)
 75         {
 76             eSQLException.printStackTrace();
 77             return(Constants.ERROR_RUN);
 78         }
 79         return(Constants.NORMAL_RUN);
 80      }
 81     /*
 82      *
 83      */
 84      public int deleteEnitreTable(String passedTableName)
 85      {
 86         if(null == localConnection)
 87             return(Constants.ERROR_RUN);
 88         PreparedStatement   localPreparedStatement      = null;
 89         //==========================
 90         // SQL DELETE Syntax
 91         // Delete All Data
 92         // It is possible to delete all rows in a table without deleting the table.
 93         // This means that the table structure, attributes, and indexes will be intact:
 94         //  DELETE FROM table_name;
 95         // or
 96         // DELETE * FROM table_name;
 97         // Note: Be very careful when deleting records. You cannot undo this statement!
 98         try
 99         {
100                 String qryString        = "";
101                 String tableName        = DatabaseKeysPropertyManager.getParameter(passedTableName);
102                 //==========================
103                 qryString       =       "DELETE * FROM "
104                             +   passedTableName;
105                 localPreparedStatement = localConnection.prepareStatement(qryString);
106                 localPreparedStatement.executeQuery();
107                 localPreparedStatement.close();
108         }
109         catch(MissingResourceException eMissingResourceException)
110         {
111             eMissingResourceException.printStackTrace();
112             return(Constants.ERROR_RUN);
113         }
114         catch(SQLException eSQLException)
115         {
116             eSQLException.printStackTrace();
117             return(Constants.ERROR_RUN);
118         }
119         return(Constants.NORMAL_RUN);
120      }
121     /*
122      *
123      */
124      public int truncateDataFromTable(String passedTableName)
125      {
126         if(null == localConnection)
127             return(Constants.ERROR_RUN);
128         PreparedStatement   localPreparedStatement      = null;
129         //==========================
130         //  The TRUNCATE TABLE Statement
131         //  What if we only want to delete the data inside the table, and not the table itself?
132         //  Then, use the TRUNCATE TABLE statement:
133         //  TRUNCATE TABLE table_name
134 
135         try
136         {
137                 String qryString        = "";
138                 String tableName        = DatabaseKeysPropertyManager.getParameter(passedTableName);
139                 //==========================
140                 qryString       =       "TRUNCATE TABLE "
141                             +   passedTableName;
142                 localPreparedStatement = localConnection.prepareStatement(qryString);
143                 localPreparedStatement.executeQuery();
144                 localPreparedStatement.close();
145         }
146         catch(MissingResourceException eMissingResourceException)
147         {
148             eMissingResourceException.printStackTrace();
149             return(Constants.ERROR_RUN);
150         }
151         catch(SQLException eSQLException)
152         {
153             eSQLException.printStackTrace();
154             return(Constants.ERROR_RUN);
155         }
156         return(Constants.NORMAL_RUN);
157      }
158     /*
159      *
160      */
161      public int dropBD_Table(String passedTableName)
162      {
163         if(null == localConnection)
164             return(Constants.ERROR_RUN);
165         PreparedStatement   localPreparedStatement      = null;
166         //==========================
167         // The DROP TABLE Statement
168         // The DROP TABLE statement is used to delete a table.
169         // DROP TABLE table_name
170         // The DROP DATABASE Statement
171         // The DROP DATABASE statement is used to delete a database.
172         // DROP DATABASE database_name
173         try
174         {
175                 String qryString        = "";
176                 String tableName        = DatabaseKeysPropertyManager.getParameter(passedTableName);
177                 //==========================
178                 qryString       =       "DROP "
179                             +   passedTableName;
180                 localPreparedStatement = localConnection.prepareStatement(qryString);
181                 localPreparedStatement.executeQuery();
182                 localPreparedStatement.close();
183         }
184         catch(MissingResourceException eMissingResourceException)
185         {
186             eMissingResourceException.printStackTrace();
187             return(Constants.ERROR_RUN);
188         }
189         catch(SQLException eSQLException)
190         {
191             eSQLException.printStackTrace();
192             return(Constants.ERROR_RUN);
193         }
194         return(Constants.NORMAL_RUN);
195      }
196 }
197 
198