Программа на Java для заполнения БД
Материал из SEWiki
package ru.aftu; public class Sample2 { public static final String[] REGIONS = {"MSK", "EKB", "SPB"}; public static final int SHOP_PER_REGION=3; public static final int SELLERS_PER_SHOP=3; public static final String[] FIRST_NAMES = {"Anna", "Maria", "Elena", "Alexandra", "Viktoria", "Irina", "Alena"}; public static final String[] LAST_NAMES = {"Ivanova", "Petrova", "Sidorova", "Kuznetsova", "Lebedeva", "Zaiceva", "Medvedeva"}; public static final String[] SEQUENCES = {"item_id_seq", "receipt_id_seq", "seller_id_seq", "shop_id_seq"}; public static final String[][] ITEMS = {{"Danone","Youghurt"},{"Valio","Youghurt"}, {"Valio", "Butter"}, {"Pork", "Meat"}, {"Beef", "Meat"}}; public static final int RECEIPT_COUNT=1500; public static final int CLIENT_COUNT=100; public static void main(String[] args) { for (int i =0; i<SEQUENCES.length; i++) { System.out.println("SELECT setval('"+SEQUENCES[i]+"', 1, false);"); } System.out.println("TRUNCATE TABLE shop CASCADE;"); System.out.println("TRUNCATE TABLE item CASCADE;"); for (int i=0; i < REGIONS.length; i++) { for (int j =0; j < SHOP_PER_REGION; j++) { System.out.println("INSERT INTO shop (region) values ('"+ REGIONS[i] + "');"); } } int shop_count = REGIONS.length * SHOP_PER_REGION; for (int i = 0; i < shop_count; i++) { for (int j = 0; j < SELLERS_PER_SHOP; j++) { System.out.println("INSERT INTO seller(name, shop_id) VALUES ('"+getName(i * SELLERS_PER_SHOP + j)+"', "+(i+1)+");"); } } int seller_count =shop_count * SELLERS_PER_SHOP; for (int i=0; i< ITEMS.length; i++) { System.out.println("INSERT INTO item (name, type, price) VALUES ('"+ITEMS[i][0]+"','"+ITEMS[i][1]+"',"+ITEMS[i][0].length()+");"); } // checks generation for (int i = 0; i < RECEIPT_COUNT; i++) { // card, seller, time System.out.println("INSERT INTO receipt(discount_card, seller_id, time) VALUES ('"+rand(CLIENT_COUNT)+"', "+rand(seller_count)+", current_timestamp);"); for (int j = 0; j < ITEMS.length; j++) { int count = rand(3) - 1; if (count > 0) { System.out.println("INSERT INTO receipt_item (receipt_id, item_id, amount) VALUES ("+(i+1)+", "+(j+1)+", "+count+");"); } } } } private static int rand(int count) { return (int)Math.floor(Math.random() * count) + 1; } private static String getName(int i) { return FIRST_NAMES[i % FIRST_NAMES.length] + " " + LAST_NAMES[ (i / FIRST_NAMES.length) % LAST_NAMES.length ]; } }