简体   繁体   English

无法在Java控制台中打印MySql表的最小值,平均值,最大值

[英]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? 如何使用结果集从mysql数据库列获取最小值,平均值或最大值?

I have a prepared statement sql constant string = Select avg(EntryValues) from Entries; 我有一个准备好的语句sql常量字符串=从条目中选择avg(EntryValues);

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... 我知道我需要使用resultset.getString(EntryValues)但我不知道如何构建从该resultet.next()循环中返回实际平均值的java方法...

Could you please help me? 请你帮助我好吗?

  1. Get a statement from your connection 从您的connection获取statement
  2. use the statement.executeQuery() method to invoke your query and assign it to your ResultSet , eg 使用statement.executeQuery()方法调用查询并将其分配给ResultSet ,例如

     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循环来遍历所有结果条目:

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

Hope this helpes, have Fun! 希望这会有所帮助,玩得开心!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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