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());
}
}
Data class:
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
Do I need to use ResultSet instead?