-
Notifications
You must be signed in to change notification settings - Fork 1
/
DataBase.java
123 lines (105 loc) · 4.27 KB
/
DataBase.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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
package com.salimov.yurii.lesson06.task03;
import java.sql.*;
public final class DataBase {
private static DataBase dataBase;
private Connection connection;
private DataBase() {
}
public static DataBase getInstance() {
if (dataBase == null) {
dataBase = new DataBase();
}
return dataBase;
}
public void connect(
final String url,
final String user,
final String password
) throws SQLException {
if (this.connection == null) {
this.connection = DriverManager.getConnection(url, user, password);
initialization();
}
}
public void disconnect() throws SQLException {
if (this.connection != null) {
this.connection.close();
}
}
public void initialization() throws SQLException {
try (Statement st = connection.createStatement()) {
st.execute("DROP TABLE IF EXISTS Clients");
st.execute("CREATE TABLE Clients (id INT NOT NULL AUTO_INCREMENT PRIMARY KEY, " +
"name VARCHAR(20) NOT NULL, " +
"surname VARCHAR(20) DEFAULT NULL, " +
"phone VARCHAR(20) NOT NULL);");
st.execute("DROP TABLE IF EXISTS Products");
st.execute("CREATE TABLE Products (id INT NOT NULL AUTO_INCREMENT PRIMARY KEY, " +
"name VARCHAR(20) NOT NULL, " +
"price DOUBLE(10,2) NOT NULL, " +
"information VARCHAR(128) DEFAULT NULL);");
st.execute("DROP TABLE IF EXISTS Orders");
st.execute("CREATE TABLE Orders (id INT NOT NULL AUTO_INCREMENT PRIMARY KEY, " +
"clientId INT NOT NULL, " +
"productId INT NOT NULL, " +
"comment VARCHAR(128) DEFAULT NULL);");
}
}
public void insertClient(final Client client) throws SQLException {
final String sql = "INSERT INTO Clients (name, surname, phone) VALUES(?, ?, ?);";
try (PreparedStatement ps = this.connection.prepareStatement(sql)) {
ps.setString(1, client.getName());
ps.setString(2, client.getSurname());
ps.setString(3, client.getPhone());
ps.executeUpdate();
}
System.out.println(client);
}
public void insertProduct(final Product product) throws SQLException {
final String sql = "INSERT INTO Products (name, price, information) VALUES(?, ?, ?);";
try (PreparedStatement ps = this.connection.prepareStatement(sql)) {
ps.setString(1, product.getName());
ps.setDouble(2, product.getPrice());
ps.setString(3, product.getInformation());
ps.executeUpdate();
}
System.out.println(product);
}
public void insertOrder(final Order order) throws SQLException {
String sql = "INSERT INTO Orders (clientId, productId, comment) VALUES(?, ?, ?);";
try (PreparedStatement ps = this.connection.prepareStatement(sql)) {
ps.setInt(1, order.getClientId());
ps.setInt(2, order.getProductId());
ps.setString(3, order.getComment());
ps.executeUpdate();
}
}
public String getClientsTable() throws SQLException {
return getTable("Clients");
}
public String getProductsTable() throws SQLException {
return getTable("Products");
}
public String getOrdersTable() throws SQLException {
return getTable("Orders");
}
private String getTable(String tableName) throws SQLException {
final StringBuilder sb = new StringBuilder();
final String sql = "SELECT * FROM " + tableName + ";";
try (PreparedStatement ps = connection.prepareStatement(sql);
ResultSet rs = ps.executeQuery()) {
ResultSetMetaData md = rs.getMetaData();
for (int i = 1; i <= md.getColumnCount(); i++) {
sb.append(md.getColumnName(i)).append("\t\t");
}
sb.append("\n");
while (rs.next()) {
for (int i = 1; i <= md.getColumnCount(); i++) {
sb.append(rs.getString(i)).append("\t\t");
}
sb.append("\n");
}
}
return sb.toString();
}
}