英文:
I don't get into a loop even though the ResultSet is full
问题
我有一个非空的结果集,它包含6列。但是如果我想使用循环,什么都不会发生。
如果我在SQL Server Management Studio中使用与Java代码中相同的参数调用存储过程,我会得到一个结果:
几分钟前一切都正常
// 那行不起作用,因为我无法进入循环
if (con != null) {
String an_id = "bkoubik";
String AS_Aufruf = "exec BfV_Web.sp_Anwender_Start\n@AnwenderID = ?";
try {
PreparedStatement STMT = con.prepareStatement(AS_Aufruf);
STMT.setString(1, an_id);
ResultSet rs = STMT.executeQuery();
// 这个if语句没有进入,那为什么while循环不起作用呢?
if (!rs.next()) {
System.out.println("no data");
}
ResultSetMetaData meta = rs.getMetaData();
int intRS = meta.getColumnCount();
// 这个循环没有进入
while (rs.next()) {
// ...
}
}
}
英文:
I have a result set which is not empty. It contains 6 columns. But if I want to use the loop, nothing happens.
If I call the stored procedure in SQL Server Management Studio with the same parameters as in the Java code, I got a result:
A few minutes ago everything worked
// That doesn't work because I can't get into the loop
if (con != null) {
String an_id = "bkoubik";
String AS_Aufruf = "exec BfV_Web.sp_Anwender_Start\n@AnwenderID = ?";
try {
PreparedStatement STMT = con.prepareStatement(AS_Aufruf);
STMT.setString(1, an_id);
ResultSet rs = STMT.executeQuery();
//THIS IF STATEMENT IS NOT ENTERING, SO WHY THEN THE WHILE LOOP
IS NOT WORKING ?
if (!rs.next() ) {
System.out.println("no data");
}
ResultSetMetaData meta = rs.getMetaData();
int intRS = meta.getColumnCount();
//THE LOOP IS NOT ENTERING
while (rs.next()) {
[...]
}
答案1
得分: 1
intRS
是列数,而不是行数。你的 rs
可能是空的。
英文:
intRS is the column count, not the row count. Your rs is likely empty.
答案2
得分: 0
The first call to rs.next()
(before the loop) already consumes the one and only row, so the next call (in the while
) will return false.
英文:
The first call to rs.next()
(before the loop) already consumes the one and only row, so the next call (in the while
) will return false.
专注分享java语言的经验与见解,让所有开发者获益!
评论