I am trying to call the procedure by specifying two input parameters, but it doesn’t work.
String query = "CALL balance_change(?, ?)";
PreparedStatement ps = connection.prepareCall(query);
ps.setString(1, "Bob");
ps.setDouble(2, 333);
ps.execute();
System.out.println();
Statement statement = connection.createStatement();
ResultSet rs = statement.executeQuery("SELECT * FROM accounts WHERE name="+"Bob");
while (rs.next()) {
System.out.println(rs.getInt(1)+" "+rs.getString(2)+" "+rs.getDouble(3));
}
Error:
Exception in thread "main" org.postgresql.util.PSQLException: ERROR: procedure balance_change(character varying, double precision) does not exist
Hint: A procedure with the given name and argument types was not found. Perhaps you should add explicit type conversions.
You defined the procedure to take numeric
as 2nd argument. But you use setDouble
for that 2nd argument, which raises the quoted exception.
Postgres function type resolution (including procedures) won’t accept double precision
input for a numeric
(without explicit cast), because there is only an “assignment” cast registered for double precision
→ numeric
. These are acceptable inputs:
CALL balance_change('bla', numeric '123'); -- type `numeric`
CALL balance_change('bla', integer '123'); -- integer types (implicit cast)
CALL balance_change('bla', '123'); -- string literal
CALL balance_change('bla', 123); -- numeric literal
There are more syntax errors in your code, but this is the immediate cause of the error message.
How well does
ps.setDouble
match thenumeric
argument?I don’t know but
"SELECT * FROM accounts WHERE name="+"Bob"
definitely isn’t going to work.