• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

Calling a DB2 UDF within Java

 
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!
 
author & internet detective
Posts: 40198
816
Eclipse IDE VI Editor Java
  • 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
Posts: 40198
816
Eclipse IDE VI Editor Java
  • 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 "?" ?
 
Don't get me started about those stupid light bulbs.
    Bookmark Topic Watch Topic
  • New Topic