如何使用sql2o从SQL获取数据?

时间:2018-02-09 00:45:41

标签: java mysql sql2o

我正在尝试使用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)

1 个答案:

答案 0 :(得分:0)

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

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

$('#my-tree').jstree({
  core: { multiple: false },
  plugins: ['multiselect']
})

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

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

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