请求表中的两个日期时间,但只获取到一个日期时间。

huangapple go评论75阅读模式
英文:

Requesting two datetimes out of a table, but only getting one

问题

以下是您要翻译的内容:

我正在尝试从数据库中获取两个日期时间值,但每次只能获取其中一个。我的意思是,如果我从查询中删除我获取的列(begintijd),我可以获取另一列(eindtijd)。

con = DriverManager.getConnection("jdbc:mysql://localhost:3306/db", "username", "password");
resultTable = new ArrayList<>();
Statement statement = con.createStatement();
statement.execute(query)
ResultSet result = statement.getResultSet();
ResultSetMetaData resultData = result.getMetaData();
while (result.next()) {
  Map<String, Object> row = new HashMap<>();
  for (int i = 1; i <= resultData.getColumnCount(); i++) {
    String type = resultData.getColumnTypeName(i);
    switch (type) {
      case "INT":
        row.put(resultData.getColumnName(i), result.getInt(i));
        break;
      case "VARCHAR":
      case "CHAR":
      case "TEXT":
        row.put(resultData.getColumnName(i), result.getString(i));
        break;
      case "DATETIME":
        row.put(resultData.getColumnName(i), new java.util.Date(result.getTimestamp(i).getTime()));
        break;
      default:
        throw new IllegalArgumentException("Data type not supported");
    }
    i++;
  }
  resultTable.add(row);
}

这是我的主要部分:

String query = "SELECT begintijd, eindtijd, lesID FROM calendar WHERE lesID = 1";
ArrayList<Map<String, Object>> resultTable = Database.executeStatement(query);
System.out.println(resultTable);

结果是这样的(没有错误):

[{begintijd=Tue Apr 07 08:30:00 CEST 2020, lesID=1}]

这是我的数据库的截图:

请求表中的两个日期时间,但只获取到一个日期时间。

我的java.util.Date实现有问题吗?

英文:

I'm trying to get two datetime values out of a database, but I'm only get one of them at a time. By that I mean that if I remove the column I do get (begintijd) from the query I do get the other column (eindtijd).

con = DriverManager.getConnection(&quot;jdbc:mysql://localhost:3306/db&quot;, &quot;username&quot;, &quot;password&quot;);
resultTable = new ArrayList&lt;&gt;();
Statement statement = con.createStatement();
statement.execute(query)
ResultSet result = statement.getResultSet();
ResultSetMetaData resultData = result.getMetaData();
while (result.next()) {
  Map&lt;String, Object&gt; row = new HashMap&lt;&gt;();
  for (int i = 1; i &lt;= resultData.getColumnCount(); i++) {
    String type = resultData.getColumnTypeName(i);
    switch (type) {
      case &quot;INT&quot;:
        row.put(resultData.getColumnName(i), result.getInt(i));
        break;
      case &quot;VARCHAR&quot;:
      case &quot;CHAR&quot;:
      case &quot;TEXT&quot;:
        row.put(resultData.getColumnName(i), result.getString(i));
        break;
      case &quot;DATETIME&quot;:
        row.put(resultData.getColumnName(i), new java.util.Date(result.getTimestamp(i).getTime()));
        break;
      default:
        throw new IllegalArgumentException(&quot;Data type not supported&quot;);
    }
    i++;
  }
  resultTable.add(row);
}

This is my main:

String query = &quot;SELECT begintijd, eindtijd, lesID FROM calendar WHERE lesID = 1&quot;;
ArrayList&lt;Map&lt;String, Object&gt;&gt; resultTable = Database.executeStatement(query);
System.out.println(resultTable);

As a result I get this (no errors):

[{begintijd=Tue Apr 07 08:30:00 CEST 2020, lesID=1}]

Here's a screenshot of my database:

请求表中的两个日期时间,但只获取到一个日期时间。

Is there something wrong with my implementation of java.util.Date, or what?

答案1

得分: 0

所以我很笨。
我使用了一个递增的for循环,然后在每个周期结束时再次增加了索引,从而跳过了偶数列。

英文:

So I'm dumb.
I used an incremental for loop, and then incremented the index again at the end of each cycle, essentially skipping the even columns.

huangapple
  • 本文由 发表于 2020年4月7日 21:44:12
  • 转载请务必保留本文链接:https://go.coder-hub.com/61081461.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定