How do you access the value of an SQL count () query in a Java program


Java Problem Overview

I want to get to the value I am finding using the COUNT command of SQL. Normally I enter the column name I want to access into the getInt() getString() method, what do I do in this case when there is no specific column name.

I have used 'AS' in the same manner as is used to alias a table, I am not sure if this is going to work, I would think not.

Statement stmt3 = con.createStatement();
ResultSet rs3 = stmt3.executeQuery("SELECT COUNT(*) FROM "+lastTempTable+") AS count");
    count = rs3.getInt("count");

Java Solutions

Solution 1 - Java

Use aliases:


and then


Solution 2 - Java

The answers provided by Bohzo and Brabster will obviously work, but you could also just use:


to get the value in the first, and in your case, only column.

Solution 3 - Java

I would expect this query to work with your program:

"SELECT COUNT(*) AS count FROM "+lastTempTable+")"

(You need to alias the column, not the table)

Solution 4 - Java

I have done it this way (example):

String query="SELECT count( from t1, t2 where and'"[email protected]"'";
int count=0;
try {
	ResultSet rs = DatabaseService.statementDataBase().executeQuery(query);
} catch (SQLException e) {
} finally {

Solution 5 - Java

			Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/bala","bala","bala");
			if(con == null) System.out.print("not connected");
			Statement st = con.createStatement();
			String myStatement = "select count(*) as total from locations";
			ResultSet rs = st.executeQuery(myStatement);
			int num = 0;
				num = (rs.getInt(1));
		catch(Exception e){

Solution 6 - Java

Statement stmt3 = con.createStatement();

ResultSet rs3 = stmt3.executeQuery("SELECT COUNT(*) AS count FROM "+lastTempTable+" ;");

count = rs3.getInt("count");

Solution 7 - Java

It's similar to above but you can try like

public Integer count(String tableName) throws CrateException {
        String query = String.format("Select count(*) as size from %s", tableName);
        try (Statement s = connection.createStatement()) {
            try (ResultSet resultSet = queryExecutor.executeQuery(s, query)) {
                Preconditions.checkArgument(, "Result set is empty");
                return resultSet.getInt("size");
        } catch (SQLException e) {
            throw new CrateException(e);


All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionAnkurView Question on Stackoverflow
Solution 1 - JavaBozhoView Answer on Stackoverflow
Solution 2 - JavaEric EijkelenboomView Answer on Stackoverflow
Solution 3 - JavabrabsterView Answer on Stackoverflow
Solution 4 - JavaandrewView Answer on Stackoverflow
Solution 5 - JavaBalajiShanmugamView Answer on Stackoverflow
Solution 6 - JavageekView Answer on Stackoverflow
Solution 7 - JavaDhruv PalView Answer on Stackoverflow