Create UDRs with UDRManager

The following code shows how an application can use the UDRManager and UDRMetaData classes to convert methods in a Java™ class on the client (inaccessible to the database server) to Java UDRs in the database server. Applications can later reference the UDRs in SQL statements. In this example, the Java class on the client is named Group1. The class has two routines, udr1 and udr2.

The following code creates methods in the Group1 class to be registered as UDRs in the database server:
import java.sql.*;

public class Group1
{
   public static String udr1 (String s1, String s2)
        throws SQLException
   {
   return s1 + s2;
   }
    // Return a formatted string with all inputs
   public static String udr2 (Integer i, String s1,
                  String s2) throws SQLException
   {
   return "{" + i + "," + s1 + "," + s2 +"}";
   }
}
The following code creates Java methods udr1 and udr2 as UDRs group1_udr1 and group1_udr2 in the database server and then uses the UDRs:
import java.sql.*;
import java.lang.reflect.*;

public class PlayWithGroup1
{
// Open a connection...
url = "jdbc:onedb://hostname:portnum:db;user=scott;password=tiger;
myConn = DriverManager.getConnection(url);

//Install the routines in the database.
UDRManager udtmgr = new UDRManager(myConn);
UDRMetaData mdata = new UDRMetaData();
Class gp1 = Class.forName("Group1");
Method method1 = gp1.getMethod("udr1", 
   new Class[]{String.class, String.class});
Method method2 = gp1.getMethod("udr2", 
   new Class[]{Integer.class, String.class, String.class});
mdata.setUDR(method1, "group1_udr1");
mdata.setUDR(method2, "group1_udr2");
mdata.setJarFileSQLName("group1_jar");
udtmgr.createUDRs(mdata, "Group1.jar", "Group1", 0);

// Use the UDRs in SQL statements:
Statement stmt = myConn.createStatement();
stmt.executeUpdate("create table tab (c1 varchar(10), 
   c2 char(20)", c3 int);
stmt.close();
Statement stmt = myConn.createStatement();
stmt.executeUpdate("insert into tab values ('hello', 'world',
   222)");
stmt.close();

Statement stmt = myConn.createStatement();
ResultSet r = stmt.executeQuery("select c3, group1_udr2(c3, c1, c2) 
   from tab where group1_udr1(c1, c2) = 'hello world'");

...

}