What’s actually slowing this PC down?
Pick the symptom - the matching free tool is one click away.
For a MySQL query that should match either of two positions for one team, write WHERE pos IN (?, ?) AND team = ?. To add one to a counter, use SET gamesplayed = gamesplayed + 1 in the UPDATE statement. In a JSP application, bind values with JDBC PreparedStatement; the SQL behavior comes from MySQL’s Boolean rules and update expressions, not from JSP.
Why the original OR/AND condition returns unexpected rows
In MySQL, AND has higher precedence than OR. That means this condition:
WHERE position = 'WR'
OR position = 'QB'
AND team = 'NYG'
is evaluated as:
WHERE position = 'WR'
OR (position = 'QB' AND team = 'NYG')
Every row with position WR can match, regardless of team; the team restriction applies only to QB rows. To require both positions to belong to the specified team, group the alternatives first:
WHERE (position = 'WR' OR position = 'QB')
AND team = 'NYG'
MySQL documents these expression rules in its operator-precedence reference. Parentheses make the intended grouping visible and reduce the chance of a future logic error. The original question appeared in a historical MySQL/JSP forum thread.
Free tools Windows power users keep installed
One-click scans. No signup required.
#1 Best Overall
Choose IN for several values from the same column
When the alternatives are simple equality checks on one column, IN expresses the same logic more compactly:
SELECT player_id, player_name, pos, team, gamesplayed
FROM players AS p
WHERE p.pos IN ('QB', 'WR')
AND p.team = 'NYG'
ORDER BY p.player_name DESC;
For application code, use placeholders for the values:
SELECT player_id, player_name, pos, team, gamesplayed
FROM players AS p
WHERE p.pos IN (?, ?)
AND p.team = ?
ORDER BY p.player_name DESC;
Explicit parentheses are equally valid and useful when each alternative has additional conditions:
WHERE (pos = ? OR pos = ?)
AND team = ?
Use IN for a list of values in one column; use grouped Boolean expressions when the alternatives are genuinely different predicates. The wording of the query is not a promise that IN will be faster: performance depends on the query, indexes, data, and server version.
The Tool Desk
Outbyte PC Repair FREEClear out junk files and repair common Windows errorsFree Scan →Outbyte Driver Updater FREEFix the driver behind crashes, sound loss and screen glitchesFind Drivers →Building a variable-length IN list
For a list whose length varies, create one placeholder per value, then bind every value separately. Do not insert the values themselves into the SQL string.
List<String> positions = List.of("QB", "WR", "HB");
if (positions.isEmpty()) {
return List.of(); // No positions means no results.
}
String placeholders = String.join(", ",
Collections.nCopies(positions.size(), "?"));
String sql = "SELECT player_id, player_name, pos, team " +
"FROM players WHERE pos IN (" + placeholders + ") " +
"AND team = ?";
try (PreparedStatement ps = connection.prepareStatement(sql)) {
int index = 1;
for (String position : positions) {
ps.setString(index++, position);
}
ps.setString(index, teamId);
try (ResultSet rs = ps.executeQuery()) {
// Map rows to application objects.
}
}
The SQL fragment assembled here contains only a count of trusted placeholder marks. An empty list must be handled before SQL construction; IN () is not a valid substitute.
Use PreparedStatement for JDBC values
Concatenating a request value into SQL can break when it contains a quote and can expose the application to SQL injection. Bind input values with a PreparedStatement, as described in the Java SE API and the Oracle JDBC tutorial.
String sql =
"SELECT player_id, player_name, pos, team, gamesplayed " +
"FROM players " +
"WHERE pos IN (?, ?) AND team = ? " +
"ORDER BY player_name DESC";
try (PreparedStatement ps = connection.prepareStatement(sql)) {
ps.setString(1, "QB");
ps.setString(2, "WR");
ps.setString(3, teamId);
try (ResultSet rs = ps.executeQuery()) {
while (rs.next()) {
long id = rs.getLong("player_id");
String name = rs.getString("player_name");
String position = rs.getString("pos");
int gamesPlayed = rs.getInt("gamesplayed");
// Convert the row to a Player or other application object.
}
}
}
Use executeQuery() for a query that returns rows, such as this SELECT. Use executeUpdate() for an UPDATE, INSERT, or DELETE. The try-with-resources blocks close the result set and statement even if processing throws an exception; the connection should also be managed and closed by its owner. Production applications commonly obtain connections from a configured DataSource and connection pool rather than opening a fresh connection through DriverManager for each request. See the MySQL Connector/J documentation for the JDBC driver.
Outdated Drivers Are Slowing You Down
One free scan finds every outdated or missing driver and matches the right update for your exact hardware.Free scan · exact hardware matchPC Slower Than It Used to Be?
A free scan shows the junk files, broken settings and background clutter dragging Windows down - then fixes them in one click.Free scan · Windows 10 & 11Increment a counter in the UPDATE statement
To add one to every matching row, let MySQL evaluate the expression as part of the update:
UPDATE players
SET gamesplayed = gamesplayed + 1
WHERE team = ?;
For the two teams in a game or event:
UPDATE players
SET gamesplayed = gamesplayed + 1
WHERE team IN (?, ?);
Binding the teams and executing the update in JDBC looks like this:
String sql =
"UPDATE players " +
"SET gamesplayed = gamesplayed + 1 " +
"WHERE team IN (?, ?)";
try (PreparedStatement ps = connection.prepareStatement(sql)) {
ps.setString(1, awayTeam);
ps.setString(2, homeTeam);
int rowsChanged = ps.executeUpdate();
}
This single-statement expression avoids the extra round trips of selecting the old value, adding one in Java, and writing a replacement. That read-modify-write pattern can lose an increment when concurrent requests read the same old value and then overwrite one another. MySQL’s UPDATE reference describes the statement form. A database statement does not, by itself, make a larger multi-statement business operation atomic.
Account for NULL counters
If gamesplayed is NULL, then gamesplayed + 1 is also NULL. If a null value should mean zero, use:
Rank #4
UPDATE players
SET gamesplayed = COALESCE(gamesplayed, 0) + 1
WHERE team IN (?, ?);
For a counter that should never be null, a schema rule such as NOT NULL DEFAULT 0 is another option. For an existing table, check its data, constraints, and application assumptions before changing the column:
ALTER TABLE players
MODIFY gamesplayed INT NOT NULL DEFAULT 0;
Keep dynamic sorting separate from bound values
A placeholder represents a value; it does not safely stand in for a column name or SQL keyword. For example, ORDER BY ? does not provide a general way to select a sort column. If the user can choose a sort field, map the request to a small set of fixed SQL fragments:
Map<String, String> allowedSorts = Map.of(
"name", "player_name",
"games", "gamesplayed",
"team", "team"
);
String sortColumn = allowedSorts.getOrDefault(sortField, "player_name");
String sql = "SELECT player_id, player_name, pos, team, gamesplayed " +
"FROM players WHERE pos IN (?, ?) AND team = ? " +
"ORDER BY " + sortColumn + " DESC";
Only the allowlisted column name is concatenated. Bind the position and team values as usual. Prepared statements protect bound values, not arbitrary SQL fragments or authorization decisions.
Move database work out of JSP scriptlets
Legacy JSP pages may contain scriptlets that create statements and execute queries directly. Such code can run, but putting database access and business logic in a view makes resource cleanup, error handling, testing, and security harder to manage. The Jakarta Server Pages specification defines JSP technology; it does not change MySQL’s query semantics.
Quick wins for a faster PC:
Clear out junk files and repair common Windows errorsFree Scan →Scan for outdated or missing drivers - takes under a minuteDriver Scan →A more maintainable flow separates responsibilities:
- A servlet or controller receives the request and validates its inputs.
- A repository or DAO performs the JDBC operation using parameterized SQL.
- The controller places the resulting model data in request attributes.
- A JSP renders the data with EL/JSTL rather than opening database connections.
List<Player> players = playerRepository.findByPositionsAndTeam(
List.of("QB", "WR"), teamId);
request.setAttribute("players", players);
request.getRequestDispatcher("/WEB-INF/views/players.jsp")
.forward(request, response);
<c:forEach var="player" items="${players}">
<tr>
<td>${player.name}</td>
<td>${player.position}</td>
<td>${player.team}</td>
</tr>
</c:forEach>
For Spring applications, the Spring JDBC reference covers a higher-level data-access option.
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Use a transaction when the update belongs to a larger event
If recording a game and incrementing player statistics are one business operation, perform both in a transaction so one does not commit without the other:
try {
connection.setAutoCommit(false);
try (PreparedStatement insertGame = connection.prepareStatement(
"INSERT INTO games (away_team, home_team) VALUES (?, ?)");
PreparedStatement increment = connection.prepareStatement(
"UPDATE players " +
"SET gamesplayed = COALESCE(gamesplayed, 0) + 1 " +
"WHERE team IN (?, ?)")) {
insertGame.setString(1, awayTeam);
insertGame.setString(2, homeTeam);
insertGame.executeUpdate();
increment.setString(1, awayTeam);
increment.setString(2, homeTeam);
int rowsChanged = increment.executeUpdate();
connection.commit();
} catch (SQLException ex) {
connection.rollback();
throw ex;
}
} finally {
connection.setAutoCommit(true);
}
Adapt transaction and connection-state handling to the application’s connection-management framework; code using a pooled connection must return it in a valid state. If both team values are equal, IN (?, ?) still updates each matching row once, not twice. Validate whether a same-team event is allowed by the business rules.
Check the return from executeUpdate(). A zero can mean the WHERE clause matched no rows; how matched-but-unchanged rows are reported can depend on database and JDBC driver settings. Confirm the deployed Connector/J behavior and expected row count rather than assuming the number always means exactly the same thing. If a specific number of player rows is expected, treat an unexpected count as a logic or data-quality issue worth handling.
Quick Recap
Troubleshoot common failures
- Rows from the wrong team appear: group the position alternatives with parentheses or use
pos IN (?, ?)before applyingAND team = ?. - No rows update: verify the bound team values and whether stored values include unexpected spaces or differ in case under the column’s collation.
- The counter remains null: use
COALESCE(gamesplayed, 0) + 1or enforce a non-null default after checking existing data. - Placeholder or syntax error: ensure the number of placeholders equals the number of bound values and bind them in the same order.
- SQL injection or quote-related errors: remove value concatenation and bind values with
PreparedStatement. - Sort-column errors or injection risk: use an allowlist of permitted column names rather than binding or concatenating a raw request parameter.
- Connection exhaustion or intermittent failures: close result sets and statements with try-with-resources, and return connections through the configured pool.
- Empty position list: return no results or deliberately use a false condition such as
WHERE 1 = 0; do not generateIN ().
Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

