• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Calling a DB2 UDF within Java

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!
 
author & internet detective
Posts: 41860
908
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 41860
908
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 "?" ?
 
sunglasses are a type of coolness prosthetic. Check out the sunglasses on this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic