我想将count查询的值赋给整数变量

时间:2014-03-11 04:40:27

标签: java mysql

`String query = "select count(book_id) as book_id from library_books_details where book_id = ?";`
PreparedStatement psEnd = collegeCon.prepareStatement(query);

psEnd.setInt(1, bookId);

2 个答案:

答案 0 :(得分:1)

ResultSet rs = psEnd.executeQuery();
      if (rs.next()) {
        int count= rs.getInt(1);
        System.out.println("Count= " + count);
      } else {
        System.out.println("error: could not get the record counts");
      } 

答案 1 :(得分:1)

根据http://docs.oracle.com/javase/tutorial/jdbc/basics/processingsqlstatements.html

数据库访问流程

    ResultSet rs = stmt.executeQuery(query);
    while (rs.next()) {
        String strColumn = rs.getString("....");
        int intColumn= rs.getInt("....");
    }