如果try或catch语句中有return,会先执行finally再执行return,但下面的一段程序我编译后提示缺少return语句,请教各位是怎么回事,我开始以为只有try中有return确实有可能不执行,于是我又在catch中加了一个return,但提示还是一样。代码如下:
public String printAllCustomers() throws SQLException{
Connection con=null;
Statement stmt=null;
ResultSet rs=null;
String s=null;
StringBuffer sb=new StringBuffer();
try{
con=provider.getConnection();
stmt=con.createStatement();
//查询记录
rs= stmt.executeQuery("SELECT ID,NAME,AGE,ADDRESS from CUSTOMERS");
//输出查询结果
while (rs.next()){
long id = rs.getLong(1);
String name = rs.getString(2);
int age = rs.getInt(3);
String address = rs.getString(4);
//返回结果
sb.append("id="+id+",name="+name+",age="+age+",address="+address);
s=sb.toString();
return s;
}
}catch(SQLException e){
return s;
}
finally{
closeResultSet(rs);
closeStatement(stmt);
closeConnection(con);
}
}
public String printAllCustomers() throws SQLException{
Connection con=null;
Statement stmt=null;
ResultSet rs=null;
String s=null;
StringBuffer sb=new StringBuffer();
try{
con=provider.getConnection();
stmt=con.createStatement();
//查询记录
rs= stmt.executeQuery("SELECT ID,NAME,AGE,ADDRESS from CUSTOMERS");
//输出查询结果
while (rs.next()){
long id = rs.getLong(1);
String name = rs.getString(2);
int age = rs.getInt(3);
String address = rs.getString(4);
//返回结果
sb.append("id="+id+",name="+name+",age="+age+",address="+address);
s=sb.toString();
return s;
}
}catch(SQLException e){
return s;
}
finally{
closeResultSet(rs);
closeStatement(stmt);
closeConnection(con);
}
}
楼主【topcs】截止到2008-07-11 09:10:42的历史汇总数据(不包括此帖):
发帖的总数量:0 发帖的总分数:0 每贴平均分数:0
回帖的总数量:0 得分贴总数量:0 回帖的得分率:0%
结贴的总数量:0 结贴的总分数:0
无满意结贴数:0 无满意结贴分:0
未结的帖子数:0 未结的总分数:0
结贴的百分比:---------------------结分的百分比:---------------------
无满意结贴率:---------------------无满意结分率:---------------------
如何结贴请参考这里:http://topic.csdn.net/u/20080501/09/ef7ba1b3-6466-49f6-9d92-36fe6d471dd1.html
Connection con=null;
Statement stmt=null;
ResultSet rs=null;
String s=null;
StringBuffer sb=new StringBuffer();
try{
con=provider.getConnection();
stmt=con.createStatement();
//查询记录
rs= stmt.executeQuery("SELECT ID,NAME,AGE,ADDRESS from CUSTOMERS");
//输出查询结果
while (rs.next()){ //BEGIN----------------------------------------
long id = rs.getLong(1);
String name = rs.getString(2);
int age = rs.getInt(3);
String address = rs.getString(4);
//返回结果
sb.append("id="+id+",name="+name+",age="+age+",address="+address);
s=sb.toString();
return s;
} //END----------------------------------------
}catch(SQLException e){
return s;
}
finally{
closeResultSet(rs);
closeStatement(stmt);
closeConnection(con);
}
}
看我begin end之间,你把return 放在循环里面了,老大。你放在END之后循环之外就可以了,要是你没有进入循环就不能返回了。所以会报搓啊