Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.sql.*;
- import java.util.Random;
- public class test_unit {
- public test_unit(int n) {
- try {
- Class.forName("org.postgresql.Driver");
- String url = "jdbc:postgresql://localhost:5432/Purchases";
- String user = "postgres";
- String passwd = "postgres";
- Connection conn = DriverManager.getConnection(url, user, passwd);
- int i = 0;
- int customer_id;
- long date; //date on milliseconds
- float price;
- String shop_c; //all categories of shops are in an array
- String shop_n; //all shop's name are in an array
- String customer_name; //registered customers
- Random r = new Random();
- Random r1 = new Random(1515135600); //those two are needed to get a long random betwen two values
- Random r2 = new Random(1514790000); //
- while (i< n) {
- customer_id = r.nextInt();
- date = r2.nextLong() - r1.nextLong();
- price = r.nextFloat();
- String query = "INSERT INTO purchases (Customer_ID, Date_b, Price, Shop_category, Shop_name, Customer)"
- + " values (?, ?, ?, ?, ?, ?)";
- PreparedStatement prepared = conn.prepareStatement(query);
- prepared.setInt(1, customer_id);
- prepared.setDate(2, new java.sql.Date(date));
- prepared.setFloat(3, price);
- prepared.setString(4, shop_c);
- prepared.setString(5, shop_n);
- prepared.setString(6, customer_name);
- }
- } catch (Exception e) {
- e.getCause();
- }
- }
- }
Add Comment
Please, Sign In to add comment