omareliotorres
omareliotorres

Reputation: 3

how to count data from mysql and then show it in javafx

My problem is that I want to apply the following two commands in JavaFX in order to get how many 1ones are in a named "party" column from a database named "conteo".

   sql ="select count(*) from conteo where party =1";

   ResultSet rs = conn.createStatement().executeQuery(sql); 

i used System.out.println(rs.getString("party")); to expect the correct number of 1ones in "party" column from "conteo" database and i get "the error column party do not exist", but actually it already exist!!!

   sql ="select count(*) from conteo where party =1";

   ResultSet rs = conn.createStatement().executeQuery(sql); 

I would like to print in command line "rs" using "System.out.println()" My database address is correctly address it, because i already write data sucessfully in mysql databases using javafx, how can i properly print "rs" result in command line?, anyhelp is appreciated, thanks in advance, Omar Torres

Upvotes: 0

Views: 719

Answers (2)

fabian
fabian

Reputation: 82461

You did not select the party column for the output. Since you're using a aggregate function, this shouldn't work in this case anyways. You'd need to use GROUPING BY+HAVING anyways.

It's much simpler to provide an alias for the value

sql ="SELECT COUNT(*) AS num FROM conteo WHERE party =1";
ResultSet rs = conn.createStatement().executeQuery(sql); 
if (rs.next()) {
    int i = rs.getInt("num");
    ...
}

or

sql ="SELECT COUNT(*) FROM conteo WHERE party =1";
ResultSet rs = conn.createStatement().executeQuery(sql); 
if (rs.next()) {
    int i = rs.getInt(1); // access first column in result
    ...
}

Upvotes: 1

Abra
Abra

Reputation: 20914

You asked...

how can i properly print "rs" result

Do you mean something like this...

if (rs.next()) {
    int theCount = rs.getInt(1);
    System.out.println(theCount);
}

Upvotes: 1

Related Questions