So far my code is:
try{
stmt = con.createStatement();
String sql = "SELECT AVG(WPM) FROM Attempts WHERE Username = ('" + Login.loginUsername + "');
rs = stmt.executeQuery(sql);
int add1 = rs.getInt("AVG(WPM)");
System.out.println("avg temp is " + add1);
}
catch (SQLException err) {
System.out.println(err.getMessage());
}
my database is set up like so:
-
(PK) ‘AttemptID’ Integer AutoIncrement
-
‘WPM’ Integer
-
(FK) ‘Username’ Varchar(45)
I want it to return average WPM based on each username. If I change the line
int add1 = rs.getInt("AVG(WPM)");
to
String add1 = rs.getString("AVG(WPM)");
there is no difference. please help.
Try with
double add1 = rs.getDouble(1);Explanaition:
rs.get...(1)gets the column based on the column index, not based on the name of the column. The database might change this when using functions in your select. Also, use double instead of int as AVG returns a double.