java - SQL 查询结果到 Java 数组
问题描述
我想将查询结果读入数组,并想在 if 条件下检查数组。在退货中查询
SELECT COLUMN
FROM Table;
OUT PUT:
A
B
C
D
F
想将上述结果存储在java中的数组中并在if条件中使用。
解决方案
它基本上会是这样的:
ResultSet rs = statement.executeQuery(yourSQLQueryString); // Get your ResultSet from Database
rs.last(); // Place the record pointer onto the last row
int counter = res.getRow(); // Get the row number (there's your count)
rs.first(); // Place the record pointer onto the first row for the while loop
String[] myArray = new String[counter]; // Declare and Initialize your array
counter = 0; // Reset counter to 0 so as to act as a Index incrementer
// Iterate through the ResultSet and fill Array
while (rs.next()) {
myArray[counter] = rs.getString(columnNumber_OR_ColumnNameString);
counter++;
}
// See what's in Array...
for (int i = 0; i < myArray.length; i++) {
System.out.println(myArray[i]);
}
您可以处理异常。
推荐阅读
- android - 无法为 com.android.build.gradle.internal.dsl.NdkOptions 类型的对象获取未知属性“armeabi”
- python-2.7 - if 语句中的搜索字符串仅匹配部分项目
- c# - 如何使用 ASP.NET Core 从 DbContext 中的 JWT 获取用户名?
- typescript - 从另一个类型中删除一个类型的属性
- actions-on-google - Dialogflow JSON V2 的丰富消息
- python - Pandas Dataframe 到特定格式的字典列表
- c++ - c++ dllimport:函数调用返回错误结果
- arrays - 如何将可变长度数组常量作为参数传递?
- react-native - 未找到 React Native ios 'GoogleMaps/GoogleMaps.h' 文件
- google-sheets - 将脚本分配给 Google 文档中的按钮