LukeSavefrogs

Untitled

Oct 14th, 2025
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.21 KB | None | 0 0
  1. import java.sql.Connection;
  2. import java.sql.PreparedStatement;
  3. import java.sql.ResultSet;
  4. import java.sql.SQLException;
  5.  
  6. public abstract class DatabaseService implements MyService {
  7.  
  8.     public < T > T executeQuery(String pQuery, Object...pParameters)
  9.     throws SQLException, MappingException {
  10.  
  11.         if (LOGGER.isTraceEnabled()) {
  12.             LOGGER.trace("Executing sql query {}...", pQuery);
  13.         }
  14.  
  15.         try (PreparedStatement vStatement = getConnection().prepareStatement(pQuery, ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY)) {
  16.  
  17.             MyDbUtils.setParameters(vStatement, pParameters);
  18.  
  19.             try (ResultSet vResultSet = vStatement.executeQuery()) {
  20.  
  21.                 if (!vResultSet.next()) {
  22.                     return null;
  23.                 }
  24.  
  25.                 Object vRis = vResultSet.getObject(1);
  26.  
  27.                 if (vResultSet.next()) {
  28.                     throw new MappingException("Too many occurrence");
  29.                 }
  30.  
  31.                 return vRis;
  32.             } catch (SQLException | RowLimitReached e) {
  33.                 throw e;
  34.             } catch (Exception e) {
  35.                 throw new MappingException(e);
  36.             }
  37.         }
  38.  
  39.     }
  40. }
Add Comment
Please, Sign In to add comment