2012-07-19 65 views
1

在我創建包含函數,這是我希望返回通過執行一個Oracle query.I產生的特定值的DAO類Servlet程序試圖像:如何從一個dao函數返回一個整數?

public int timeofdayafternoonthsmon(Getset g) throws ClassNotFoundException, SQLException { 
    // TODO Auto-generated method stub 
    Connection con=Dbconnection.getConnection(); 
    String userid=g.getuserid(); 
    PreparedStatement pstmt=con.prepareStatement("select count(timeofday) from mealdb where timeofday=? and userid=?"); 
    pstmt.setString(1,"Afternoon"); 
    pstmt.setString(2,userid); 
    int no=pstmt.executeUpdate(); 
    System.out.println(""+no); 
    return no; 
} 

但問題是它返回1作爲(我猜)成功。但我希望它返回執行此查詢的結果。

+1

我猜還有另一個函數,而不是executeupdate可以服務我的理由。 – Mistu4u 2012-07-19 07:26:11

回答

2
public int timeofdayafternoonthsmon(Getset g) throws ClassNotFoundException, SQLException { 
    // TODO Auto-generated method stub 
    Connection con=Dbconnection.getConnection(); 
    String userid=g.getuserid(); 
    PreparedStatement pstmt=con.prepareStatement("select count(timeofday) from mealdb where timeofday=? and userid=?"); 
    pstmt.setString(1,"Afternoon"); 
    pstmt.setString(2,userid); 

    // execute a query, not an update statement and fetch the result set 
    ResultSet rs = stmt.executeQuery(); 
    // position to the first result, there must be one since count(timeofday) returns at least 0 
    rs.next(); 

    System.out.println(""+rs.getInt(1)); 
    return rs.getInt(1); 
} 
+0

在這方面@BorisPavlović我想澄清一下,何時使用更新以及何時使用查詢? – Mistu4u 2012-07-19 07:34:34

+1

查詢 - 選擇;更新 - 插入,更新,刪除 – 2012-07-19 08:06:39

相關問題