首页 > 解决方案 > SQL 查询结果到 Java 数组

问题描述

我想将查询结果读入数组,并想在 if 条件下检查数组。在退货中查询

SELECT COLUMN
 FROM Table;
OUT PUT:
A
B
C
D
F 

想将上述结果存储在java中的数组中并在if条件中使用。

标签: java

解决方案


它基本上会是这样的:

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]);
}

您可以处理异常。


推荐阅读