forked from green-code-initiative/creedengo-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAvoidSQLRequestInLoopCheck.java
More file actions
151 lines (132 loc) · 5.46 KB
/
AvoidSQLRequestInLoopCheck.java
File metadata and controls
151 lines (132 loc) · 5.46 KB
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
/*
* creedengo - Java language - Provides rules to reduce the environmental footprint of your Java programs
* Copyright © 2024 Green Code Initiative (https://green-code-initiative.org/)
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.greencodeinitiative.creedengo.java.checks;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
class AvoidSQLRequestInLoopCheck {
AvoidSQLRequestInLoopCheck(AvoidSQLRequestInLoopCheck mc) {
}
public void testWithNoLoop() {
try {
// create our mysql database connection
String myDriver = "driver";
String myUrl = "driver";
Class.forName(myDriver);
Connection conn = DriverManager.getConnection(myUrl, "toor", "");
// our SQL SELECT query.
// if you only need a few columns, specify them by name instead of using "*"
String query = "SELECT * FROM users";
// create the java statement
Statement st = conn.createStatement();
ResultSet rs = st.executeQuery(query);
// iterate through the java resultset
while (rs.next()) {
int id = rs.getInt("id");
System.out.println(id);
}
st.close();
} catch (Exception e) {
System.err.println("Got an exception! ");
System.err.println(e.getMessage());
}
}
public void testWithForLoop() {
try {
// create our mysql database connection
String myDriver = "driver";
String myUrl = "driver";
Class.forName(myDriver);
Connection conn = DriverManager.getConnection(myUrl, "toor", "");
// our SQL SELECT query.
// if you only need a few columns, specify them by name instead of using "*"
String baseQuery = "SELECT name FROM users where id = ";
for (int i = 0; i < 20; i++) {
// create the java statement
String query = baseQuery.concat("" + i);
Statement st = conn.createStatement();
ResultSet rs = st.executeQuery(query); // Noncompliant {{Avoid SQL request in loop}}
// iterate through the java resultset
while (rs.next()) {
String name = rs.getString("name");
System.out.println(name);
}
st.close();
}
} catch (Exception e) {
System.err.println("Got an exception! ");
System.err.println(e.getMessage());
}
}
public void testWithForEachLoop() {
try {
// create our mysql database connection
String myDriver = "driver";
String myUrl = "driver";
Class.forName(myDriver);
Connection conn = DriverManager.getConnection(myUrl, "toor", "");
// our SQL SELECT query.
// if you only need a few columns, specify them by name instead of using "*"
String query = "SELECT * FROM users";
int[] intArray = {10, 20, 30, 40, 50};
for (int i : intArray) {
System.out.println(i);
// create the java statement
Statement st = conn.createStatement();
ResultSet rs = st.executeQuery(query); // Noncompliant {{Avoid SQL request in loop}}
// iterate through the java resultset
while (rs.next()) {
int id = rs.getInt("id");
System.out.println(id);
}
st.close();
}
} catch (Exception e) {
System.err.println("Got an exception! ");
System.err.println(e.getMessage());
}
}
public void testWithWhileLoop() {
try {
// create our mysql database connection
String myDriver = "driver";
String myUrl = "driver";
Class.forName(myDriver);
Connection conn = DriverManager.getConnection(myUrl, "toor", "");
// our SQL SELECT query.
// if you only need a few columns, specify them by name instead of using "*"
String query = "SELECT * FROM users";
int i = 0;
while (i < -1) {
// create the java statement
Statement st = conn.createStatement();
ResultSet rs = st.executeQuery(query); // Noncompliant {{Avoid SQL request in loop}}
// iterate through the java resultset
while (rs.next()) {
int id = rs.getInt("id");
System.out.println(id);
}
st.close();
}
} catch (Exception e) {
System.err.println("Got an exception! ");
System.err.println(e.getMessage());
}
}
}