Skip to content

Commit eb215e6

Browse files
committed
HW13, SqlHelper for implementation
1 parent 334d878 commit eb215e6

5 files changed

Lines changed: 51 additions & 24 deletions

File tree

src/ru/javaops/webapp/Config.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
package ru.javaops.webapp;
22

3+
import ru.javaops.webapp.storage.IStorage;
4+
import ru.javaops.webapp.storage.SqlStorage;
5+
36
import java.io.*;
47
import java.util.Properties;
58

@@ -8,6 +11,7 @@ public class Config {
811
private static final Config INSTANCE = new Config();
912

1013
private File storageDir;
14+
private IStorage storage;
1115

1216
public static Config getInstance() {
1317
return INSTANCE;
@@ -18,6 +22,7 @@ private Config() {
1822
Properties prop = new Properties();
1923
prop.load(is);
2024
storageDir = new File(prop.getProperty("storage.dir"));
25+
storage = new SqlStorage(prop.getProperty("db.url"), prop.getProperty("db.user"), prop.getProperty("db.password"));
2126
} catch (IOException e) {
2227
throw new IllegalStateException("Invalid config file " + PROPS.getAbsolutePath());
2328
}
@@ -26,4 +31,8 @@ private Config() {
2631
public File getStorageDir() {
2732
return storageDir;
2833
}
34+
35+
public IStorage getStorage() {
36+
return storage;
37+
}
2938
}

src/ru/javaops/webapp/MainConcurrancy.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ public void run() {
6060

6161
//final Object lock2 = new Object();
6262
final MainConcurrancy mainConcurancy = new MainConcurrancy();
63-
CountDownLatch latch = new CountDownLatch(THREADS_COUNT);
63+
//CountDownLatch latch = new CountDownLatch(THREADS_COUNT);
6464
//Создать кол-во потоков равное кол-ву ядер на ПК/Сервере
6565
// ExecutorService executorService = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors());
6666
ExecutorService executorService = Executors.newCachedThreadPool();
@@ -73,7 +73,7 @@ public void run() {
7373
mainConcurancy.inc();
7474
//System.out.println(threadLocal.get().format(new Date()));
7575
}
76-
latch.countDown();
76+
//latch.countDown();
7777
return 5;
7878
}
7979
});
@@ -108,7 +108,7 @@ public void run() {
108108
// e.printStackTrace();
109109
// }
110110
// });
111-
latch.await(10, TimeUnit.SECONDS);
111+
//latch.await(10, TimeUnit.SECONDS);
112112
executorService.shutdown();
113113
//Thread.sleep(500);
114114
LazySingleton.getInstance();
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package ru.javaops.webapp.sql;
2+
3+
public class SqlHelper {
4+
5+
}

src/ru/javaops/webapp/storage/SqlStorage.java

Lines changed: 33 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import ru.javaops.webapp.sql.ConnectionFactory;
88

99
import java.sql.*;
10+
import java.util.ArrayList;
1011
import java.util.List;
1112

1213
public class SqlStorage implements IStorage {
@@ -18,8 +19,8 @@ public SqlStorage(String dbUrl, String dbUser, String dbPassword) {
1819

1920
@Override
2021
public void clear() {
21-
try(Connection conn = connectionFactory.getConnection();
22-
PreparedStatement ps = conn.prepareStatement("DELETE FROM resume")){
22+
try (Connection conn = connectionFactory.getConnection();
23+
PreparedStatement ps = conn.prepareStatement("DELETE FROM resume")) {
2324
ps.execute();
2425
} catch (SQLException e) {
2526
throw new StorageException(e);
@@ -29,10 +30,10 @@ public void clear() {
2930
@Override
3031
public Resume get(String uuid) {
3132
try (Connection conn = connectionFactory.getConnection();
32-
PreparedStatement ps = conn.prepareStatement("SELECT * FROM resume r WHERE r.uuid = ?")) {
33+
PreparedStatement ps = conn.prepareStatement("SELECT * FROM resume r WHERE r.uuid = ?")) {
3334
ps.setString(1, uuid);
3435
ResultSet rs = ps.executeQuery();
35-
if(!rs.next()) {
36+
if (!rs.next()) {
3637
throw new NotExistStorageException(uuid);
3738
}
3839
return new Resume(uuid, rs.getString("full_name"));
@@ -43,15 +44,16 @@ public Resume get(String uuid) {
4344

4445
@Override
4546
public void update(Resume resume) {
46-
if (get(resume.getUuid()).equals(resume)) {
47-
try (Connection conn = connectionFactory.getConnection();
48-
PreparedStatement ps = conn.prepareStatement("UPDATE resume SET full_name = ? WHERE uuid = ?")) {
49-
ps.setString(1, resume.getFullName());
50-
ps.setString(2, resume.getUuid());
51-
ps.executeUpdate();
52-
} catch (SQLException e) {
53-
throw new StorageException(e);
47+
try (Connection conn = connectionFactory.getConnection();
48+
PreparedStatement ps = conn.prepareStatement("UPDATE resume SET full_name = ? WHERE uuid = ?")) {
49+
ps.setString(1, resume.getFullName());
50+
ps.setString(2, resume.getUuid());
51+
if (ps.executeUpdate() == 0) {
52+
throw new NotExistStorageException(resume.getUuid());
5453
}
54+
} catch (SQLException e) {
55+
throw new StorageException(e);
56+
5557
}
5658
}
5759

@@ -69,20 +71,32 @@ public void save(Resume resume) {
6971

7072
@Override
7173
public void delete(String uuid) {
72-
if (get(uuid).getUuid().equals(uuid)) {
73-
try (Connection conn = connectionFactory.getConnection();
74-
PreparedStatement ps = conn.prepareStatement("DELETE FROM resume r WHERE r.uuid = ?")) {
75-
ps.setString(1, uuid);
76-
ps.executeUpdate();
77-
} catch (SQLException e) {
74+
try (Connection conn = connectionFactory.getConnection();
75+
PreparedStatement ps = conn.prepareStatement("DELETE FROM resume r WHERE r.uuid = ?")) {
76+
ps.setString(1, uuid);
77+
if (ps.executeUpdate() == 0) {
7878
throw new NotExistStorageException(uuid);
7979
}
80+
} catch (SQLException e) {
81+
throw new StorageException(e);
8082
}
8183
}
8284

8385
@Override
8486
public List<Resume> getAllSorted() {
85-
return null;
87+
try (Connection conn = connectionFactory.getConnection();
88+
PreparedStatement ps = conn.prepareStatement("SELECT uuid, full_name FROM resume ORDER BY uuid ASC ")) {
89+
ResultSet rs = ps.executeQuery();
90+
List<Resume> resumes = new ArrayList<>();
91+
while (rs.next()) {
92+
String uuid = rs.getString(1).replaceAll("\\s", "");
93+
String fullName = rs.getString(2);
94+
resumes.add(new Resume(uuid, fullName));
95+
}
96+
return resumes;
97+
} catch (SQLException e) {
98+
throw new StorageException(e);
99+
}
86100
}
87101

88102
@Override

test/ru/javaops/webapp/storage/SqlStorageTest.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66

77
public class SqlStorageTest extends AbstractStorageTest {
88
public SqlStorageTest() {
9-
//super(new SqlStorage(Config.getInstance().getStorageDir().toString(), "postgres", "postgres"));
10-
super(new SqlStorage("jdbc:postgresql://localhost:5432/resumes", "postgres", "postgres"));
9+
super(Config.getInstance().getStorage());
1110
}
1211
}

0 commit comments

Comments
 (0)