# JDBC Programmers:  How do I use SQL functions like count(\*), max(\*), etc.?

**URL:** <https://boards.straightdope.com/t/jdbc-programmers-how-do-i-use-sql-functions-like-count-max-etc/215499>\
**Category:** In My Humble Opinion\
**Created:** [November 27, 2003, 1:56am UTC](https://boards.straightdope.com/t/jdbc-programmers-how-do-i-use-sql-functions-like-count-max-etc/215499 "2003-11-27T01:56:08Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Spectre\_of\_Pithecanthropus](https://sea3.discourse-cdn.com/straightdope/user_avatar/boards.straightdope.com/spectre_of_pithecanthropus/32/12343_2.png) [@Spectre\_of\_Pithecanthropus](https://boards.straightdope.com/u/Spectre_of_Pithecanthropus)\
**Post date:** [November 27, 2003, 1:56am UTC](https://boards.straightdope.com/t/jdbc-programmers-how-do-i-use-sql-functions-like-count-max-etc/215499/1 "2003-11-27T01:56:08Z")

</div>

[Note: I am looking for definite factual info here, but the topic doesn’t seem to be of general enough interest to warrant placement in GQ].

When using SQL statements in a JDBC program, you create a statement object and then a ResultSet object that links your statement object with the query you want to execute. For example, the following code fragment will display the value of column FOO for every row in the BAR table:

\*\*

Connection c = DriverManager.getConnection…(creates a db session)

Statement s = c.createStatement();  
ResultSet r = s.executeQuery (“select FOO from BAR”);

while (r.next()){  
System.out.println (r.getString(“foo”));  
}\*\*

But how do I code a statement that uses a builtin function like COUNT(_) or MAX(_)? Suppose I wanted to execute the query

\*\*  
select COUNT(\*) from BAR where FOO=‘SOMEVALUE’"\*\*, and save the result in an int variable?

I’ve poked around a bit but haven’t been able to figure this out. Any help would be appreciated.

---

<div class="post-metadata">

**Author:** ![rickh57](https://avatars.discourse-cdn.com/v4/letter/r/54ee81/32.png) [@rickh57](https://boards.straightdope.com/u/rickh57)\
**Post date:** [November 27, 2003, 2:00am UTC](https://boards.straightdope.com/t/jdbc-programmers-how-do-i-use-sql-functions-like-count-max-etc/215499/2 "2003-11-27T02:00:28Z")

</div>

You’re existing code should work; you just need to convert the returned data into an int using something like Integer.parseInt(s) where s is the returned column.

---

<div class="post-metadata">

**Author:** ![Absolute](https://avatars.discourse-cdn.com/v4/letter/a/b2d939/32.png) [@Absolute](https://boards.straightdope.com/u/Absolute)\
**Post date:** [November 27, 2003, 2:22am UTC](https://boards.straightdope.com/t/jdbc-programmers-how-do-i-use-sql-functions-like-count-max-etc/215499/3 "2003-11-27T02:22:27Z")

</div>

\*\*  
Connection c = DriverManager.getConnection();

Statement s = c.createStatement();  
ResultSet r = s.executeQuery (“select count(\*) as cn from BAR”);

while (r.next()){  
System.out.println (r.getInt(“cn”));  
}  
\*\*

---

<div class="post-metadata">

**Author:** ![Absolute](https://avatars.discourse-cdn.com/v4/letter/a/b2d939/32.png) [@Absolute](https://boards.straightdope.com/u/Absolute)\
**Post date:** [November 27, 2003, 2:23am UTC](https://boards.straightdope.com/t/jdbc-programmers-how-do-i-use-sql-functions-like-count-max-etc/215499/4 "2003-11-27T02:23:41Z")

</div>

Alternatively:  
\*\*  
Connection c = DriverManager.getConnection();

Statement s = c.createStatement();  
ResultSet r = s.executeQuery (“select count(\*) from BAR”);

while (r.next()){  
System.out.println (r.getInt(1));  
}  
\*\*
