OtterMind/Chat2DB · error · IllegalStateException

Failed to create table for excel import:

Error message

Failed to create table for excel import: 

What it means

IllegalStateException 'Failed to create table for excel import: <msg>' from ReadHeaderListener.createTable when the generated CREATE TABLE statement (buildCreateTable) throws a SQLException on the H2 connection.

Source

Thrown at chat2db-community-server/chat2db-community-domain/chat2db-community-domain-core/src/main/java/ai/chat2db/community/domain/core/impl/excel/ReadHeaderListener.java:129

        }
        sb = new StringBuilder(sb.substring(0, sb.length() - 2));
        sb.append("\n );");
        return sb.toString();
    }

    private void createTable(ExcelCheckResponse.Sheet sheet) {
        String sql = buildCreateTable(sheet);
        if (StringUtils.isEmpty(sql)) {
            return;
        }
        Statement statement = null;
        ResultSet resultSet = null;
        try {
            statement = this.connection.createStatement();
            statement.execute(sql);
        } catch (SQLException e) {
            log.error("SQLException", e);
            throw new IllegalStateException("Failed to create table for excel import: " + e.getMessage(), e);
        } finally {
            try {
                if (resultSet != null) {
                    resultSet.close();
                }
                if (statement != null) {
                    statement.close();
                }
            } catch (SQLException e) {
                log.info("SQLException", e);
            }
        }
    }

    @Override
    public void invoke(Map<Integer, Object> data, AnalysisContext context) {
        if (currentSheet == null) {
            setCurrentSheet(context.readSheetHolder().getSheetNo());

View on GitHub (pinned to 5ee1e990e7)

Solutions

  1. Inspect the wrapped SQLException message for the SQLSTATE (duplicate name, syntax, etc.) and fix the header/type accordingly.
  2. Sanitize/unique-ify column names in the Excel sheet before import.
  3. Drop any pre-existing import table for this sheet, or let the importer use a fresh name.
  4. Confirm the H2 connection used by the listener is still open.
Defensive patterns

Strategy: try-catch

Validate before calling

// preview the generated SQL and validate identifiers before execute
String sql = buildCreateTable(sheet);
if (sql == null || sql.isEmpty()) return;

Try / catch

try { statement.execute(sql); }
catch (SQLException e) {
    log.error("create table failed: {}", sql, e);
    throw new IllegalStateException("Failed to create table for excel import: " + e.getMessage(), e);
}

Prevention

When it happens

Trigger: execute(sql) fails because the generated table/column names contain illegal characters, duplicate table name, reserved keyword as column, unsupported column type mapping, or the H2 connection is closed.

Common situations: Excel header row with blank/duplicate/special-character column names; a sheet previously imported; column type inference producing an unsupported SQL type; connection dropped.

Related errors


AI-assisted analysis of OtterMind/Chat2DB@5ee1e990e7 (2026-08-14). Data as JSON: /api/errors/68ceeda1521d56a2. Report an issue: GitHub.