Skip to content

Commit 5f430e4

Browse files
committed
Review comments
1 parent f383e4c commit 5f430e4

File tree

3 files changed

+68
-40
lines changed

3 files changed

+68
-40
lines changed

persistence-modules/core-java-persistence-4/src/main/java/com/baeldung/sql/MultipleSQLExecution.java

Lines changed: 15 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,10 @@
55
import java.sql.ResultSet;
66
import java.sql.SQLException;
77
import java.sql.Statement;
8-
9-
import org.slf4j.Logger;
10-
import org.slf4j.LoggerFactory;
8+
import java.util.ArrayList;
9+
import java.util.List;
1110

1211
public class MultipleSQLExecution {
13-
14-
private final Logger logger = LoggerFactory.getLogger(MultipleSQLExecution.class);
1512

1613
private Connection connection;
1714

@@ -50,33 +47,27 @@ public boolean callStoredProcedure() throws SQLException {
5047
}
5148
}
5249

53-
public boolean executeMultipleSelectStatements() throws SQLException {
50+
public List<User> executeMultipleSelectStatements() throws SQLException {
5451
String sql = "SELECT * FROM users WHERE email = '[email protected]';" +
5552
"SELECT * FROM users WHERE email = '[email protected]';";
5653

54+
List<User> users = new ArrayList<>();
55+
5756
try (Statement statement = connection.createStatement()) {
58-
boolean hasResultSet = statement.execute(sql);
59-
// We'll log each record using this loop
57+
statement.execute(sql); // Here we execute the multiple queries
58+
6059
do {
61-
if (hasResultSet) {
62-
try (ResultSet resultSet = statement.getResultSet()) {
63-
while (resultSet.next()) {
64-
logger.info("User ID: " + resultSet.getInt("id"));
65-
logger.info("Name: " + resultSet.getString("name"));
66-
logger.info("Email: " + resultSet.getString("email"));
67-
}
68-
}
69-
} else {
70-
// Here we don't have any update statements.
71-
// However, if SQL contains update statements the we need to handle update counts gracefully
72-
int updateCount = statement.getUpdateCount();
73-
if (updateCount == -1) {
74-
logger.info("No update counts for this statement.");
60+
try (ResultSet resultSet = statement.getResultSet()) {
61+
while (resultSet != null && resultSet.next()) {
62+
int id = resultSet.getInt("id");
63+
String name = resultSet.getString("name");
64+
String email = resultSet.getString("email");
65+
users.add(new User(id, name, email));
7566
}
7667
}
77-
} while (statement.getMoreResults() || statement.getUpdateCount() != -1);
68+
} while (statement.getMoreResults());
7869

79-
return true;
8070
}
71+
return users;
8172
}
8273
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package com.baeldung.sql;
2+
3+
public class User {
4+
private int id;
5+
private String name;
6+
private String email;
7+
8+
public User(int id, String name, String email) {
9+
this.id = id;
10+
this.name = name;
11+
this.email = email;
12+
}
13+
14+
public int getId() {
15+
return id;
16+
}
17+
18+
public void setId(int id) {
19+
this.id = id;
20+
}
21+
22+
public String getName() {
23+
return name;
24+
}
25+
26+
public void setName(String name) {
27+
this.name = name;
28+
}
29+
30+
public String getEmail() {
31+
return email;
32+
}
33+
34+
public void setEmail(String email) {
35+
this.email = email;
36+
}
37+
}
38+

persistence-modules/core-java-persistence-4/src/test/java/com/baeldung/sql/MultipleSQLExecutionLiveTest.java

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,13 @@
88
import java.sql.ResultSet;
99
import java.sql.SQLException;
1010
import java.sql.Statement;
11+
import java.util.List;
1112

1213
import static org.junit.jupiter.api.Assertions.assertEquals;
1314
import static org.junit.jupiter.api.Assertions.assertTrue;
1415

15-
// We need to make sure that database user_db is created along with the necessary users table.
16-
// We have added it in the setup() method.
16+
// Please note, this test requires a MySQL server running on localhost:3306 with database users_db already created.
17+
// We have added creation of the table it in the setup() method.
1718
public class MultipleSQLExecutionLiveTest {
1819

1920
private static Connection connection;
@@ -74,24 +75,22 @@ public void givenStoredProcedure_whenCalling_thenRecordsAreInserted() throws SQL
7475
}
7576

7677
@Test
77-
public void givenMultipleSelectStatements_whenExecuting_thenRecordsAreFetched() throws SQLException {
78-
// First, we'll insert data for testing
78+
public void givenMultipleSelectStatements_whenExecuting_thenCorrectUsersAreFetched() throws SQLException {
7979
MultipleSQLExecution execution = new MultipleSQLExecution(connection);
8080
execution.executeMultipleStatements();
8181

82-
// Next, we'll execute the select statements
83-
boolean result = execution.executeMultipleSelectStatements();
84-
assertTrue(result, "The select statements should execute successfully.");
82+
List<User> users = execution.executeMultipleSelectStatements();
8583

86-
// Lastly, we'll verify if the correct records are returned
87-
try (Statement statement = connection.createStatement();
88-
ResultSet resultSet = statement.executeQuery("SELECT * FROM users WHERE email IN ('[email protected]', '[email protected]')")) {
89-
int count = 0;
90-
while (resultSet.next()) {
91-
count++;
92-
}
93-
assertEquals(2, count, "There should be two records fetched.");
94-
}
84+
// Here we verify the correct number of users were fetched
85+
assertEquals(2, users.size(), "There should be exactly two users fetched.");
86+
87+
List<String> fetchedUserNames = users.stream()
88+
.map(User::getName)
89+
.toList();
90+
91+
// Here, we verify that expected users are present
92+
List<String> expectedUserNames = List.of("Alice", "Bob");
93+
assertTrue(fetchedUserNames.containsAll(expectedUserNames), "Fetched users should match the expected names.");
9594
}
9695
}
9796

0 commit comments

Comments
 (0)