If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > DB2 > Connectivity Error in external java function with JDBC connectivity

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 08-26-09, 09:56
ratheeshnellikkal ratheeshnellikkal is offline
Registered User
 
Join Date: Mar 2009
Posts: 5
Connectivity Error in external java function with JDBC connectivity

Hi All,

I need to create a external jave user defined function in db2 (db2 9.5 and AIX 5.1).
In my java class I need to establish a jdbc connectivity with the database .
following is my java code

import java.sql.*;
public class TestDataInsert {
public static String doInsertData(int id,String pcdata){
Connection conn = null;
String driver="com.ibm.db2.jcc.DB2Driver";
String url="jdbc:db2://server_ip:50000/MYDB";
String sql="insert into MYSCHEMA.MYTAB values(?,?)";
String msg="data inserted";
int status=0;
/*get the database connection */


try {
Class.forName(driver);
}catch (Exception e){
msg=e.getLocalizedMessage();
}
try{
conn =DriverManager.getConnection(url,"db2inst1","pass@ 123");
System.out.println("after connection ");
PreparedStatement st=conn.prepareStatement(sql);
System.out.println("after prepared statement ");
st.setInt(1,id);
st.setString(2,pcdata);
status=st.executeUpdate();
if(status==1){
conn.commit();
conn.close();
msg="data inserted";
}
} catch (Exception ex) {
msg=ex.getLocalizedMessage();
}
return msg;
}
}

This java class uses two jar files inorder to do the JDBC connectivity.
Those are

db2jcc.jar and db2jcc_license_cu.jar

This java code is working properly from my java tool (Eclipse) and inserting data into the table also.
i have created the external java user defined function as follows.


CREATE FUNCTION "MYSCHEMA"."JAVAFUN_INS" (
"ID" INTEGER,
"NAME" VARCHAR(50) )
RETURNS VARCHAR(20)
SPECIFIC "INS"
LANGUAGE JAVA
EXTERNAL NAME 'TestDataInsert.doInsertData'
PARAMETER STYLE JAVA
DETERMINISTIC
NOT FENCED
THREADSAFE
NO EXTERNAL ACTION
NO SQL
RETURNS NULL ON NULL INPUT
NO SCRATCHPAD
NO FINAL CALL
ALLOW PARALLEL
NO DBINFO
INHERIT SPECIAL REGISTERS;

I involked the UDF as follows:

select MYSCHEMA.JAVAFUN_INS(1,'test') from sysibm.sysdummy1

It throws me an error as follows.

No suitable driver

But i have done the following settings also like,

I put the .class file in /home/db2inst1/sqllib/function
I put both the jar files in /home/db2inst1/sqllib/function

and I have set the classpath as follows

/home/db2inst1/sqllib/java/db2java.zip:/home/db2inst1/sqllib/java/db2jcc.jar:
/home/db2inst1/sqllib/java/sqlj.zip:/home/db2inst1/sqllib/function/db2jcc.jar:
/home/db2inst1/sqllib/function/db2jcc_license_cu.jar:/home/db2inst1/sqllib/java/db2jcc_license_cu.jar

Still it is unable to find the driver.

Can any one help me out?
Thanks in advance.

Ratheesh Nellikkal



Last edited by ratheeshnellikkal; 08-29-09 at 00:46.
Reply With Quote
  #2 (permalink)  
Old 08-26-09, 10:25
n_i n_i is offline
:-)
 
Join Date: Jun 2003
Location: Toronto, Canada
Posts: 4,449
Check the manual for Java UDF examples - you are missing a few points.
Reply With Quote
  #3 (permalink)  
Old 08-26-09, 11:20
ratheeshnellikkal ratheeshnellikkal is offline
Registered User
 
Join Date: Mar 2009
Posts: 5
Thanks Nick,
Could u plz suggest me some usefull url for the same?
Reply With Quote
  #4 (permalink)  
Old 08-26-09, 11:28
n_i n_i is offline
:-)
 
Join Date: Jun 2003
Location: Toronto, Canada
Posts: 4,449
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On