Advertisement
VladislavSavvateev

Untitled

Sep 29th, 2021
853
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.00 KB | None | 0 0
  1. package ru.savok;
  2.  
  3. import java.io.*;
  4. import java.sql.*;
  5. import java.time.LocalDateTime;
  6. import java.util.ArrayList;
  7. import java.util.List;
  8. import java.util.Properties;
  9. import java.util.Random;
  10.  
  11. public class Main {
  12.  
  13.     public static void main(String[] args) throws ClassNotFoundException, SQLException, IOException {
  14.         Class.forName("org.postgresql.Driver");
  15.  
  16.         String url = "jdbc:postgresql://localhost/savok";
  17.         Properties props = new Properties();
  18.         props.setProperty("user", "savok");
  19.         props.setProperty("password", "12345678");
  20.         Connection con = DriverManager.getConnection(url, props);
  21.  
  22.         createTable(con);
  23.  
  24.         List<Shit> shits = getShitFromDb(con);
  25.  
  26.         con.close();
  27.     }
  28.  
  29.     public static void createTable(Connection con) throws SQLException {
  30.         Statement stmt = con.createStatement();
  31.         stmt.execute("create table if not exists test.things(id bigserial constraint things_pk primary key, data bytea);");
  32.     }
  33.  
  34.     public static void placeShitInDb(Connection con, List<Shit> shits) throws SQLException, IOException {
  35.         PreparedStatement stmt = con.prepareStatement("insert into test.things(data) values (?) returning id");
  36.  
  37.         for (Shit shit : shits) {
  38.             ByteArrayOutputStream baos = new ByteArrayOutputStream();
  39.             ObjectOutputStream oos = new ObjectOutputStream(baos);
  40.             oos.writeObject(shit);
  41.  
  42.             stmt.setBytes(1, baos.toByteArray());
  43.             ResultSet rs = stmt.executeQuery();
  44.             rs.next();
  45.             shit.setId(rs.getLong(1));
  46.         }
  47.     }
  48.  
  49.     public static List<Shit> getShitFromDb(Connection con) throws SQLException, IOException, ClassNotFoundException {
  50.         ArrayList<Shit> result = new ArrayList<>();
  51.  
  52.         Statement stmt = con.createStatement();
  53.         ResultSet rs = stmt.executeQuery("select id, data from test.things");
  54.         while (rs.next()) {
  55.             ByteArrayInputStream bais = new ByteArrayInputStream(rs.getBytes(2));
  56.             ObjectInputStream ois = new ObjectInputStream(bais);
  57.             Object obj = ois.readObject();
  58.             if (obj instanceof Shit) {
  59.                 Shit shit = (Shit)obj;
  60.                 shit.setId(rs.getLong(1));
  61.                 result.add(shit);
  62.             }
  63.         }
  64.  
  65.         return result;
  66.     }
  67.  
  68.     private static Random random = new Random();
  69.     public static String generateRandomString() {
  70.         StringBuilder sb = new StringBuilder();
  71.         for (int i = 0; i < 16; i++) sb.append(alphabet.charAt(random.nextInt(alphabet.length())));
  72.         return sb.toString();
  73.     }
  74.  
  75.     private static final String alphabet = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
  76.  
  77.     public static ArrayList<Shit> generateALotOfShit() {
  78.         ArrayList<Shit> result = new ArrayList<>();
  79.         for (int i = 0; i < 1000; i++) {
  80.             Shit shit = new Shit();
  81.             Piss piss = new Piss();
  82.  
  83.             piss.setUhh(generateRandomString());
  84.             piss.setTime(LocalDateTime.now());
  85.             shit.setWhat(generateRandomString());
  86.             shit.setPiss(piss);
  87.  
  88.             result.add(shit);
  89.         }
  90.  
  91.         return result;
  92.     }
  93.  
  94.     public static class Shit implements Serializable {
  95.         private long id;
  96.         private String what;
  97.         private Piss piss;
  98.  
  99.         public long getId() {return id;}
  100.         public String getWhat() {return what;}
  101.         public Piss getPiss() {return piss;}
  102.  
  103.         public void setId(long id) {this.id = id;}
  104.         public void setPiss(Piss piss) {this.piss = piss;}
  105.         public void setWhat(String what) {this.what = what;}
  106.     }
  107.  
  108.     public static class Piss implements Serializable {
  109.         private String uhh;
  110.         private LocalDateTime time;
  111.  
  112.         public String getUhh() {return uhh;}
  113.         public LocalDateTime getTime() {return time;}
  114.  
  115.         public void setUhh(String uhh) {this.uhh = uhh;}
  116.         public void setTime(LocalDateTime time) {this.time = time;}
  117.     }
  118. }
  119.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement