Guest User

Untitled

a guest
May 6th, 2018
256
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.47 KB | None | 0 0
  1. import java.sql.*;
  2. import java.util.Random;
  3.  
  4. public class test_unit {
  5. public test_unit(int n) {
  6.  
  7. try {
  8.  
  9. Class.forName("org.postgresql.Driver");
  10.  
  11. String url = "jdbc:postgresql://localhost:5432/Purchases";
  12. String user = "postgres";
  13. String passwd = "postgres";
  14.  
  15. Connection conn = DriverManager.getConnection(url, user, passwd);
  16.  
  17. int i = 0;
  18.  
  19. int customer_id;
  20. long date; //date on milliseconds
  21. float price;
  22. String shop_c; //all categories of shops are in an array
  23. String shop_n; //all shop's name are in an array
  24. String customer_name; //registered customers
  25.  
  26. Random r = new Random();
  27. Random r1 = new Random(1515135600); //those two are needed to get a long random betwen two values
  28. Random r2 = new Random(1514790000); //
  29.  
  30. while (i< n) {
  31.  
  32. customer_id = r.nextInt();
  33. date = r2.nextLong() - r1.nextLong();
  34. price = r.nextFloat();
  35.  
  36. String query = "INSERT INTO purchases (Customer_ID, Date_b, Price, Shop_category, Shop_name, Customer)"
  37. + " values (?, ?, ?, ?, ?, ?)";
  38. PreparedStatement prepared = conn.prepareStatement(query);
  39. prepared.setInt(1, customer_id);
  40. prepared.setDate(2, new java.sql.Date(date));
  41. prepared.setFloat(3, price);
  42. prepared.setString(4, shop_c);
  43. prepared.setString(5, shop_n);
  44. prepared.setString(6, customer_name);
  45. }
  46.  
  47.  
  48. } catch (Exception e) {
  49. e.getCause();
  50. }
  51.  
  52. }
  53. }
Add Comment
Please, Sign In to add comment