Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

EC72 Java Example Code #361

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 23 additions & 14 deletions ecocode-rules-specifications/src/main/rules/EC72/java/EC72.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -28,23 +28,32 @@ public void foo() {

```java
public void foo() {
// ...
String query = "SELECT name FROM users where id in (0 ";
for (int i = 1; i < 20; i++) {

query = baseQuery.concat("," + i);
StringBuilder queryBuilder = new StringBuilder("SELECT name FROM users WHERE id IN (");
for (int i = 0; i < 20; i++) {
if (i > 0) {
queryBuilder.append(",");
}
queryBuilder.append("?");
}
queryBuilder.append(")");

String query = queryBuilder.toString();

query = baseQuery.concat(")");
Statement st = conn.createStatement();
ResultSet rs = st.executeQuery(query); // compliant
try (Connection conn = DriverManager.getConnection("your-database-url");
PreparedStatement pst = conn.prepareStatement(query)) {

// iterate through the java resultset
while (rs.next()) {
String name = rs.getString("name");
System.out.println(name);
for (int i = 0; i < 20; i++) {
pst.setInt(i + 1, i);
}

try (ResultSet rs = pst.executeQuery()) { // compliant
while (rs.next()) {
String name = rs.getString("name");
System.out.println(name);
}
}
} catch (SQLException e) {
e.printStackTrace();
}
st.close();
// ...
}
```