• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Calling a DB2 UDF within Java

 
Brad Messerle
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I can execute the following sql inside the db2 command center and works fine.

values GetInvRespCntClmId(1);

Java Code

public String xyz(Connection con,int anInteger){

int responces = 0;
PreparedStatement pstmt = null;
try {

pstmt = con.prepareStatement("values GetInvRespCntClmId(?)");
pstmt.setInt(1, anInteger);

ResultSet rs = pstmt.executeQuery();
while (rs.next()) {
responces = rs.getInt(1);
}

rs.close();
pstmt.close();

} catch (SQLException e) {
e.printStackTrace();
} finally {
try {
pstmt.close();
} catch (SQLException e1) {
e1.printStackTrace();
}
}

return new Integer(responces).toString();

}

I get a DB2 --- DB2 SQL error: SQLCODE: -418, SQLSTATE: 42610, SQLERRMC: null

Any suggestions?

Thank You!
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 35279
384
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Brad,
Values() is a function in db2, not a SQL statement. So it wouldn't work from JDBC. You could create a select that goes against a system table and just return one row with that function call.

This is the technique Oracle uses for getting the system date.
 
Brad Messerle
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What I did was create a view that I could create a query against.

Is this what you suggested? Can elebrate on creating a select statement against a system table?

Thanks!
 
Brad Messerle
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here is the sql statement i came up with but i cant get it to work from jdbc
select SumRecommendedLow(?) from xyz fetch first 1 row only

Can you please provide a sample sql statement calling a udf?


Thanks!
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 35279
384
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

This is the idea behind what I was thinking since xyz could be any table.

What happens when you try to call it from JDBC? Does it work if you hard code the parameter instead of using the "?" ?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic