英文:
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("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);
}
This is my main:
String query = "SELECT begintijd, eindtijd, lesID FROM calendar WHERE lesID = 1";
ArrayList<Map<String, Object>> 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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论