List of usage examples for java.sql SQLException getLocalizedMessage
public String getLocalizedMessage()
From source file:org.pentaho.reporting.engine.classic.extensions.datasources.mondrian.AbstractMDXDataFactory.java
/** * Queries a datasource. The string 'query' defines the name of the query. The Parameterset given here may contain * more data than actually needed for the query. * <p/>//from w w w . j av a2 s .c o m * The parameter-dataset may change between two calls, do not assume anything, and do not hold references to the * parameter-dataset or the position of the columns in the dataset. * * @param rawMdxQuery the mdx Query string. * @param parameters the parameters for the query * @return the result of the query as table model. * @throws org.pentaho.reporting.engine.classic.core.ReportDataFactoryException if an error occured while performing * the query. */ public Result performQuery(final String rawMdxQuery, final DataRow parameters) throws ReportDataFactoryException { try { if (connection == null) { connection = mondrianConnectionProvider.createConnection(computeProperties(parameters), dataSourceProvider.getDataSource()); } } catch (SQLException e) { throw new ReportDataFactoryException("Failed to create datasource:" + e.getLocalizedMessage(), e); } catch (MondrianException e) { throw new ReportDataFactoryException("Failed to create datasource:" + e.getLocalizedMessage(), e); } try { if (connection == null) { throw new ReportDataFactoryException("Factory is closed."); } final MDXCompiler compiler = new MDXCompiler(parameters, getLocale()); final String mdxQuery = compiler.translateAndLookup(rawMdxQuery, parameters); // Alternatively, JNDI is possible. Maybe even more .. final Query query = connection.parseQuery(mdxQuery); final Statement statement = query.getStatement(); final int queryTimeoutValue = calculateQueryTimeOut(parameters); if (queryTimeoutValue > 0) { statement.setQueryTimeoutMillis(queryTimeoutValue * 1000); } parametrizeQuery(parameters, query); //noinspection deprecation final Result resultSet = connection.execute(query); if (resultSet == null) { throw new ReportDataFactoryException("query returned no resultset"); } return resultSet; } catch (MondrianException e) { throw new ReportDataFactoryException("Failed to create datasource:" + e.getLocalizedMessage(), e); } }
From source file:org.orbisgis.mapeditor.map.MapEditor.java
/** * The user can export the selected features into a new datasource *//*from w w w . j a va 2 s . c o m*/ public void onCreateDataSourceFromSelection() { // Get the selected layer(s) ILayer[] selectedLayers = getMapControl().getToolManager().getSelectedLayerAndStyle(); // Loop through all selected layers. if (selectedLayers == null || selectedLayers.length == 0) { GUILOGGER.warn(I18N.tr("No layers are selected.")); } else { for (ILayer layer : selectedLayers) { Set<Long> selection = layer.getSelection(); // If there is a nonempty selection, then ask the user to name it. if (!selection.isEmpty()) { try { String newName = CreateSourceFromSelection.showNewNameDialog(this, dataManager.getDataSource(), layer.getTableReference()); // If newName is not null, then the user clicked OK and // entered a valid name. if (newName != null) { execute(new CreateSourceFromSelection(dataManager.getDataSource(), selection, layer.getTableReference(), newName)); } } catch (SQLException ex) { GUILOGGER.error(ex.getLocalizedMessage(), ex); } } else { GUILOGGER.warn(I18N.tr("Layer {0} has no selected geometries.", layer.getName())); } } } }
From source file:org.pentaho.pac.server.PacServiceImpl.java
public boolean testDataSourceValidationQuery(PentahoDataSource ds) throws PacServiceException { Connection conn = null;/*from w w w . j a v a 2 s .c om*/ Statement stmt = null; ResultSet rs = null; try { conn = getDataSourceConnection(ds); if (!StringUtils.isEmpty(ds.getQuery())) { stmt = conn.createStatement(); rs = stmt.executeQuery(ds.getQuery()); } else { throw new PacServiceException(Messages.getErrorString("PacService.ERROR_0028_QUERY_NOT_VALID")); //$NON-NLS-1$ } } catch (DataSourceManagementException dme) { throw new PacServiceException(Messages.getErrorString("PacService.ERROR_0029_QUERY_VALIDATION_FAILED", //$NON-NLS-1$ dme.getLocalizedMessage()), dme); } catch (SQLException e) { throw new PacServiceException(Messages.getErrorString("PacService.ERROR_0029_QUERY_VALIDATION_FAILED", //$NON-NLS-1$ e.getLocalizedMessage()), e); } finally { try { closeAll(conn, stmt, rs, true); } catch (SQLException e) { throw new PacServiceException(e); } } return true; }
From source file:org.pentaho.platform.dataaccess.datasource.wizard.service.impl.DSWDatasourceServiceImpl.java
private IPentahoResultSet executeQuery(String connectionName, String query, String previewLimit) throws QueryValidationException { SQLConnection sqlConnection = null;/*from ww w. j a v a2s. c o m*/ try { int limit = (previewLimit != null && previewLimit.length() > 0) ? Integer.parseInt(previewLimit) : -1; sqlConnection = (SQLConnection) PentahoConnectionFactory.getConnection( IPentahoConnection.SQL_DATASOURCE, connectionName, PentahoSessionHolder.getSession(), new SimpleLogger(DatasourceServiceHelper.class.getName())); sqlConnection.setMaxRows(limit); sqlConnection.setReadOnly(true); return sqlConnection.executeQuery(BEFORE_QUERY + query + AFTER_QUERY); } catch (SQLException e) { String error = "DatasourceServiceImpl.ERROR_0009_QUERY_VALIDATION_FAILED"; if (e.getSQLState().equals("S0021")) { // Column already exists error = "DatasourceServiceImpl.ERROR_0021_DUPLICATE_COLUMN_NAMES"; } logger.error(Messages.getErrorString(error)); throw new QueryValidationException(Messages.getString(error)); } catch (Exception e) { logger.error(Messages.getErrorString("DatasourceServiceImpl.ERROR_0009_QUERY_VALIDATION_FAILED", //$NON-NLS-1$ e.getLocalizedMessage()), e); throw new QueryValidationException(e.getLocalizedMessage(), e); } finally { if (sqlConnection != null) { sqlConnection.close(); } } }
From source file:es.juntadeandalucia.panelGestion.negocio.utiles.JDBCConnector.java
public String getPrimaryKey(String tableName, String schemaName) throws Exception { Exception error = null;/* w ww .j a v a 2s .c o m*/ String primaryKey = null; Connection connection = null; try { DataSource dataSource = poolDataSources.get(schemaId); connection = dataSource.getConnection(); connection.setAutoCommit(false); // get primary keys DatabaseMetaData dataBaseMetaData = connection.getMetaData(); ResultSet rs = dataBaseMetaData.getPrimaryKeys(null, schemaName, tableName); if (rs.next()) { primaryKey = rs.getString("column_name"); } } catch (SQLException e) { error = e; } finally { if (connection != null) { try { if (error != null) { connection.rollback(); } } catch (SQLException se) { log.warn("Se produjo un error al manejar la conexin: ".concat(se.getLocalizedMessage())); } try { connection.close(); } catch (SQLException se) { log.warn("Se produjo un error al intentar cerrar la conexin: " .concat(se.getLocalizedMessage())); } } } if (error != null) { throw error; } return primaryKey; }
From source file:es.juntadeandalucia.panelGestion.negocio.utiles.JDBCConnector.java
/** * This method is the execution base. It depends on the execution type * * @param sql the SQL sentence to execute * @param params the parameters for the query * @param executionType type of the execution * /* w w w . j a va 2 s.c o m*/ * @return the result of the query execution * @throws Exception if some error occurred */ private Object executeBase(String sql, String[] params, ExecutionType executionType) throws Exception { Exception error = null; Object result = null; Connection connection = null; PreparedStatement preparedStmnt = null; try { DataSource dataSource = poolDataSources.get(schemaId); connection = dataSource.getConnection(); connection.setAutoCommit(false); preparedStmnt = connection.prepareStatement(sql); if (params != null) { for (int i = 0; i < params.length; i++) { String param = params[i]; preparedStmnt.setString(i + 1, param); } } result = executePreparedStatement(preparedStmnt, executionType); connection.commit(); } catch (SQLException e) { error = e; } finally { if (preparedStmnt != null) { try { preparedStmnt.close(); } catch (SQLException se2) { log.warn("No se pudo cerrar el statment: ".concat(se2.getLocalizedMessage())); } } if (connection != null) { try { if (error != null) { connection.rollback(); } } catch (SQLException se) { log.warn("Se produjo un error al manejar la conexin: ".concat(se.getLocalizedMessage())); } try { connection.close(); } catch (SQLException se) { log.warn("Se produjo un error al intentar cerrar la conexin: " .concat(se.getLocalizedMessage())); } } } if (error != null) { throw error; } return result; }
From source file:es.juntadeandalucia.panelGestion.negocio.utiles.JDBCConnector.java
/** * TODO// w w w . ja v a 2s.co m * * @param sql * @return */ public String getGeometryType(String sql) throws Exception { Exception error = null; String geometryType = null; Connection connection = null; PreparedStatement preparedStmnt = null; try { DataSource dataSource = poolDataSources.get(schemaId); connection = dataSource.getConnection(); connection.setAutoCommit(false); preparedStmnt = connection.prepareStatement(sql); ResultSet result = preparedStmnt.executeQuery(); while (result.next()) { geometryType = result.getString("type"); } } catch (SQLException e) { error = e; } finally { if (connection != null) { try { if (error != null) { connection.rollback(); } } catch (SQLException se) { log.warn("Se produjo un error al manejar la conexin: ".concat(se.getLocalizedMessage())); } try { connection.close(); } catch (SQLException se) { log.warn("Se produjo un error al intentar cerrar la conexin: " .concat(se.getLocalizedMessage())); } } } if (error != null) { throw error; } return geometryType; }
From source file:es.juntadeandalucia.panelGestion.negocio.utiles.JDBCConnector.java
public List<ColumnVO> getColumnsMetaData(String sql) throws Exception { Exception error = null;/*from ww w . j av a 2s . c om*/ List<ColumnVO> tableColumns = new LinkedList<ColumnVO>(); Connection connection = null; PreparedStatement preparedStmnt = null; try { DataSource dataSource = poolDataSources.get(schemaId); connection = dataSource.getConnection(); connection.setAutoCommit(false); preparedStmnt = connection.prepareStatement(sql); ResultSet rs = preparedStmnt.executeQuery(); ResultSetMetaData rsmd = rs.getMetaData(); for (int i = 1; i <= rsmd.getColumnCount(); i++) { String columnName = rsmd.getColumnName(i); String columnType = rsmd.getColumnTypeName(i); int columnSqlType = rsmd.getColumnType(i); int columnLength = rsmd.getColumnDisplaySize(i); int columnPrecision = rsmd.getPrecision(i); ColumnVO column = new ColumnVO(); column.setNameOnTable(columnName); column.setType(columnType); column.setSqlType(columnSqlType); column.setLength(columnLength); column.setPrecision(columnPrecision); column.setInTable(true); tableColumns.add(column); } } catch (SQLException e) { error = e; } finally { if (preparedStmnt != null) { try { preparedStmnt.close(); } catch (SQLException se2) { log.warn("No se pudo cerrar el statment: ".concat(se2.getLocalizedMessage())); } } if (connection != null) { try { if (error != null) { connection.rollback(); } } catch (SQLException se) { log.warn("Se produjo un error al manejar la conexin: ".concat(se.getLocalizedMessage())); } try { connection.close(); } catch (SQLException se) { log.warn("Se produjo un error al intentar cerrar la conexin: " .concat(se.getLocalizedMessage())); } } } if (error != null) { throw error; } return tableColumns; }
From source file:es.juntadeandalucia.panelGestion.negocio.utiles.JDBCConnector.java
public List<RowVO> getRows(String sql, List<ColumnVO> columns) throws Exception { List<RowVO> rows = new LinkedList<RowVO>(); Exception error = null;/*from w w w . j av a2 s. com*/ Connection connection = null; PreparedStatement preparedStmnt = null; try { DataSource dataSource = poolDataSources.get(schemaId); connection = dataSource.getConnection(); connection.setAutoCommit(false); preparedStmnt = connection.prepareStatement(sql); ResultSet result = preparedStmnt.executeQuery(); while (result.next()) { RowVO row = new RowVO(); for (ColumnVO column : columns) { String columnName = column.getNameOnTable(); String columnValue = null; Object columnValueObj = result.getObject(columnName); if (columnValueObj != null) { columnValue = columnValueObj.toString(); } /* if the column is a geometry type then set the * geometry column name of the row */ if (column.getSqlType() == Types.OTHER) { row.setGeomFieldName(columnName); } row.addField(columnName, columnValue); } rows.add(row); } } catch (SQLException e) { error = e; } finally { if (preparedStmnt != null) { try { preparedStmnt.close(); } catch (SQLException se2) { log.warn("No se pudo cerrar el statment: ".concat(se2.getLocalizedMessage())); } } if (connection != null) { try { if (error != null) { connection.rollback(); } } catch (SQLException se) { log.warn("Se produjo un error al manejar la conexin: ".concat(se.getLocalizedMessage())); } try { connection.close(); } catch (SQLException se) { log.warn("Se produjo un error al intentar cerrar la conexin: " .concat(se.getLocalizedMessage())); } } } if (error != null) { throw error; } return rows; }
From source file:es.juntadeandalucia.panelGestion.negocio.utiles.JDBCConnector.java
public List<ColumnVO> getColumnsMetaDataExceptGeom(String sql, String geometryName) throws Exception { Exception error = null;//from w w w. ja v a 2 s .c o m List<ColumnVO> tableColumns = new LinkedList<ColumnVO>(); Connection connection = null; PreparedStatement preparedStmnt = null; try { DataSource dataSource = poolDataSources.get(schemaId); connection = dataSource.getConnection(); connection.setAutoCommit(false); preparedStmnt = connection.prepareStatement(sql); ResultSet rs = preparedStmnt.executeQuery(); ResultSetMetaData rsmd = rs.getMetaData(); String geometryFieldName = geometryName; if (StringUtils.isEmpty(geometryFieldName)) { geometryFieldName = "the_geom"; } for (int i = 1; i <= rsmd.getColumnCount(); i++) { String columnName = rsmd.getColumnName(i); if (!columnName.equals(geometryFieldName)) { String columnType = rsmd.getColumnTypeName(i); int columnSqlType = rsmd.getColumnType(i); int columnLength = rsmd.getColumnDisplaySize(i); int columnPrecision = rsmd.getPrecision(i); ColumnVO column = new ColumnVO(); column.setNameOnTable(columnName); column.setType(columnType); column.setSqlType(columnSqlType); column.setLength(columnLength); column.setPrecision(columnPrecision); column.setInTable(true); tableColumns.add(column); } } } catch (SQLException e) { error = e; } finally { if (preparedStmnt != null) { try { preparedStmnt.close(); } catch (SQLException se2) { log.warn("No se pudo cerrar el statment: ".concat(se2.getLocalizedMessage())); } } if (connection != null) { try { if (error != null) { connection.rollback(); } } catch (SQLException se) { log.warn("Se produjo un error al manejar la conexin: ".concat(se.getLocalizedMessage())); } try { connection.close(); } catch (SQLException se) { log.warn("Se produjo un error al intentar cerrar la conexin: " .concat(se.getLocalizedMessage())); } } } if (error != null) { throw error; } return tableColumns; }