java - 如何从 dao 函数返回整数?

标签 java jakarta-ee oracle10g dao

在 servlet 程序中,我创建了一个包含函数的 DAO 类,我希望该函数返回通过执行 Oracle 查询生成的特定值。我尝试了如下操作:

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 作为(我猜)成功。但我希望它返回执行此查询的结果。

最佳答案

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);
}

关于java - 如何从 dao 函数返回整数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11555827/

相关文章:

java - "loader constraints violated when linking javax/xml/namespace/QName class"来自 Oracle 10g 上的 webapp

javascript - 在一堆数字中找到一个未使用的数字

java - If 语句比较字符串

java - 如何获取数组对象及其索引

java - 当我创建一个新项目时 Eclipse 给我一个错误

jakarta-ee - Keycloak Backchannel注销

JavaMail API 和 Tomcat 7 实现

java - 如何在CDI事件中使用参数?

sql - 重叠数据

java - 如果-否则-如果 : It always give me the same result