本文介绍了如何使用JDBI将2d数组从PostgreSQL DB转换为java 2d数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何将Postgres DB中的2d char数组转换为本地Java char [] []?这是我基于的尝试:

How can I convert a 2d char array from my Postgres DB to a native Java char[][]? This is my attempt based on this answer:

import java.sql.Array;
import java.sql.ResultSet;
import java.sql.SQLException;

import org.skife.jdbi.v2.StatementContext;
import org.skife.jdbi.v2.tweak.ResultSetMapper;

public class GameMapper implements ResultSetMapper<Game>{
    public Game map(int index, ResultSet resultSet, StatementContext statementContext) throws SQLException
    {
        Array board = resultSet.getArray("BOARD");
        return new Game(resultSet.getInt("ID"),
                        resultSet.getInt("WHOSE_TURN"),
                        resultSet.getInt("WINNER"),
                        (char[][]) board.getArray());
    }
}

数据类:

public class Game {
    protected int id;
    protected int whoseTurn;
    protected int winner;
    protected char[][] board;

    public Game(int id, int turn, int winner, char[][] board ) {
        this.id=id;
        this.whoseTurn=turn;
        this.winner=winner;
        this.board=board;
    }

    @JsonProperty
    public int getId() {
        return id;
    }

    @JsonInclude(Include.NON_NULL)
    public int getWhoseTurn() {
        return whoseTurn;
    }

    @JsonInclude(Include.NON_NULL)
    public int getWinner() {
        return winner;
    }

    public char[][] getBoard() {
        return board;
    }
}

DAO:

@RegisterMapper(GameMapper.class)
public interface GameDAO {

    @SqlUpdate("create table if not exists GAMES (ID integer, WHOSE_TURN varchar(10), WINNER varchar(10), BOARD char(1)[][])")
    void createTableIfNotExists();

    @SqlUpdate("insert into GAMES (ID, WHOSE_TURN, WINNER, BOARD) values (:id, :whoseTurn, :winner, :board)")
    void insert(@BindBean Game game);
}

但这会导致强制转换错误:

But this results in a cast error:

java.lang.ClassCastException: [[Ljava.lang.String; cannot be cast to [[C

我是否需要使用代替?

推荐答案

[数组。因此 [[是array-of array-of,即两个类型名称都是2-d数组。

[ is array-of. So [[ is array-of array-of, i.e. both type names are 2-d arrays.

L 表示引用类型,后跟类名。

L indicates a reference type, followed by the class name.

C 是原始类型 char

因此在这里,我们看到您正在尝试转换两个- java.lang.String 的二维数组到 char 的二维数组。那没有任何意义。看起来JDBI将Java数据类型 char 映射到SQL字符串类型,例如 character 字符变化(通过检查数据库中的表进行验证)。如果是这样,JDBC驱动程序将从数据库中以 java.lang.String 返回。

So here, we see that you are attempting to cast two-dimensional array of java.lang.String to a two-dimensional array of char. That doesn't make any sense. It looks like JDBI maps the Java data type char to an SQL string type like character or character varying, (verify by checking the table in the database). If so, the JDBC driver will return that from the database as java.lang.String.

所以您需要覆盖该映射以使用数据库数据类型 char -一个字符-或者更简单地,断言字符串是一个字符长,然后将它们解压缩为一个字符字符。因此,您必须复制阵列。

So you need to either override that mapping to use the database data type "char" - a single character - or, more simply, assert that the strings are one character long and unpack them into a character. So you'll have to copy the arrays.

这篇关于如何使用JDBI将2d数组从PostgreSQL DB转换为java 2d数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 04:09