How to Develop Java Stored Procedures
I’ll try to demonstrate how we can use loadjava to load our Java class into Oracle Database and use its methods as stored procedures. Before we begin to write the Java codes, I’ll create a simple table. When I call my java stored procedure, it will insert a record to this table.
1 2 3 4 5 |
CREATE TABLE hr.sampletable ( id NUMBER, name VARCHAR2(50), email VARCHAR2(50) ); |
I created this table in HR schema because I’ll load my java object in HR schema. If you’ll use another schema, then do not forget to load your java code in same schema or set required permissions. This is the main method I’ll use as my stored procedure:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
public static void insert_into_table(int ID, String Name, String Email) { Connection DB = DriverManager.getConnection("jdbc:default:connection:"); String SQL ="INSERT INTO sampletable VALUES (?,?,?)"; PreparedStatement cmd = DB.prepareStatement(SQL); cmd.setInt(1, ID); cmd.setString(2, Name ); cmd.setString(3, Email ); cmd.executeUpdate(); cmd.close(); } |