-
Notifications
You must be signed in to change notification settings - Fork 0
Create DBConnection.java #8
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,16 @@ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import java.sql.*; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| public class DBConnection { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| public void getData() { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| try { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/mydb", "user", "pass"); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Statement stmt = conn.createStatement(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ResultSet rs = stmt.executeQuery("SELECT * FROM users"); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| while (rs.next()) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| System.out.println(rs.getString("username")); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } catch (SQLException e) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| e.printStackTrace(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+4
to
+15
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🛠️ Refactor suggestion Resource leak & unstructured error handling – switch to try-with-resources
- public void getData() {
- try {
- Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/mydb", "user", "pass");
- Statement stmt = conn.createStatement();
- ResultSet rs = stmt.executeQuery("SELECT * FROM users");
- while (rs.next()) {
- System.out.println(rs.getString("username"));
- }
- } catch (SQLException e) {
- e.printStackTrace();
- }
+ public List<String> getUsernames() throws SQLException {
+ List<String> users = new ArrayList<>();
+ String query = "SELECT username FROM users";
+
+ try (Connection conn = DriverManager.getConnection(
+ System.getenv("DB_URL"),
+ System.getenv("DB_USER"),
+ System.getenv("DB_PASS"));
+ PreparedStatement ps = conn.prepareStatement(query);
+ ResultSet rs = ps.executeQuery()) {
+
+ while (rs.next()) {
+ users.add(rs.getString("username"));
+ }
+ }
+ return users;
}📝 Committable suggestion
Suggested change
🧰 Tools🪛 ast-grep (0.38.1)[warning] 5-5: A secret is hard-coded in the application. Secrets stored in source code, such as credentials, identifiers, and other types of sensitive data, can be leaked and used by internal or external malicious actors. Use environment variables to securely provide credentials and other secrets or retrieve them from a secure vault or Hardware Security Module (HSM). (drivermanager-hardcoded-secret-java) 🤖 Prompt for AI Agents |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hard-coded credentials leak secrets
"user"/"pass"embedded in source control violates OWASP secret-management guidance and surfaced in static analysis. Move them to env-vars or a secrets vault.🤖 Prompt for AI Agents