-
Notifications
You must be signed in to change notification settings - Fork 0
Create UserDAO.java #10
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,21 @@ | ||||||||||||||||||||||||||||||
| import java.sql.*; | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| public class UserDAO { | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| public void findUser(String username) { | ||||||||||||||||||||||||||||||
| String url = "jdbc:mysql://localhost:3306/mydb"; | ||||||||||||||||||||||||||||||
| String user = "admin"; | ||||||||||||||||||||||||||||||
| String password = "supersecret"; | ||||||||||||||||||||||||||||||
| try { | ||||||||||||||||||||||||||||||
| Connection conn = DriverManager.getConnection(url, user, password); | ||||||||||||||||||||||||||||||
| Statement stmt = conn.createStatement(); | ||||||||||||||||||||||||||||||
| ResultSet rs = stmt.executeQuery("SELECT * FROM users WHERE username = '" + username + "'"); | ||||||||||||||||||||||||||||||
|
Comment on lines
+10
to
+12
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. Use try-with-resources and
-Connection conn = DriverManager.getConnection(url, user, password);
-Statement stmt = conn.createStatement();
-ResultSet rs = stmt.executeQuery("SELECT * FROM users WHERE username = '" + username + "'");
+try (Connection conn = DriverManager.getConnection(url, user, password);
+ PreparedStatement ps =
+ conn.prepareStatement("SELECT email FROM users WHERE username = ?")) {
+
+ ps.setString(1, username);
+ try (ResultSet rs = ps.executeQuery()) {
+ while (rs.next()) {
+ // see comment below about handling PII
+ }
+ }
+}📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||||||||||||||||||||||||||
| while (rs.next()) { | ||||||||||||||||||||||||||||||
| System.out.println(rs.getString("email")); | ||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||
|
Comment on lines
+14
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 Avoid printing PII to stdout; return or log appropriately Dumping user email addresses to standard output both leaks personally-identifiable information and makes unit-testing difficult. -while (rs.next()) {
- System.out.println(rs.getString("email"));
-}
+List<String> emails = new ArrayList<>();
+while (rs.next()) {
+ emails.add(rs.getString("email"));
+}
+return emails; // adjust method signature accordingly
🤖 Prompt for AI Agents |
||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| } catch (SQLException e) { | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||
|
Comment on lines
+17
to
+19
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. Empty catch block hides problems – add logging or rethrow Silently swallowing -} catch (SQLException e) {
-
-}
+} catch (SQLException e) {
+ // at minimum log and rethrow a domain-specific exception
+ Logger.getLogger(UserDAO.class.getName()).log(Level.SEVERE,
+ "Database error while finding user " + username, e);
+ throw new RuntimeException("Unable to fetch user", e);
+}📝 Committable suggestion
Suggested change
🤖 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.
🛠️ Refactor suggestion
Remove hard-coded DB credentials – externalise via configuration
Embedding the JDBC URL, username and especially the plaintext password directly in the source code is a severe security and maintainability issue.
Move these values to environment variables, a secrets-manager, or a properties file that is not committed to VCS.
📝 Committable suggestion
🤖 Prompt for AI Agents