Created
September 7, 2022 18:18
-
-
Save ilyash-b/298cda6f02d86dbdd94ab7af4ded9f0c to your computer and use it in GitHub Desktop.
Java Project refactor
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public int getId(String queryId, String queryParameter, String objectName) { | |
try (PreparedStatement preparedStmt = getConnection().prepareStatement(queryId)) { | |
preparedStmt.setString(1, queryParameter); | |
ResultSet u = preparedStmt.executeQuery(); | |
if (u.next()) { | |
return u.getInt("id"); | |
} | |
throw new SQLException(objectName + " does not exists"); | |
} catch (SQLException e) { | |
e.printStackTrace(); | |
throw new RuntimeException(e); | |
} | |
} | |
public int getUserId2(User user) { | |
return getId("SELECT id FROM users WHERE userName= ? ", user.getUsername(), "User"); | |
} | |
public int getGameId2(String gameName) { | |
return getId("SELECT id FROM games WHERE name= ? ", gameName, "Game"); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment