简体   繁体   English

JDBC —列函数的ResultSet值

[英]JDBC — ResultSet values for column functions

How to get the outcome of the query-- the AVG(DIST). 如何获取查询结果-AVG(DIST)。

MySQL is showing this result under the column "AVG(DIST)"-- no other clue. MySQL正在“ AVG(DIST)”列下显示此结果-没有其他线索。

How do i read this value from the ResultSet instance ( rs in below code)? 如何从ResultSet实例(以下代码中的rs )读取此值?

PreparedStatement ps = c.prepareStatement("SELECT AVG(DIST) FROM POOL_TABLE");
ResultSet rs = ps.executeQuery();

ResultSet seems to be referring to them all by column names. ResultSet似乎通过列名称引用了它们。

Not well-familiar to JDBC -- yet! 不太熟悉JDBC了!

TIA. TIA。

You can use an alias name in the query and retrieve the value in any of the two ways.! 您可以在查询中使用别名,并以两种方式中的任何一种检索值。

PreparedStatement ps = c.prepareStatement("SELECT AVG(DIST) AS AVERAGE_ALIAS FROM POOL_TABLE");
ResultSet rs = ps.executeQuery();

double avg = 0;
while (rs.next()) {
    avg = rs.getDouble(1);
    // OR
    avg= rs.getDouble("AVERAGE_ALIAS");
}

Here it is: 这里是:

double avg = 0;
while (rs.next()) {
    avg = rs.getDouble(1);
}

Use alias if you have many aggregate functions in your query. 如果查询中有很多聚合函数,请使用别名。 See below answer! 见下面的答案!

PreparedStatement used for inserting of data, i think you should use Statement. 用于插入数据的PreparedStatement,我认为您应该使用Statement。

Statement stmt = c.createStatement();
ResultSet rs = stmt.executeQuery("SELECT AVG(DIST) FROM POOL_TABLE");
if( rs.next() ){
  System.out.print( rs.getString(1) );
}

i hope this example would help you :) 我希望这个例子对您有帮助:)

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

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