+-

我正在分配数据库,但遇到了问题.
我们需要一次打印3行,在3行的每一个倍数处停止并提示用户继续或中断.
我们需要一次打印3行,在3行的每一个倍数处停止并提示用户继续或中断.
这是将Java JDBC与Oracle数据库一起使用.
if (rset.next()){
// At this point, there are reviews to show
int rinc = 0;
String seenextreviews = "y";
System.out.println("Rating - Text - Reviewer - RDate");
while(rset.next()){
// Iterate through the result set
System.out.println("Increment: " + rinc);
if(rinc % 3 == 0) {
// Displayed 3 reviews
seenextreviews = "maybe";
while(!seenextreviews.equals("y") && !seenextreviews.equals("n")){
seenextreviews = console.readLine("See next reviews (y/n)? ");
if (!seenextreviews.equals("y") && !seenextreviews.equals("n")){
System.out.println("Invalid input '" + seenextreviews + "'");
}
}
if (seenextreviews.equals("n")) {
System.out.println("Breaking...");
break;
} else {
System.out.println("Continue...");
}
}
System.out.println(rset.getInt("rating") + " - " +
rset.getString("text") + " - " +
rset.getString("reviewer") + " - " +
rset.getString("rdate"));
rinc = rinc + 1;
}
}
对于给定的代码,仅“ if(rset.next())”内的所有“ System.out.println(values)”都为System.out.println(“ Invalid input’” seeedreviews“’”);和System.out.println(rset.getInt(“ rating”)…);打印,而其他所有内容都不打印.
但是,console.readLine(“ text”)仍会在询问用户输入之前打印文本.
为什么我的打印报表不打印?将查询更改为一次选择3行并调用多个查询的唯一替代方法是吗?
最佳答案
For the given code, all of the ‘System.out.println(values)’ within ‘if (rset.next())’ only
从ResultSet#next
Moves the cursor froward one row from its current position.
因此,看起来您的查询返回了一行,并且您在if(rset.next())条件下进行了读取,而转到了从第二行到末尾读取的while(rset.next())时,但是由于您没有第二行要读取,因此while循环结束.
可能的解决方案:使用标志来打印标题:
int rinc = 0;
String seenextreviews = "y";
while(rset.next()) {
//this serves as flag
if (rinc == 0) {
System.out.println("Rating - Text - Reviewer - RDate");
}
//rest of your code logic...
}
点击查看更多相关文章
转载注明原文:java-resultset.next()循环中的打印语句不打印 - 乐贴网