Skip to content

Instantly share code, notes, and snippets.

@valarpirai
Created January 16, 2026 06:56
Show Gist options
  • Select an option

  • Save valarpirai/ed3c8ab43a850fadbbaf23de62184d7c to your computer and use it in GitHub Desktop.

Select an option

Save valarpirai/ed3c8ab43a850fadbbaf23de62184d7c to your computer and use it in GitHub Desktop.
Controller without error handling
import org.springframework.web.bind.annotation.*;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.sql.*;
@RestController
@RequestMapping("/api/config")
public class ConfigController {
private static final String DB_URL = "jdbc:mysql://localhost:3306/mydb";
private static final String DB_USER = "admin";
private static final String DB_PASSWORD = "password";
@PostMapping("/enable")
public String enableConfig(@RequestBody String jsonPayload) throws Exception {
// Parse JSON - assumes it's valid
ObjectMapper mapper = new ObjectMapper();
ConfigRequest request = mapper.readValue(jsonPayload, ConfigRequest.class);
// Save to database - assumes connection works
Connection conn = DriverManager.getConnection(DB_URL, DB_USER, DB_PASSWORD);
String sql = "INSERT INTO configurations (feature_name, enabled, updated_by) VALUES (?, ?, ?)";
PreparedStatement stmt = conn.prepareStatement(sql);
stmt.setString(1, request.getFeatureName());
stmt.setBoolean(2, request.isEnabled());
stmt.setString(3, request.getUpdatedBy());
stmt.executeUpdate();
// Never closes resources
return "Success";
}
static class ConfigRequest {
private String featureName;
private boolean enabled;
private String updatedBy;
// Getters and setters omitted for brevity
public String getFeatureName() { return featureName; }
public void setFeatureName(String featureName) { this.featureName = featureName; }
public boolean isEnabled() { return enabled; }
public void setEnabled(boolean enabled) { this.enabled = enabled; }
public String getUpdatedBy() { return updatedBy; }
public void setUpdatedBy(String updatedBy) { this.updatedBy = updatedBy; }
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment