Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package ru.savok;
- import java.io.*;
- import java.sql.*;
- import java.time.LocalDateTime;
- import java.util.ArrayList;
- import java.util.List;
- import java.util.Properties;
- import java.util.Random;
- public class Main {
- public static void main(String[] args) throws ClassNotFoundException, SQLException, IOException {
- Class.forName("org.postgresql.Driver");
- String url = "jdbc:postgresql://localhost/savok";
- Properties props = new Properties();
- props.setProperty("user", "savok");
- props.setProperty("password", "12345678");
- Connection con = DriverManager.getConnection(url, props);
- createTable(con);
- List<Shit> shits = getShitFromDb(con);
- con.close();
- }
- public static void createTable(Connection con) throws SQLException {
- Statement stmt = con.createStatement();
- stmt.execute("create table if not exists test.things(id bigserial constraint things_pk primary key, data bytea);");
- }
- public static void placeShitInDb(Connection con, List<Shit> shits) throws SQLException, IOException {
- PreparedStatement stmt = con.prepareStatement("insert into test.things(data) values (?) returning id");
- for (Shit shit : shits) {
- ByteArrayOutputStream baos = new ByteArrayOutputStream();
- ObjectOutputStream oos = new ObjectOutputStream(baos);
- oos.writeObject(shit);
- stmt.setBytes(1, baos.toByteArray());
- ResultSet rs = stmt.executeQuery();
- rs.next();
- shit.setId(rs.getLong(1));
- }
- }
- public static List<Shit> getShitFromDb(Connection con) throws SQLException, IOException, ClassNotFoundException {
- ArrayList<Shit> result = new ArrayList<>();
- Statement stmt = con.createStatement();
- ResultSet rs = stmt.executeQuery("select id, data from test.things");
- while (rs.next()) {
- ByteArrayInputStream bais = new ByteArrayInputStream(rs.getBytes(2));
- ObjectInputStream ois = new ObjectInputStream(bais);
- Object obj = ois.readObject();
- if (obj instanceof Shit) {
- Shit shit = (Shit)obj;
- shit.setId(rs.getLong(1));
- result.add(shit);
- }
- }
- return result;
- }
- private static Random random = new Random();
- public static String generateRandomString() {
- StringBuilder sb = new StringBuilder();
- for (int i = 0; i < 16; i++) sb.append(alphabet.charAt(random.nextInt(alphabet.length())));
- return sb.toString();
- }
- private static final String alphabet = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
- public static ArrayList<Shit> generateALotOfShit() {
- ArrayList<Shit> result = new ArrayList<>();
- for (int i = 0; i < 1000; i++) {
- Shit shit = new Shit();
- Piss piss = new Piss();
- piss.setUhh(generateRandomString());
- piss.setTime(LocalDateTime.now());
- shit.setWhat(generateRandomString());
- shit.setPiss(piss);
- result.add(shit);
- }
- return result;
- }
- public static class Shit implements Serializable {
- private long id;
- private String what;
- private Piss piss;
- public long getId() {return id;}
- public String getWhat() {return what;}
- public Piss getPiss() {return piss;}
- public void setId(long id) {this.id = id;}
- public void setPiss(Piss piss) {this.piss = piss;}
- public void setWhat(String what) {this.what = what;}
- }
- public static class Piss implements Serializable {
- private String uhh;
- private LocalDateTime time;
- public String getUhh() {return uhh;}
- public LocalDateTime getTime() {return time;}
- public void setUhh(String uhh) {this.uhh = uhh;}
- public void setTime(LocalDateTime time) {this.time = time;}
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement