java 通过 resultSet 获取查询的字段
ResultSet 是 Java 中用于存储查询结果的对象,可以通过 ResultSet 对象获取查询结果中的数据字段。
以下是获取 ResultSet 中字段数据的示例代码:
// 获取 ResultSet 对象
ResultSet resultSet = statement.executeQuery("SELECT * FROM users");
// 遍历 ResultSet 中的数据
while (resultSet.next()) {
// 获取 name 字段的值
String name = resultSet.getString("name");
// 获取 age 字段的值
int age = resultSet.getInt("age");
// 输出查询结果
System.out.println(name + ", " + age);
}
在上面的代码中,首先通过 executeQuery 方法执行 SQL 查询语句,得到一个 ResultSet 对象。然后通过 while 循环遍历 ResultSet 中的数据,使用 getString 和 getInt 方法分别获取 name 和 age 字段的值,并将其输出
原文地址: https://www.cveoy.top/t/topic/hkds 著作权归作者所有。请勿转载和采集!