Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * To change this license header, choose License Headers in Project Properties.
- * To change this template file, choose Tools | Templates
- * and open the template in the editor.
- */
- package datageneration;
- import java.sql.*;
- import java.util.concurrent.TimeUnit;
- /**
- * @author Theodor Sandström
- */
- public class DataGeneration {
- // DB connection variable
- static protected Connection con;
- private String mysqlURL = "jdbc:mysql:///KOD";
- private String mysqlDriver = "com.mysql.jdbc.Driver";
- private String userID = "root";
- private String password = "sudo";
- int size = 2;
- public void standardConnect()
- {
- try
- {
- // register the driver with DriverManager
- Class.forName(mysqlDriver);
- //create a connection to the database
- con = DriverManager.getConnection(mysqlURL, userID, password);
- // Set the auto commit of the connection to false.
- // An explicit commit will be required in order to accept
- // any changes done to the DB through this connection.
- con.setAutoCommit(false);
- //Some logging
- System.out.println("Connected to " + mysqlURL + " using "+ mysqlDriver);
- }
- catch (Exception e)
- {
- e.printStackTrace();
- }
- }
- public static void main(String[] argv)throws Exception{
- DataGeneration me = new DataGeneration();
- System.out.println("trying connection...");
- me.standardConnect();
- System.out.println("conection attempted");
- String clearQuery="DELETE FROM Activity";
- Statement clearStatement = con.createStatement();
- clearStatement.executeUpdate(clearQuery);
- con.commit();
- String insertQuery = "INSERT INTO Activity (Device, Data_amount) VALUES(?, ?)";
- PreparedStatement insertStatement = con.prepareStatement(insertQuery);
- for (int i=1; i<me.size; i++){
- double rand = Math.random();
- if(rand<0.25){
- insertStatement.setString(1, "Linux");
- }
- else if(rand<0.5){
- insertStatement.setString(1, "Windows");
- }
- else if(rand<0.75){
- insertStatement.setString(1, "Android");
- }
- else{
- insertStatement.setString(1, "iPhone");
- }
- long data = 100000 + (long)(Math.random()*200000);
- insertStatement.setLong(2, data);
- insertStatement.executeUpdate();
- con.commit();
- TimeUnit.SECONDS.sleep((long)(Math.random()*2));
- }
- con.close();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement