天天看点

if、else问题问题

问题

if(rst.next()==false)
	            	 System.out.println("联结失败......");
	             while(rst.next()){
	            	 System.out.println("联结成功......");
	             }
           

【疑惑】:看似是要么输出联结失败,要么输出联结成功

【解答】:实际没有输出,因为当第一个next()的值为true时,就没有输出,最后报错超出范围(多读了一次next())

【修改】:用if else语句,能对第一个next()进行判别

if(rst.next()==false)
	            	 System.out.println("联结失败......");
	             else{
	            	do{
	            	
	            	 System.out.println("联结成功......");
	            	
	                 System.out.println("title:"+rst.getString("title")+"  "+"price:"+rst.getString("price")+"  "+"person:"+rst.getString("person")+
	                		"  title2:"+rst.getString("title2")+"  "+"price2:"+rst.getString("price2")+"\n");
	              } while(rst.next());
           

2.为什么要用do while

【解答】:do是继续对上一个next()判别,否则超出范围