Search in sources :

Example 1 with DatabaseException

use of com.github.noraui.exception.data.DatabaseException in project NoraUi by NoraUi.

the class DBDataProvider method initColumns.

private void initColumns() throws DatabaseException, TechnicalException {
    columns = new ArrayList<>();
    String sqlRequest;
    try {
        final Path file = Paths.get(dataInPath + scenarioName + ".sql");
        sqlRequest = new String(Files.readAllBytes(file), Charset.forName(Constants.DEFAULT_ENDODING));
        sqlSanitized4readOnly(sqlRequest);
    } catch (final IOException e) {
        throw new TechnicalException(Messages.getMessage(TechnicalException.TECHNICAL_ERROR_MESSAGE) + e.getMessage(), e);
    }
    try (Connection connection = getConnection();
        PreparedStatement statement = connection.prepareStatement(sqlRequest);
        ResultSet rs = statement.executeQuery()) {
        if (rs.getMetaData().getColumnCount() < 1) {
            throw new DatabaseException("Input data is empty. No column have been found.");
        }
        for (int i = 1; i <= rs.getMetaData().getColumnCount(); i++) {
            columns.add(rs.getMetaData().getColumnLabel(i));
        }
    } catch (final SQLException e) {
        throw new TechnicalException(Messages.getMessage(TechnicalException.TECHNICAL_ERROR_MESSAGE) + e.getMessage(), e);
    }
    resultColumnName = Messages.getMessage(ResultColumnNames.RESULT_COLUMN_NAME);
}
Also used : Path(java.nio.file.Path) TechnicalException(com.github.noraui.exception.TechnicalException) SQLException(java.sql.SQLException) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement) IOException(java.io.IOException) DatabaseException(com.github.noraui.exception.data.DatabaseException)

Aggregations

TechnicalException (com.github.noraui.exception.TechnicalException)1 DatabaseException (com.github.noraui.exception.data.DatabaseException)1 IOException (java.io.IOException)1 Path (java.nio.file.Path)1 Connection (java.sql.Connection)1 PreparedStatement (java.sql.PreparedStatement)1 ResultSet (java.sql.ResultSet)1 SQLException (java.sql.SQLException)1