KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > stockonline > database > oracle > Seed


1 /*
2  * StockOnline: EJB 1.1 Benchmark.
3  *
4  * Copyright Commonwealth Scientific and Industrial Research Organisation (CSIRO - www.csiro.au), Australia 2001, 2002, 2003.
5  *
6  * Contact: Paul.Brebner@csiro.au
7  *
8  * This library is free software; you can redistribute it and/or modify it
9  * under the terms of the GNU Lesser General Public License as published by
10  * the Free Software Foundation; either version 2.1 of the License, or any
11  * later version.
12  *
13  * This library is distributed in the hope that it will be useful, but WITHOUT
14  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
15  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
16  * for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public License
19  * along with this library; if not, write to the Free Software Foundation,
20  * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
21  *
22  * Originally developed for the CSIRO Middleware Technology Evaluation (MTE) Project, by
23  * the Software Architectures and Component Technologies Group, CSIRO Mathematical and Information Sciences
24  * Canberra and Sydney, Australia
25  *
26  * www.cmis.csiro.au/sact/
27  * www.cmis.csiro.au/adsat/mte.htm
28  *
29  * Initial developer(s): Shiping Chen, Paul Brebner, Lei Hu, Shuping Ran, Ian Gorton, Anna Liu.
30  * Contributor(s): ______________________.
31  */

32
33
34 /*
35  * @(#) seed.java
36  *
37  *
38  *
39  * This java program is to seed an oracle stockonline database
40  * using Oracle JDBC driver.
41  *
42  *
43  */

