-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPostgreSQLDatabaseConnection.java
107 lines (86 loc) · 3.59 KB
/
PostgreSQLDatabaseConnection.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
/*
* 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 cz.terner.postgesqlsample;
import java.util.List;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.ArrayList;
import org.apache.commons.dbutils.DbUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.ResultSetHandler;
import org.apache.commons.dbutils.handlers.BeanHandler;
/**
*
* @author terner
*/
public class PostgreSQLDatabaseConnection {
//JDBC driver name and database URL
static final String JDBC_DRIVER = "org.postgresql.Driver";
static final String DB_URL = "jdbc:postgresql://localhost:5432/library";
static final String USER = "postgres";
static final String PASS = "user";
//records to generate
static final int RECORDS = 300000;
//records to store in database
private static List<VssItemDemo> vid;
public static void main(String[] args) throws SQLException {
//prepare records of VssItemDemo
vid = new ArrayList<>();
VssItemGenerator vig = new VssItemGenerator(RECORDS);
long startList = System.currentTimeMillis();
vid = vig.getViList();
long endList = System.currentTimeMillis();
//zkouska viListu
// for (VssItemDemo vss : vid) {
// System.out.println("ID: " + vss.getVssuid() + "\tTeilNummer: " + vss.getTeilnummer() +
// "\tType: " + vss.getVsstype().name() +
// "\tEgew: " + vss.getEgew() + "\tGgew: " + vss.getGgew());
// }
// if (vid.isEmpty()) {
//
// } else {
// return;
// }
Connection conn = null;
QueryRunner qr = new QueryRunner();
DbUtils.loadDriver(JDBC_DRIVER);
//Open connection
System.out.println("Connecting to database...");
conn = DriverManager.getConnection(DB_URL, USER, PASS);
//ResultSetHandler<VssItemDemo> rh = new BeanHandler<>(VssItemDemo.class);
long start = System.currentTimeMillis();
int rec = 0;
try {
for (VssItemDemo vss : vid) {
rec += qr.update(conn,
"INSERT INTO vssitem(teilnummer,vsstype,egew,ggew,vssuid) VALUES (?,?,?,?,?)",
vss.getTeilnummer(),
vss.getVsstype().name(),
vss.getEgew(),
vss.getGgew(),
vss.getVssuid()
);
if (rec % 5000 == 0) {
System.out.println("Stored: " + rec + " records into database.");
}
}
//int ir = qr.update(conn,
//"INSERT INTO books(title,author_id) VALUES (?,?)",
//"Kákona zpívá", 63);
//System.out.println("Records inserted: " + ir);
} catch (Exception e) {
System.err.println(e.getMessage());
} finally {
DbUtils.close(conn);
}
long end = System.currentTimeMillis();
System.out.println("Time list: " + (endList - startList));
System.out.println("Time: " + (end - start));
System.out.println("Records generated: " + RECORDS);
System.out.println("Time need for writting 1 record to db: " + ((double) (end - start) / RECORDS));
}
}