org.postgresql.util.PSQLException: The column index is out of range: 2, number of columns: 1

10,509

You haven't posted your stacktrace but your error seems to appear from this:

statement.setBytes(2, tile.getData());

Which is because you have only one parameter to bind to:

INSERT INTO level1 (TILE_ID,TILE_DATA,TILE_LEVEL,TILE_COLUMN,TILE_ROW,TILE_IMAGE_FORMAT,TILE_SOURCE) VALUES (0,?,1,0,0,'JPG','null');

The fact that the parameter is at position 2 in the values list is not what counts. It's the fact that it's the first place holder that matters. So your code should be,

statement.setBytes(1, tile.getData());
Share:
10,509
Menelaos Kotsollaris
Author by

Menelaos Kotsollaris

Any application that can be written in JavaScript, will eventually be written in JavaScript. Rule of Least Power - Jeff Atwood When you strive to comprehend your code, you create better work and become better at what you do. The code isn’t just your job anymore, it’s your craft. You don't know JavaScript - Kyle Simpson Every time you see duplication in the code, it represents a missed opportunity for abstraction. When you find duplicated code, do the right thing. Delete it from the system. Clean Code - Robert Cecil Martin Programming: Love It or Leave It. Jeff Atwood The cleaner and nicer the program, the faster it is going to run. And if it does not, it will be easy to make it fast. Effective Java - Joshua Bloch Program to Interface, not to Implementation. GOF - Elements of Reusable Object Oriented Software; Erich Gamma et al. To BLOB or Not To BLOB? Russel Sears et al. Every object should be immutable. HrBill32 Take your time to understand the language prior pushing code. Tim Beatty Do whatever keeps you motivated. Evan Digby

Updated on July 25, 2022

Comments

  • Menelaos Kotsollaris
    Menelaos Kotsollaris almost 2 years

    I have the following code:

    String insert = "INSERT INTO " + tableName +
                    "(" + COLUMNS.TILE_ID + "," + COLUMNS.TILE_DATA + "," +
                    COLUMNS.TILE_LEVEL + "," + COLUMNS.TILE_COLUMN +
                    "," + COLUMNS.TILE_ROW +
                    "," + COLUMNS.TILE_IMAGE_FORMAT + "," + COLUMNS.TILE_SOURCE +
                    ")";
    String values = id + ",?" + "," +
                    tile.getLevel() + "," + tile.computeColumn() + "," +
                    tile.computeRow() + ",\'" + tile.getFileType().toUpperCase() +
                    "\'," + "\'" +
                    tile.getSource() + "\');";
            String query = insert + " VALUES (" + values;
            System.out.println(query);
            PreparedStatement statement = conn.prepareStatement(query);
            statement.setBytes(2, tile.getData());
            return this.conn.createStatement().executeUpdate(query);
    

    The query value:

    INSERT INTO level1 (TILE_ID,TILE_DATA,TILE_LEVEL,TILE_COLUMN,TILE_ROW,TILE_IMAGE_FORMAT,TILE_SOURCE) VALUES (0,?,1,0,0,'JPG','null');

    The error I get:

    org.postgresql.util.PSQLException: The column index is out of range: 2, number of columns: 1.

    My Table:

    tile_id bigint NOT NULL,
      tile_data bytea,
      tile_level smallint,
      tile_row integer,
      tile_column integer,
      tile_image_format image_format,
      tile_source character varying(30),
      CONSTRAINT level10_pkey PRIMARY KEY (tile_id)
    

    Any ideas?