0

我正在尝试使用 sql2o lib 将数据从 mySQL 获取到 java 中的 List。

但由于某种原因,我只是不明白如何正确使用它(看起来像)。

这是错误的代码:

   List<String> returning = new ArrayList<String>();
   String date = "";
   String playerList = "";
   String playerCount = "";
   String playerMax = "";

   con.createQuery(sql)
   .throwOnMappingFailure(true).addColumnMapping("date", date)
   .addColumnMapping("playerList", playerList)
   .addColumnMapping("playerCount", playerCount)
   .addColumnMapping("playerMax", playerMax).executeAndFetch(String.class);

   returning.add(date);
   returning.add(playerList);
   returning.add(playerCount);
   returning.add(playerMax);

这是我得到的错误:

org.sql2o.Sql2oException: Could not map date to any property.
   at org.sql2o.DefaultResultSetHandlerFactory.newResultSetHandler0(DefaultResultSetHandlerFactory.java:199)
   at org.sql2o.DefaultResultSetHandlerFactory.access$200(DefaultResultSetHandlerFactory.java:17)
   at org.sql2o.DefaultResultSetHandlerFactory$5.evaluate(DefaultResultSetHandlerFactory.java:160)
   at org.sql2o.DefaultResultSetHandlerFactory$5.evaluate(DefaultResultSetHandlerFactory.java:156)
   at org.sql2o.tools.AbstractCache.get(AbstractCache.java:49)
   at org.sql2o.DefaultResultSetHandlerFactory.newResultSetHandler(DefaultResultSetHandlerFactory.java:173)
   at org.sql2o.PojoResultSetIterator.<init>(PojoResultSetIterator.java:20)
   at org.sql2o.Query$14.iterator(Query.java:547)
   at org.sql2o.Query.executeAndFetch(Query.java:588)
   at org.sql2o.Query.executeAndFetch(Query.java:574)
   at lol.discordbot.database.QueryServerInfo.getCurrent(QueryServerInfo.java:31)
   at lol.discordbot.command.Query.execute(Query.java:20)
   at lol.discordbot.command.CommandsListener.onMessageReceived(CommandsListener.java:39)
4

1 回答 1

0

我认为您误解了列映射是什么。列映射用于将列名称映射到对象字段名称。

您应该首先创建一个数据类来保存查询结果。从上面的代码中,我假设您正在尝试获取玩家。

public class Player {
   public String date;
   public String playerList;
   public String playerCount;
   public String playerMax
}

(考虑使用更好的数据类型。日期为日期,int 为计数等)

然后就可以使用sql2o来获取数据了

List<Player> players = con.createQuery(sql).executeAndFetch(Player.class);
于 2018-02-09T09:46:12.860 回答