44
45 package stockonline.database.oracle;
46
47 import java.util.*;
48 import java.io.*;
49 import java.sql.*;
50
51 public class Seed
52 {
53     final boolean verbose = true;
54     // Change the following settings for your database
55
//
56
String JavaDoc dbURL;
57     String JavaDoc userName;
58     String JavaDoc passWord;
59
60     // The following parameters will be set on the command-line
61
//
62
int ACCT = 3000; // number of accounts
63
int ITEM = 3000; // number of stock items
64
int HOLDING = 10; // number of stockholding per account
65

66     Connection con;
67     Statement stmt;
68     String JavaDoc sql;
69
70     public Seed(String JavaDoc[] args) throws Exception JavaDoc
71     {
72         dbURL = new String JavaDoc(args[0]);
73         userName = new String JavaDoc(args[1]);
74         passWord = new String JavaDoc(args[2]);
75
76         System.out.println("Argments:");
77         
78         System.out.println("dbURL = " + dbURL);
79         System.out.println("userName = " + userName);
80         System.out.println("passWord = " + passWord);
81         
82         System.out.println("Number of Account: " + ACCT);
83         System.out.println("Number of Item : " + ITEM);
84         System.out.println("Number of Holding: " + HOLDING);
85
86         try
87         {
88             System.out.println("To get a connection");
89             init();
90
91             System.out.println("To create stmt");
92             stmt = con.createStatement();
93             
94             System.out.println("To seed the database...");
95             population();
96
97             con.close();
98             System.out.println("\n....................done!");
99         }
100         catch (SQLException se)
101         {
102             System.err.println("get SQL Exception: " + se);
103             return;
104         }
105     }
106
107     private void population() throws SQLException
108     {
109         cleanUp();
110
111         seedAccount();
112         seedStockItem();
113         seedStockHolding();
114     }
115
116     private void cleanUp() throws SQLException
117     {
118         System.out.println("Clean up stockonline database...");
119
120         sql = "truncate table StockTransaction";
121         System.out.println(sql);
122         stmt.executeUpdate(sql);
123         
124         sql = "truncate Table StockHolding";
125         System.out.println(sql);
126                 stmt.executeUpdate(sql);
127
128         sql = "truncate table SubAccount";
129         System.out.println(sql);
130         stmt.executeUpdate(sql);
131
132         sql = "truncate table StockItem";
133         System.out.println(sql);
134         stmt.executeUpdate(sql);
135         
136         try {
137             sql = "Drop sequence subaccountseq";
138             System.out.println(sql);
139             stmt.executeUpdate(sql);
140             
141         } catch (Exception JavaDoc e) {};
142              
143         try {
144             sql = "Drop sequence StockTransactionSeq";
145             System.out.println(sql);
146             stmt.executeUpdate(sql);
147         } catch (Exception JavaDoc e) {};
148         
149         sql = "Create sequence subaccountseq increment by 1 START WITH 3001 MAXVALUE 10000000 NOCYCLE CACHE 10000";
150         System.out.println(sql);
151         stmt.executeUpdate(sql);
152         
153         sql = "Create sequence StockTransactionSeq increment by 1 START WITH 1 MAXVALUE 10000000 NOCYCLE CACHE 10000";
154         System.out.println(sql);
155         stmt.executeUpdate(sql);
156     }
157
158     private void seedAccount() throws SQLException
159     {
160         System.out.println("Populating Account with "+ACCT+" records ...");
161
162         for (int i=1; i<=ACCT; i++)
163         {
164             sql = "INSERT INTO subaccount VALUES("
165                                    + i + ",'"
166                                    + RandGenerator.randomStrFixLen(3) + "','"
167                                    + RandGenerator.randomStrChaLen(i) + "',"
168                                    + RandGenerator.randomInt(100000, 1000000)
169                                    + ")";
170             if(verbose)
171                 if(i%100==0) System.out.println(sql);
172             stmt.executeUpdate(sql);
173         }
174     }
175
176     private void seedStockItem() throws SQLException
177     {
178
179             System.out.println("Populating StockItem with "+ITEM+" records ...");
180             for (int i=1; i<=ITEM; i++)
181             {
182                 String JavaDoc code = "C" + i;
183
184                 String JavaDoc sql = "INSERT INTO StockItem VALUES("
185                                    + i + ",'"
186                                    + RandGenerator.randomStrFixLen(5) + "','"
187                                    + code + "',"
188                                    + RandGenerator.randomFloat(10.0F, 20.0F) + ","
189                                    + RandGenerator.randomFloat(20.0F, 30.0F) + ","
190                                    + RandGenerator.randomFloat(10.0F, 20.0F)
191                                    + ")";
192                 if(verbose)
193                     if(i%100==0) System.out.println(sql);
194                 stmt.executeUpdate(sql);
195             }
196     }
197
198     private void seedStockHolding() throws SQLException
199     {
200         int stock[] = new int[HOLDING+1];
201         int stockID, amount;
202         boolean not_hold;
203         String JavaDoc sql = "INSERT INTO StockHolding VALUES(?,?,?)";
204
205                 System.out.println("Populating StockHolding with "+HOLDING+" records per account ...");
206                 PreparedStatement pstmt = con.prepareStatement(sql);
207
208                 for (int i=1; i<=ACCT; i++)
209                 {
210                     for (int k=1; k<=HOLDING; k++) stock[k] = -1;
211                     
212                     for (int j=1; j<=HOLDING; )
213                     {
214                         stockID = RandGenerator.randomInt(1, ITEM);
215                         not_hold = true;
216
217                         for(int k=1; k<=j; k++)
218                         {
219                             if(stockID==stock[k])
220                             {
221                                 not_hold = false;
222                                 break;
223                             }
224                         }
225
226                         if(not_hold)
227                         {
228                             stock[j++] = stockID;
229                             amount = RandGenerator.randomInt(1000,10000);
230                             pstmt.setInt(1, i);
231                             pstmt.setInt(2, stockID);
232                             pstmt.setInt(3, amount);
233
234                             if(verbose)
235                             if(i%100==0) System.out.println(sql + " with " + i + ", " + stockID + ", " + amount);
236                             pstmt.executeUpdate();
237                         }
238                     }
239                 }
240         }
241
242     public static void main(String JavaDoc[] args) throws Exception JavaDoc
243     {
244         if (args.length<3)
245         {
246             System.out.println("Usage: java stockonline.datatbase.oracle.seed dbURL userName passWord");
247             System.out.println("where: \n\t dbURL = jdbc:oracle:thin:@hostname:1521:SID");
248             return;
249         }
250         
251         new Seed(args);
252     }
253
254     public void init()
255     {
256         System.out.println("init() called");
257
258         try
259         {
260             System.out.println("To register the driver");
261             DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
262             System.out.println("To register the driver.......................OK");
263         }
264         catch(SQLException ex)
265         {
266             String JavaDoc msg = "Fail to register the JDBC Driver";
267             System.out.println(msg);
268         }
269
270         try
271         {
272             System.out.println("To get a JDBC connecion");
273             con = DriverManager.getConnection(dbURL,userName, passWord);
274             System.out.println("To get a JDBC connection..........................OK");
275         }
276         catch(SQLException ex)
277         {
278             String JavaDoc msg = "Fail to get and set a JDBC connection";
279             System.out.println(msg);
280         }
281     }
282
283 }
284
Popular Tags