mercredi 5 décembre 2018

Invoke method in another class's try catch block

I have some methods that follow this pattern

try(Connection connection = MySqlConnection.getConnection()){
        PreparedStatement statement = connection.prepareStatement(
                "Insert into db values (NULL ,?,?,?,?,?, NULL , ?)",
                Statement.RETURN_GENERATED_KEYS);
        ...
        statement.executeUpdate();
        ...
    }
    catch(SQLException e) {
        throw new RuntimeException(e);
    }

I was told to extract try-catch with connection to class MySqlConnection and create a new method that would execute all logic and encapsulate creating connection. So, I don't quite get this approach and have no idea, how to resolve it without writing some ugly templates or strategies. Would it be better to just leave it as it is, or can it be implemented in an easy way?

Aucun commentaire:

Enregistrer un commentaire