Skip to content

Commit 5fa7c6e

Browse files
committed
implemented jdbc example
1 parent fee755d commit 5fa7c6e

File tree

9 files changed

+404
-39
lines changed

9 files changed

+404
-39
lines changed

jdbc-demo/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
# JDBC demo
2-
Time-series data service using simple JDBC.
2+
Simple data service using JDBC.
33

44

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package itx.examples.jdbc.dto;
2+
3+
public class UserData {
4+
5+
private final String id;
6+
private final String email;
7+
private final String password;
8+
9+
public UserData(String id, String email, String password) {
10+
this.id = id;
11+
this.email = email;
12+
this.password = password;
13+
}
14+
15+
public String getId() {
16+
return id;
17+
}
18+
19+
public String getEmail() {
20+
return email;
21+
}
22+
23+
public String getPassword() {
24+
return password;
25+
}
26+
27+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package itx.examples.jdbc.services;
2+
3+
import java.sql.Connection;
4+
import java.sql.PreparedStatement;
5+
import java.sql.SQLException;
6+
7+
public final class DBUtils {
8+
9+
private DBUtils() {
10+
}
11+
12+
public static void initDb(Connection connection) throws DataServiceException {
13+
try {
14+
connection.setAutoCommit(false);
15+
String query = "CREATE TABLE users ( id varchar(255), email varchar(255), password varchar(255), PRIMARY KEY ( id ) )";
16+
PreparedStatement statement = connection.prepareStatement(query);
17+
statement.execute();
18+
connection.commit();
19+
} catch (SQLException e) {
20+
throw new DataServiceException(e);
21+
}
22+
}
23+
24+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package itx.examples.jdbc.services;
2+
3+
import itx.examples.jdbc.dto.UserData;
4+
5+
import java.util.Collection;
6+
import java.util.Optional;
7+
8+
/**
9+
* Synchronous data service implementation using transactions.
10+
* Each method call runs in own transaction.
11+
*/
12+
public interface DataService {
13+
14+
Optional<UserData> findById(String id) throws DataServiceException;
15+
16+
UserData save(String email, String password) throws DataServiceException;
17+
18+
Collection<UserData> findAll() throws DataServiceException;
19+
20+
void deleteById(String id) throws DataServiceException;
21+
22+
void update(UserData userData) throws DataServiceException;
23+
24+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package itx.examples.jdbc.services;
2+
3+
public class DataServiceException extends Exception {
4+
5+
public DataServiceException() {
6+
}
7+
8+
public DataServiceException(Throwable t) {
9+
super(t);
10+
}
11+
12+
}
Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
package itx.examples.jdbc.services;
2+
3+
import itx.examples.jdbc.dto.UserData;
4+
import org.slf4j.Logger;
5+
import org.slf4j.LoggerFactory;
6+
7+
import java.sql.Connection;
8+
import java.sql.PreparedStatement;
9+
import java.sql.ResultSet;
10+
import java.sql.SQLException;
11+
import java.util.ArrayList;
12+
import java.util.Collection;
13+
import java.util.Optional;
14+
import java.util.UUID;
15+
16+
public class DataServiceImpl implements DataService {
17+
18+
private static final Logger LOG = LoggerFactory.getLogger(DataServiceImpl.class);
19+
20+
private final Connection connection;
21+
22+
public DataServiceImpl(Connection connection) {
23+
this.connection = connection;
24+
disableAutocommit();
25+
}
26+
27+
@Override
28+
public Optional<UserData> findById(String id) throws DataServiceException {
29+
try {
30+
String query = "SELECT * FROM users WHERE id=?";
31+
PreparedStatement statement = connection.prepareStatement(query);
32+
statement.setString(1, id);
33+
ResultSet resultSet = statement.executeQuery();
34+
while (resultSet.next()) {
35+
String userId = resultSet.getString("id");
36+
String email = resultSet.getString("email");
37+
String password = resultSet.getString("password");
38+
return Optional.of(new UserData(userId, email, password));
39+
}
40+
this.connection.commit();
41+
return Optional.empty();
42+
} catch (SQLException e) {
43+
transactionRollback();
44+
throw new DataServiceException(e);
45+
}
46+
}
47+
48+
@Override
49+
public UserData save(String email, String password) throws DataServiceException {
50+
try {
51+
String query = "INSERT INTO users ( id, email, password ) VALUES ( ?, ?, ? )";
52+
String id = UUID.randomUUID().toString();
53+
PreparedStatement statement = connection.prepareStatement(query);
54+
statement.setString(1, id);
55+
statement.setString(2, email);
56+
statement.setString(3, password);
57+
statement.execute();
58+
this.connection.commit();
59+
return new UserData(id, email, password);
60+
} catch (SQLException e) {
61+
transactionRollback();
62+
throw new DataServiceException(e);
63+
}
64+
}
65+
66+
@Override
67+
public Collection<UserData> findAll() throws DataServiceException {
68+
Collection<UserData> userDataCollection = new ArrayList<>();
69+
try {
70+
String query = "SELECT * FROM users";
71+
PreparedStatement statement = connection.prepareStatement(query);
72+
ResultSet resultSet = statement.executeQuery();
73+
while (resultSet.next()) {
74+
String id = resultSet.getString("id");
75+
String email = resultSet.getString("email");
76+
String password = resultSet.getString("password");
77+
userDataCollection.add(new UserData(id, email, password));
78+
}
79+
this.connection.commit();
80+
} catch (SQLException e) {
81+
transactionRollback();
82+
throw new DataServiceException(e);
83+
}
84+
return userDataCollection;
85+
}
86+
87+
@Override
88+
public void deleteById(String id) throws DataServiceException {
89+
try {
90+
if (findById(id).isEmpty()) {
91+
throw new DataServiceException();
92+
}
93+
String query = "DELETE FROM users WHERE id=?";
94+
PreparedStatement statement = connection.prepareStatement(query);
95+
statement.setString(1, id);
96+
statement.execute();
97+
this.connection.commit();
98+
} catch (SQLException e) {
99+
transactionRollback();
100+
throw new DataServiceException(e);
101+
}
102+
}
103+
104+
@Override
105+
public void update(UserData userData) throws DataServiceException {
106+
try {
107+
if (findById(userData.getId()).isEmpty()) {
108+
throw new DataServiceException();
109+
}
110+
String query = "UPDATE users SET email=?, password=? WHERE id=?";
111+
PreparedStatement statement = connection.prepareStatement(query);
112+
statement.setString(1, userData.getEmail());
113+
statement.setString(2, userData.getPassword());
114+
statement.setString(3, userData.getId());
115+
statement.execute();
116+
this.connection.commit();
117+
} catch (SQLException e) {
118+
transactionRollback();
119+
throw new DataServiceException(e);
120+
}
121+
}
122+
123+
private void transactionRollback() {
124+
try {
125+
this.connection.rollback();
126+
} catch (SQLException e) {
127+
LOG.error("Transaction rollback failed", e);
128+
}
129+
}
130+
131+
private void disableAutocommit() {
132+
try {
133+
this.connection.setAutoCommit(false);
134+
} catch (SQLException e) {
135+
LOG.error("Autocommit = false failed", e);
136+
}
137+
}
138+
139+
}

0 commit comments

Comments
 (0)