[英]Converting ResultSet to array of to int
I am trying to convert ResultSet
to array of int
, that because the method I have returns int[]
: 我试图将ResultSet
转换为int
数组,因为我拥有的方法返回int[]
:
public static int[] GetPathIDsFromDB (String Dbnum, int objectid) throws SQLException{
String selectSQL = "SELECT ID FROM " + Dbnum + ".tabName where ObjectID=?";
dbConnection = getDBConnection();
preparedStatement = dbConnection.prepareStatement(selectSQL);
preparedStatement.setInt(1 , objectid);
ResultSet rs = preparedStatement.executeQuery();
//int [] rsIDs = new rs.count;
int [] rsIDs = null;
while (rs.next()) {
rsIDs=rs.getInt(1); // the error int cannot be converted to int[]
}
return rsIDs;
}
How can I convert that ? 我该如何转换呢?
Personally, I would put them into a list, then convert to an array at the end. 就个人而言,我会将它们放入列表中,然后最后转换为数组。
List<Integer> rsIds = new ArrayList<Integer>();
while (rs.next()) {
rsIds.add(rs.getInt(1));
}
return rsIds.toArray(new Integer[](rsIds.size());
It should automatically convert the Integer[] to an int[] for you via autoboxing. 它应该通过自动装箱自动将Integer []转换为int []。
Seeing as how I'm an idiot, and that doesn't work, this is how I would then do it: 看到我是个白痴,那是行不通的,这就是我要做的事情:
rs.last();
int[] rsIds = new int[rs.getRow()];
rs.beforeFirst();
int counter = 0;
while (rs.next()) {
rsIds[counter] = rs.getInt(1);
counter++;
}
This won't work if the ResultSet is of type TYPE_FORWARD_ONLY, you would need to change the scroll type for it to work. 如果ResultSet的类型为TYPE_FORWARD_ONLY,则此方法将不起作用,您需要更改滚动类型以使其起作用。
You need to add each result from the ResultSet
to the array and for that you need a counter variable as the index of the array. 您需要将每个结果从ResultSet
添加到数组,为此,您需要一个计数器变量作为数组的索引。
Also, initialize your int[]
array properly because currently its just initialized to null
. 另外,正确初始化int[]
数组,因为当前它刚刚初始化为null
。
int[] rsIDs = new int[size]; // decide the size
int counter = 0; // counter
while (rs.next()) {
rsIDs[counter++] = rs.getInt(1);
}
And if you do not know the size before hand, then it is better to use an ArrayList
instead of an array
. 而且,如果您事先不知道大小,那么最好使用ArrayList
而不是array
。
There are two ways to solve the problem: 有两种方法可以解决此问题:
Execute another query to extract size first, extract size and then create array of this size: 执行另一个查询以首先提取大小,提取大小,然后创建此大小的数组:
String sizeQuery = "SELECT count(*) FROM " + Dbnum + ".tabName where ObjectID=?";
Create an List
of Integer
, add result to it (as M21B8 described), and then convert it to an int[]
, as: 创建一个Integer
List
,向其中添加结果(如M21B8所述),然后将其转换为int[]
,如下所示:
int size = list.size(); int[] result = new int[size]; for (int i = 0; i < size; i++) { result[i] = list.get(i); } return result;
You could make your life easier and use an ArrayList: 您可以使生活更轻松,并使用ArrayList:
ResultSet rs = preparedStatement.executeQuery();
ArrayList<Integer> rsIDs = new ArrayList<>();
while (rs.next()) {
rsIDs.add(rs.getInt(1));
}
If you still want it in an array you can create it afterwards. 如果仍然希望将其放在数组中,则可以在以后创建它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.