Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.sql.*;
- import java.util.function.Function;
- // TODO: documentation
- public class GigDataStore {
- private final String DB_URL;
- private final String DB_USER;
- private final String DB_PASS;
- public GigDataStore(String DB_URL, String DB_USER, String DB_PASS) {
- this.DB_URL = DB_URL;
- this.DB_USER = DB_USER;
- this.DB_PASS = DB_PASS;
- }
- public Gig getGig(int id) {
- return runOnNewStatement(statement -> {
- try {
- String query = String.format(
- "SELECT * FROM dm.gigs WHERE id = %d;",
- id
- );
- ResultSet rs = statement.executeQuery(query);
- }
- });
- }
- // run whatever queries we need, and then close it
- // I got tired of typing this all out
- // Also, functional Java! Yay!
- private <T> T runOnNewStatement(Function<Statement, T> dataFunction) {
- // this syntax automatically closes everything if there is a problem
- // don't you just love multiple-use syntax?
- try (Connection conn = DriverManager.getConnection(this.DB_URL, this.DB_USER, this.DB_PASS);
- Statement stmt = conn.createStatement()) {
- return dataFunction.apply(stmt);
- } catch (SQLException e) {
- e.printStackTrace();
- }
- return null;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement