org.postgresql.util.PSQLException:列索引超出范围:2,列数:1

时间:2016-06-08 23:29:05

标签: java postgresql jdbc

我有以下代码:

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

查询值:

  

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

错误我得到了:

  

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)

有什么想法吗?

2 个答案:

答案 0 :(得分:3)

您还没有发布您的堆栈跟踪,但您的错误似乎从此处出现:

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

答案 1 :(得分:0)

感谢e4c5的答案,我将代码修改如下:

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;
PreparedStatement statement = conn.prepareStatement(query);
statement.setBytes(1, tile.getData());
int result = statement.executeUpdate();
statement.close();
return result;
  

参数在值列表中位于第2位的事实并不重要。这是第一个重要的事实。