I am trying to call a java function from plsql to return a csv item at a certain index in a csv string. The csv string can also contain separators within quotes (based on this Stack Overflow question).
The code
set serverout on size 100000
CREATE OR REPLACE AND COMPILE JAVA SOURCE NAMED "XxpayCsv" AS
import java.io.ByteArrayInputStream;
public class XxpayCsv
{
public static String csv(String line, Integer idx)
{
String otherThanQuote = " [^\"] ";
String quotedString = String.format(" \" %s* \" ", otherThanQuote);
String regex = String.format("(?x) "+ // enable comments, ignore white spaces
", "+ // match a comma
"(?= "+ // start positive look ahead
" (?: "+ // start non-capturing group 1
" %s* "+ // match 'otherThanQuote' zero or more times
" %s "+ // match 'quotedString'
" )* "+ // end group 1 and repeat it zero or more times
" %s* "+ // match 'otherThanQuote'
" $ "+ // match the end of the string
") ", // stop positive look ahead
otherThanQuote, quotedString, otherThanQuote);
String[] tokens = line.split(regex, -1);
//for(String t : tokens) {
// System.out.println("> "+t);
//}
return tokens[idx];
}
};
/
CREATE OR REPLACE
FUNCTION xxpay_csv_at(s varchar2, i number) RETURN VARCHAR2 AS
LANGUAGE JAVA NAME 'XxpayCsv.csv (s, i) return java.lang.String';
/
DECLARE
my_string clob; -- VARCHAR2(400 CHAR);
BEGIN
my_string := xxpay_csv_at('a,"b,c",d', 1);
dbms_output.put_line('The value of the string is: ' || my_string);
END;
/
gives me the error
DECLARE
*
ERROR at line 1:
ORA-29531: no method csv in class XxpayCsv
ORA-06512: at "APPS.XXPAY_CSV_AT", line 1
ORA-06512: at line 4
because I am not sure how to pass in the string and integer as parameters (I'm not a Java programmer). What am I doing wrong?