1 32 33 34 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 String dbURL; 57 String userName; 58 String passWord; 59 60 int ACCT = 3000; int ITEM = 3000; int HOLDING = 10; 66 Connection con; 67 Statement stmt; 68 String sql; 69 70 public Seed(String [] args) throws Exception 71 { 72 dbURL = new String (args[0]); 73 userName = new String (args[1]); 74 passWord = new String (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 e) {}; 142 143 try { 144 sql = "Drop sequence StockTransactionSeq"; 145 System.out.println(sql); 146 stmt.executeUpdate(sql); 147 } catch (Exception 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 code = "C" + i; 183 184 String 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 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 [] args) throws Exception 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 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 msg = "Fail to get and set a JDBC connection"; 279 System.out.println(msg); 280 } 281 } 282 283 } 284 | Popular Tags |