I don’t get into a loop even though the ResultSet is full.

huangapple go评论138阅读模式
英文:

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:

I don’t get into a loop even though the ResultSet is full.

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()) { 

              [...]
            }

I don’t get into a loop even though the ResultSet is full.

I don’t get into a loop even though the ResultSet is full.

答案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.

huangapple
  • 本文由 发表于 2020年8月14日 20:42:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/63412987.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定