D:\JavaFrameworks\InsuranceFramework\src\java\db_adapter\CreateNewTable.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 
 30 public class CreateNewTable
 31 {
 32     Connection  localConnection = null;
 33 
 34     public CreateNewTable()
 35     {
 36         BD_Connector localBD_Connector = new BD_Connector();
 37         localConnection = localBD_Connector.getConnection();
 38     }
 39     /*
 40      *
 41      */
 42      public int createBD_Table(String passedTableName)
 43      {
 44         if(null == localConnection)
 45             return(Constants.ERROR_RUN);
 46         PreparedStatement   localPreparedStatement      = null;
 47         //==========================
 48         // Now we want to create a table called "Persons" that contains five columns: PersonID, LastName, FirstName, Address, and City.
 49         // We use the following CREATE TABLE statement:
 50         // CREATE TABLE Persons
 51         // (
 52         //  PersonID int,
 53         //  LastName varchar(255),
 54         //  FirstName varchar(255),
 55         //  Address varchar(255),
 56         //  City varchar(255)
 57         // );
 58         try
 59         {
 60                 String qryString        = "";
 61                 String tableName        = DatabaseKeysPropertyManager.getParameter(passedTableName);
 62                 //==========================
 63                 qryString       =       "CREATE TABLE  "
 64                             +   passedTableName
 65                             +   "{"
 66                             +   "PersonID int,"
 67                             +   "LastName varchar(255),"
 68                             +   "FirstName varchar(255),"
 69                             +   "Address varchar(255),"
 70                             +   "City varchar(255)"
 71                             +   "};";
 72 
 73                 localPreparedStatement = localConnection.prepareStatement(qryString);
 74                 localPreparedStatement.executeQuery();
 75                 localPreparedStatement.close();
 76         }
 77         catch(MissingResourceException eMissingResourceException)
 78         {
 79             eMissingResourceException.printStackTrace();
 80             return(Constants.ERROR_RUN);
 81         }
 82         catch(SQLException eSQLException)
 83         {
 84             eSQLException.printStackTrace();
 85             return(Constants.ERROR_RUN);
 86         }
 87         return(Constants.NORMAL_RUN);
 88      }
 89     /*
 90      *
 91      */
 92      public int createBD_TableW_PRIMARY_KEY(String passedTableName)
 93      {
 94         if(null == localConnection)
 95             return(Constants.ERROR_RUN);
 96         PreparedStatement   localPreparedStatement      = null;
 97         //==========================
 98         // Now we want to create a table called "Persons" that contains five columns: PersonID, LastName, FirstName, Address, and City.
 99         // We use the following CREATE TABLE statement - NOT NULL PRIMARY KEY:
100         // CREATE TABLE Persons
101         // (
102         //  PersonID int NOT NULL PRIMARY KEY,
103         //  LastName varchar(255)  NOT NULL,
104         //  FirstName varchar(255),
105         //  Address varchar(255),
106         //  City varchar(255)
107         // );
108         try
109         {
110                 String qryString        = "";
111                 String tableName        = DatabaseKeysPropertyManager.getParameter(passedTableName);
112                 //==========================
113                 qryString       =       "CREATE TABLE  "
114                             +   passedTableName
115                             +   "{"
116                             +   "PersonID int NOT NULL PRIMARY KEY,"
117                             +   "LastName varchar(255)  NOT NULL,"
118                             +   "FirstName varchar(255),"
119                             +   "Address varchar(255),"
120                             +   "City varchar(255)"
121                             +   "};";
122 
123                 localPreparedStatement = localConnection.prepareStatement(qryString);
124                 localPreparedStatement.executeQuery();
125                 localPreparedStatement.close();
126         }
127         catch(MissingResourceException eMissingResourceException)
128         {
129             eMissingResourceException.printStackTrace();
130             return(Constants.ERROR_RUN);
131         }
132         catch(SQLException eSQLException)
133         {
134             eSQLException.printStackTrace();
135             return(Constants.ERROR_RUN);
136         }
137         return(Constants.NORMAL_RUN);
138      }
139     /*
140      *
141      */
142      public int createBD_TableW_PRIMARY_KEY_MultipleColumns(String passedTableName)
143      {
144         if(null == localConnection)
145             return(Constants.ERROR_RUN);
146         PreparedStatement   localPreparedStatement      = null;
147         //==========================
148         // Now we want to create a table called "Persons" that contains five columns: PersonID, LastName, FirstName, Address, and City.
149         // We use the following CREATE TABLE statement - NOT NULL PRIMARY KEY:
150         // To allow naming of a PRIMARY KEY constraint, and for defining a PRIMARY KEY constraint
151         // on multiple columns, use the following SQL syntax
152         // CREATE TABLE Persons
153         // (
154         //  PersonID int NOT NULL,
155         //  LastName varchar(255)  NOT NULL,
156         //  FirstName varchar(255),
157         //  Address varchar(255),
158         //  City varchar(255),
159         //  CONSTRAINT pk_PersonID PRIMARY KEY (PersonID,LastName)
160         // );
161         try
162         {
163                 String qryString        = "";
164                 String tableName        = DatabaseKeysPropertyManager.getParameter(passedTableName);
165                 //==========================
166                 qryString       =       "CREATE TABLE  "
167                             +   passedTableName
168                             +   "{"
169                             +   "PersonID int NOT NULL,"
170                             +   "LastName varchar(255)  NOT NULL,"
171                             +   "FirstName varchar(255),"
172                             +   "Address varchar(255),"
173                             +   "City varchar(255)"
174                             +   "CONSTRAINT pk_PersonID PRIMARY KEY (PersonID,LastName)"
175                             +   "};";
176 
177                 localPreparedStatement = localConnection.prepareStatement(qryString);
178                 localPreparedStatement.executeQuery();
179                 localPreparedStatement.close();
180         }
181         catch(MissingResourceException eMissingResourceException)
182         {
183             eMissingResourceException.printStackTrace();
184             return(Constants.ERROR_RUN);
185         }
186         catch(SQLException eSQLException)
187         {
188             eSQLException.printStackTrace();
189             return(Constants.ERROR_RUN);
190         }
191         return(Constants.NORMAL_RUN);
192      }
193 }
194 
195