D:\JavaFrameworks\InsuranceFramework\src\java\db_adapter\CreateNewTable.java |
1
2
3
4
5
6 package db_adapter;
7
8
9
10 @author
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
24
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
49
50
51
52
53
54
55
56
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
99
100
101
102
103
104
105
106
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
149
150
151
152
153
154
155
156
157
158
159
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