我试图将数据库中某些表的值放到HashMap
。这是我对resultSet的处理方式。我在我的数据库中有下表:
CREATE TABLE myguests (
id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
firstname VARCHAR(30) NOT NULL
)
以及它的映射类:
@Entity
@Table
public class MyGuests {
@Column
@Id
public Integer id;
@Column
public String firstname;
}
以下是我如何提取表格:
String basicQuery = "SELECT * FROM myguests";
resultSet = statement.executeQuery(basicQuery);
while (resultSet.next()) {
Map<Field, Object> mapInfo = new HashMap<>();
Field[] declaredFields = MyGuests.class.getDeclaredFields();
for (int i = 0; i < declaredFields.length; i++) {
mapInfo.put(declaredFields[i],
resultSet.getString(declaredFields[i].getName().toString().toLowerCase()));
}
}
但是我收到了错误:
mapInfo.put(declaredFields[i],
resultSet.getString(declaredFields[i].getName().toString().toLowerCase()));
错误:
Exception in thread "main" java.sql.SQLException: Column 'id' not found.
我无法理解为什么结果集无法获取,我在数据库中查看它显然已填充并且具有名为id,firstname的列