I've written a bubble sort algorithm in Java and I'm trying to get it to run on Oracle 21c and I get ORA-06575: Package or function BUBBLESORT is in an invalid state
I'll state below the steps that I took in order to get to this error.
The BubbleSort.java file has this contents
public class BubbleSort {
public static void main() {
int[] nums = new int[10000];
for (int i = 0; i < nums.length; i ) {
nums[i] = (int) (Math.random() * 10000);
}
boolean swapped;
int tmp;
do {
swapped = false;
for (int i = 1; i < nums.length; i) {
if (nums[i - 1] > nums[i]) {
tmp = nums[i];
nums[i] = nums[i - 1];
nums[i - 1] = tmp;
swapped = true;
}
}
} while (swapped);
}
}
I've loaded it with loadjava and got the following response
loading : class BubbleSort
resolving: class BubbleSort
Classes Loaded: 1
Resources Loaded: 0
Sources Loaded: 0
Published Interfaces: 0
Classes generated: 0
Classes skipped: 0
Synonyms Created: 0
Errors: 0
After that I made a procedure using this statement
create or replace procedure BubbleSort
as language java name 'BubbleSort.main()';
/
When I try to call it using the call BubbleSort();
statement, it throws a ORA-06575: Package or function BUBBLESORT is in an invalid state
As a first debugging step I looked in user_errors
and got this
PLS-00103: Encountered the symbol "end-of-file" when expecting one of the following:
;
The symbol ";" was substituted for "end-of-file" to continue.
What should I do to solve this error?
CodePudding user response:
Change the method name to something other than main
:
CREATE AND COMPILE JAVA SOURCE NAMED BubbleSortJava AS
public class BubbleSort {
private static final int ARRAY_SIZE = 10000;
public static void sort() {
int[] nums = new int[ARRAY_SIZE];
for (int i = 0; i < ARRAY_SIZE; i ) {
nums[i] = (int) (Math.random() * 10000);
}
boolean swapped;
int tmp;
do {
swapped = false;
for (int i = 1; i < ARRAY_SIZE; i) {
if (nums[i - 1] > nums[i]) {
tmp = nums[i];
nums[i] = nums[i - 1];
nums[i - 1] = tmp;
swapped = true;
}
}
} while (swapped);
}
}
/
Then:
CREATE PROCEDURE BubbleSort
as language java name 'BubbleSort.sort()';
/
Then:
SELECT * FROM USER_ERRORS;
Shows no errors, and:
BEGIN
BubbleSort();
END;
/
Works
If you want to pass in an array to sort then:
CREATE TYPE number_list AS TABLE OF NUMBER;
and:
CREATE AND COMPILE JAVA SOURCE NAMED BubbleSortJava2 AS
import java.math.BigDecimal;
import java.sql.SQLException;
import java.sql.Array;
import oracle.jdbc.OracleConnection;
import oracle.jdbc.driver.OracleDriver;
public class BubbleSort2 {
public static Array sort(Array p_values) throws SQLException
{
BigDecimal[] values = (BigDecimal[]) p_values.getArray();
boolean swapped;
BigDecimal tmp;
do {
swapped = false;
for (int i = 1; i < values.length; i) {
if (
values[i - 1] != null
&& (values[i] == null || values[i - 1].compareTo(values[i]) == 1)
)
{
tmp = values[i];
values[i] = values[i - 1];
values[i - 1] = tmp;
swapped = true;
}
}
} while (swapped);
OracleConnection conn = (OracleConnection) new OracleDriver().defaultConnection();
return conn.createOracleArray("NUMBER_LIST", values);
}
}
/
CREATE FUNCTION BubbleSort2(p_values IN number_list) RETURN number_list
AS LANGUAGE JAVA NAME 'BubbleSort2.sort(java.sql.Array) return java.sql.Array';
/
Then:
SELECT *
FROM TABLE(BubbleSort2(number_list(3,5,4,2,1)));
Outputs:
COLUMN_VALUE |
---|
1 |
2 |
3 |
4 |
5 |