2010-03-03 2 views
0

j'ai le tableau suivantnombres aléatoires en java et à la création de tables dans MySQL

select * from consumer6; 

Service_ID | Service_Type | consumer_feedback | 

    93   Computing       1 
    93   Computing       1 
    93   Computing       1 
    93   Computing        1 
    93   Computing        1 
    93   Computing        1 
    93   Computing        1 
    93   Computing        1 
    93   Computing        1 
    93   Computing        1 
    93   Computing        1 
    93   Computing        1 
    93  Computing        1 
    93   Computing        1 
    93   Computing       1 
    93   Computing        1     
    93   Computing        1 
    93   Computing        1 
    93   Computing        1    
    93  Computing        1 
    70   Data          

select * from consumer5; 

Service_ID   Service_Type    consumer_feedback 
    89   Computing      -1 

Je veux créer 100 tables pour mes project.I ont Service_Types (données, informatique, imprimante) .Je me sers du fonctions aléatoires

int min1=31;//printer 
    int max1=40; 
    int min2=41;//data 
    int max2 =70; 
    int min3=71; //computing 
    int max3=75; 
    int min = 1; 
int max = 9; 
    int min4=1; 
    int max4=30; //consumer 

    int min5 = -1; 
    int max5 =1; 

    int feedback = (int) (Math.random() * (max5 - min5 + 1)) + min5; 


    int number = (int) (Math.random() * (max - min + 1)) + min;  

    int provider1 = (int) (Math.random() * (max1 - min1 + 1)) + min1; 

    int provider2 = (int) (Math.random() * (max2 - min2 + 1)) + min2; 
    int provider3 = (int) (Math.random() * (max3 - min3 + 1)) + min3; 
    int consumer= (int) (Math.random() * (max4 - min4 + 1)) + min4; 

J'ai besoin des types de services à distribuer dans 100 tables de telle sorte que tous les fournisseurs (31-40 imprimante, données 41-70, 71-75 Computing à distribuer uniformément avec des valeurs de rétroaction générant 1 plus Nombre de fois.S'il vous plaît aidez-moi

+4

100 tables, en utilisant les mêmes colonnes, fait un * vraiment * pauvre modèle de données. Ce devrait être une seule table. –

+0

je crois que ce n'est pas toujours le cas ... Parfois, il est préférable de répartir les lignes d'une table unique en plusieurs tables différentes, disons que pour l'exécution de la requête plus rapide. mais oui, il a son propre ensemble d'inconvénients ... juste une idée! – ultrajohn

+1

La plupart des bases de données vous permettent de partitionner des tables sur plusieurs périphériques de stockage. Vous n'avez donc pas besoin de le faire manuellement. Une règle générale de conception de base de données est de ne pas diviser une table en deux juste parce que le nombre de lignes est élevé. – e4c5

Répondre

0
public static void main(String[] args) { 

    for(int i=0;i<100;i++){ 
     String table = "table"+i; 
     createTableFor(table); //add your table creation info here, but I strongly suggest you to use a single table and add the consumer as a column 
     generateRandomData(table,10); 
    } 

} 

private static void generateRandomData(String table,int sample) { 
    Random rnd = new Random(); 
    int[] feedback = genMostlyPositiveFeedback(sample); //I am assuming you want positive feedback for all, not by service type 
    for(int i=0;i<sample;i++){ 
     int random = rnd.nextInt(75); 
     for(Range range:Range.values()){ 
      if (range.isInRange(random)){ 
       Object[] data = new Object[]{range,random,feedback[i]}; 
       System.out.println(Arrays.asList(data)); //replace this with a SQL Insert statement 
       break; 
      }    
     } 
    }  
} 

private static int[] genMostlyPositiveFeedback(int sample) { 
    boolean mostlyPositive = false; 
    Random rnd = new Random(); 
    int[] feedback = new int[sample]; 
    while(!mostlyPositive){ 
     for(int i=0;i<sample;i++){ 
      feedback[i] = rnd.nextInt(3) - 1; //generates -1, 0 and 1. If you need only -1 and 1, try nextBoolean() 
     } 
     mostlyPositive = checkIfItsMostlyPositive(feedback); 
    } 
    return feedback; 
} 

private static boolean checkIfItsMostlyPositive(int[] feedback) { 
    int countPositives = 0; 
    for(int i=0;i<feedback.length;i++){ 
     if (feedback[i] == 1){ 
      countPositives++; 
     } 
    } 
    return countPositives > (feedback.length/2 + 1); 
} 
Questions connexes