我有以下代码:

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);

查询值:
插入级别1(平铺ID、平铺数据、平铺级别、平铺列、平铺行、平铺图像格式、平铺源)值(0,?,1,0,0,'JPG','null');
我得到的错误是:
org.postgresql.util.PSQLException:列索引超出范围:2,列数:1。
我的桌子:
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)

有什么想法吗?

最佳答案

您尚未发布stacktrace,但您的错误似乎出现在:

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

这是因为只有一个参数要绑定到:
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');

参数在值列表中的位置2并不重要。重要的是它是第一名。所以你的代码应该是,
statement.setBytes(1, tile.getData());

关于java - org.postgresql.util.PSQLException:列索引超出范围:2,列数:1,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/37714583/

10-12 06:17