Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.sql.Connection;
- import java.sql.PreparedStatement;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- public abstract class DatabaseService implements MyService {
- public < T > T executeQuery(String pQuery, Object...pParameters)
- throws SQLException, MappingException {
- if (LOGGER.isTraceEnabled()) {
- LOGGER.trace("Executing sql query {}...", pQuery);
- }
- try (PreparedStatement vStatement = getConnection().prepareStatement(pQuery, ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY)) {
- MyDbUtils.setParameters(vStatement, pParameters);
- try (ResultSet vResultSet = vStatement.executeQuery()) {
- if (!vResultSet.next()) {
- return null;
- }
- Object vRis = vResultSet.getObject(1);
- if (vResultSet.next()) {
- throw new MappingException("Too many occurrence");
- }
- return vRis;
- } catch (SQLException | RowLimitReached e) {
- throw e;
- } catch (Exception e) {
- throw new MappingException(e);
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment