I have this SQL function:
CREATE FUNCTION Average (@year int)
RETURNS decimal(4,2)
AS
BEGIN
DECLARE @result as decimal(4,2);
SET @result = (SELECT AVG(CAST(MilkQuantity AS decimal(4,2))) FROM MilkProduction WHERE YEAR(Date) = @year);
RETURN @result;
END
GO
How do I execute this function in java and what data type do I even bind it to?
This is what I came up with and it works but it seems like I'm coloring outside the lines here:
PreparedStatement ps = con.prepareStatement("SELECT dbo.Average(" + year + ")");
ResultSet rs = ps.executeQuery();
rs.next();
System.out.println("Average: " + rs.getFloat(""));