Advertisement
Guest User

Untitled

a guest
Apr 3rd, 2017
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.79 KB | None | 0 0
  1. /*
  2. * To change this license header, choose License Headers in Project Properties.
  3. * To change this template file, choose Tools | Templates
  4. * and open the template in the editor.
  5. */
  6. package datageneration;
  7.  
  8. import java.sql.*;
  9. import java.util.concurrent.TimeUnit;
  10.  
  11. /**
  12. * @author Theodor Sandström
  13. */
  14.  
  15. public class DataGeneration {
  16.  
  17. // DB connection variable
  18. static protected Connection con;
  19.  
  20. private String mysqlURL = "jdbc:mysql:///KOD";
  21. private String mysqlDriver = "com.mysql.jdbc.Driver";
  22. private String userID = "root";
  23. private String password = "sudo";
  24.  
  25. int size = 2;
  26.  
  27. public void standardConnect()
  28. {
  29. try
  30. {
  31. // register the driver with DriverManager
  32. Class.forName(mysqlDriver);
  33. //create a connection to the database
  34. con = DriverManager.getConnection(mysqlURL, userID, password);
  35. // Set the auto commit of the connection to false.
  36. // An explicit commit will be required in order to accept
  37. // any changes done to the DB through this connection.
  38. con.setAutoCommit(false);
  39. //Some logging
  40. System.out.println("Connected to " + mysqlURL + " using "+ mysqlDriver);
  41. }
  42. catch (Exception e)
  43. {
  44. e.printStackTrace();
  45. }
  46. }
  47.  
  48. public static void main(String[] argv)throws Exception{
  49.  
  50. DataGeneration me = new DataGeneration();
  51.  
  52. System.out.println("trying connection...");
  53. me.standardConnect();
  54. System.out.println("conection attempted");
  55.  
  56. String clearQuery="DELETE FROM Activity";
  57. Statement clearStatement = con.createStatement();
  58. clearStatement.executeUpdate(clearQuery);
  59.  
  60. con.commit();
  61.  
  62. String insertQuery = "INSERT INTO Activity (Device, Data_amount) VALUES(?, ?)";
  63. PreparedStatement insertStatement = con.prepareStatement(insertQuery);
  64.  
  65.  
  66. for (int i=1; i<me.size; i++){
  67. double rand = Math.random();
  68. if(rand<0.25){
  69. insertStatement.setString(1, "Linux");
  70. }
  71. else if(rand<0.5){
  72. insertStatement.setString(1, "Windows");
  73. }
  74. else if(rand<0.75){
  75. insertStatement.setString(1, "Android");
  76. }
  77. else{
  78. insertStatement.setString(1, "iPhone");
  79. }
  80. long data = 100000 + (long)(Math.random()*200000);
  81. insertStatement.setLong(2, data);
  82. insertStatement.executeUpdate();
  83. con.commit();
  84. TimeUnit.SECONDS.sleep((long)(Math.random()*2));
  85. }
  86.  
  87. con.close();
  88.  
  89. }
  90.  
  91. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement