List of usage examples for java.sql SQLException getLocalizedMessage
public String getLocalizedMessage()
From source file:com.egt.core.db.xdp.RecursoCachedRowSetDataProvider.java
private Object execute(String procedure, Object argumento) { // throws SQLException Bitacora.trace(getClass(), "executeFunction", procedure, argumento); if (StringUtils.isNotBlank(procedure) && argumento != null) { try {/*from w w w .j a v a 2 s . co m*/ if (TLC.getAgenteSql().isStoredProcedure(procedure)) { Object[] args = new Object[] { argumento }; Object resultado = TLC.getAgenteSql().executeProcedure(procedure, args); if (resultado instanceof ResultSet) { ResultSet resultSet = (ResultSet) resultado; if (resultSet.next()) { return resultSet.getObject(1); } // } else if (resultado instanceof Number) { // return resultado; } return resultado; } } catch (SQLException ex) { String localizedMessage = StringUtils.substringBefore(ex.getLocalizedMessage(), " Where: "); TLC.getBitacora().error(localizedMessage); return ex; } } return null; }
From source file:swp.bibjsf.persistence.Data.java
/** * Generic retrieval of elements from a given table fulfilling given * constraints and sorted by given order. Only the elements from...to in * that order are returned./*w ww . j av a2 s.com*/ * * @param constraints * constraints to be fulfilled * @param from * index of first relevant element (index of very first element * is 0) * @param to * index of last relevant element * @param order * the ordering * @param table * name of the table from which to retrieve the elements * @param clazz * the class of the elements to be retrieved, i.e., Element.class * @return matching elements * @throws DataSourceException */ public <Element extends BusinessObject> List<Element> getElements(List<Constraint> constraints, final int from, final int to, List<OrderBy> order, String table, Class<Element> clazz) throws DataSourceException { // We want to retrieve only some of the matching results, but not // all. For a very large data set, we might otherwise run into // memory problems. And since this code is run by a server serving // multiple clients at once, memory consumption and computing // time is an issue. // // In Derby 10.7 upward, limiting the search for certain number of // results would be possible using the FETCH and OFFSET keywords as // follows: // // Sort T using column I, then fetch rows 11 through 20 of the sorted // rows (inclusive) // SELECT * FROM T ORDER BY I OFFSET 10 ROWS FETCH NEXT 10 ROWS ONLY // // Glashfish 3.1, however, ships with Derby 10.6.2.1. That version of // Derby // does not support FETCH/OFFSET. // If we ever migrate to a more current Derby version, we should use // FETCH/OFFSET // instead. // // For this reason, we follow a pagination strategy described at: // http://www.onjava.com/pub/a/onjava/2007/01/31/tuning-derby.html // // Notice that we set the max rows value to the last row that we need // (incremented by one). So, with this solution we fetch not only the // rows that we wanted (from - to), but first fetched a all rows up to // 'to' // and then filter to the rows of interest. Unfortunately, there is no // way // to tell the JDBC driver to start with a certain row, so we must // specify the // maximum row of the page that will be displayed. This means that // performance will be good for early pages and drop in performance as // the user browses results. The good news is that in most cases, the // user // will not go far, but will usually either find what he's looking for // in // the first few pages or refine the search query. logger.debug("get elements for table " + table); ArrayList<Element> allResults = new ArrayList<Element>(); try { Connection connection = dataSource.getConnection(); try { String query = "SELECT * FROM " + table + toQuery(constraints) + toOrderByClause(order); logger.debug("getElements " + query); PreparedStatement stmt = connection.prepareStatement(query); try { try { stmt.setMaxRows(to + 1); } catch (SQLException e) { // ignore this exception and try to run the query anyway } fillInArguments(constraints, stmt); ResultSet rs = stmt.executeQuery(); try { // Use the BeanHandler implementation to convert the // first // ResultSet row into a Reader JavaBean. ResultSetHandler<Element> handler = new BeanHandler<Element>(clazz); int i = 0; Element reader; while ((reader = handler.handle(rs)) != null) { if (from <= i && i <= to) { allResults.add(reader); } else if (i > to) { break; } i++; } } finally { rs.close(); } } finally { stmt.close(); } } finally { connection.close(); } } catch (SQLException e) { logger.error(e.getLocalizedMessage()); throw new DataSourceException(e.getLocalizedMessage()); } return allResults; }
From source file:ips1ap101.lib.core.db.xdp.RecursoCachedRowSetDataProvider.java
protected Object execute(String procedure, Object argumento) { // throws SQLException Bitacora.trace(getClass(), "execute", procedure, argumento); if (StringUtils.isNotBlank(procedure) && argumento != null) { try {/*from w ww .jav a2 s . c o m*/ if (TLC.getAgenteSql().isStoredProcedure(procedure)) { Object[] args = new Object[] { argumento }; Object resultado = TLC.getAgenteSql().executeProcedure(procedure, args); if (resultado instanceof ResultSet) { ResultSet resultSet = (ResultSet) resultado; if (resultSet.next()) { return resultSet.getObject(1); } // } else if (resultado instanceof Number) { // return resultado; } return resultado; } } catch (SQLException ex) { String localizedMessage = DBUtils.getProperErrorMessage(ex.getLocalizedMessage()); TLC.getBitacora().error(localizedMessage); return ex; } } return null; }
From source file:org.opencms.db.generic.CmsVfsDriver.java
/** * Returns all organizational units for the given resource.<p> * /*from w w w .j av a 2s. c o m*/ * @param dbc the database context * @param projectId the id of the project * @param resource the resource * * @return a list of {@link org.opencms.security.CmsOrganizationalUnit} objects * * @throws CmsDbSqlException if something goes wrong */ public List<CmsOrganizationalUnit> getResourceOus(CmsDbContext dbc, CmsUUID projectId, CmsResource resource) throws CmsDbSqlException { List<CmsOrganizationalUnit> ous = new ArrayList<CmsOrganizationalUnit>(); String resName = resource.getRootPath(); if (resource.isFolder() && !resName.endsWith("/")) { resName += "/"; } Connection conn = null; PreparedStatement stmt = null; ResultSet res = null; List<CmsRelation> rels = new ArrayList<CmsRelation>(); try { conn = m_sqlManager.getConnection(dbc); stmt = m_sqlManager.getPreparedStatementForSql(conn, m_sqlManager.readQuery(projectId, "C_READ_RESOURCE_OUS")); stmt.setInt(1, CmsRelationType.OU_RESOURCE.getId()); stmt.setString(2, resName); res = stmt.executeQuery(); while (res.next()) { rels.add(internalReadRelation(res)); } } catch (SQLException e) { throw new CmsDbSqlException( Messages.get().container(Messages.ERR_GENERIC_SQL_1, CmsDbSqlException.getErrorQuery(stmt)), e); } finally { m_sqlManager.closeAll(dbc, conn, stmt, res); } for (CmsRelation rel : rels) { try { ous.add(m_driverManager.readOrganizationalUnit(dbc, rel.getSourcePath().substring(CmsUserDriver.ORGUNIT_BASE_FOLDER.length()))); } catch (CmsException e) { // should never happen if (LOG.isErrorEnabled()) { LOG.error(e.getLocalizedMessage(), e); } } } return ous; }