简体   繁体   中英

Can not print in java console the min, avg, max value of a MySql table

How can I use resultset to get me the minimum, average or maximum value from a mysql database column?

I have a prepared statement sql constant string = Select avg(EntryValues) from Entries;

I know I need to use a resultset.getString(EntryValues) but I dont know how to build the java method that would return the actual average value from that resultset.next() loop thing...

Could you please help me?

  1. Get a statement from your connection
  2. use the statement.executeQuery() method to invoke your query and assign it to your ResultSet , eg

     ResultSet rs = statement.executeQuery("SELECT AVG(EntryValues) FROM Entries"); 
  3. Your result is one simple ' row ' therefore you can use

     if(rs.next()) { // check if a result was returned String avg = rs.getString(1); // get your result } 

If your result contains multiple rows you'll have to use a while-loop for example to iterate through all the result entries:

while(rs.next()) {
  // do your thing
}

Hope this helpes, have Fun!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM