简体   繁体   中英

Execute jar file using PLSQL

I have aa standalone java program and it read the data from REST end point and insert data into table in Server.

   package com.test.main;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Scanner;

import org.apache.tomcat.util.http.fileupload.IOUtils;
import org.json.JSONObject;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.gson.Gson;
import com.test.connectdb.ConDataBase;
import com.test.entity.User;

public class Main {

    public static void main(String[] args)  {

        try {


            URL url = new URL("https://jsonplaceholder.typicode.com/todos/");
            HttpURLConnection conn = (HttpURLConnection)url.openConnection();
            conn.setRequestMethod("GET");

            conn.connect();
            System.out.println("DONE2");

            int responsecode = conn.getResponseCode();

            String inline = "";
            if(responsecode == 200){
                Scanner sc = new Scanner(url.openStream());
                while(sc.hasNext())
                {
                inline+=sc.nextLine();
                }
                System.out.println("JSON data in string format");
                System.out.println(inline);
                sc.close();

            }else{
                throw new RuntimeException("HttpResponseCode:" +responsecode);

            }

              //-----------------------------------------------------------------------
                Connection con = new ConDataBase().buildConnection();

                User[] userList = new Gson().fromJson(inline, User[].class);
                System.out.println(userList.length);
                for (User user : userList) {
                   //System.out.println(user.getCompleted());
                     String insert_date = "insert into XX_USER "
                                + "(USER_ID)"
                                + "VALUES"
                                +"('"+user.getCompleted()+"')";


                    try {

                        PreparedStatement ps_data = con.prepareStatement(insert_date);
                        ps_data.executeUpdate();
                        con.commit();
                        System.out.println("Successfully Inserted");
                    } catch (SQLException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                        System.out.println(e.getMessage());
                    }
                }


        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }



        }

}

I need to run this jar file using PLSQL. That means I have transferred this jar file into Linux server path (/home/rest). Oracle database is installed in server. I need to run this jar using PLSQL. Is it possible?

Use the LOADJAVA utility to load the jar file and all other jar dependencies into Oracle's internal classpath (this is different from the operating system's class path).

You will probably also want to change your code to a static method without arguments (rather than main with a string array argument) as it will make invoking the method much simpler.

// package and imports
public class Main {
  public static void yourMethodName()  {
    // your code
  }
}

Then you need to use something like:

CREATE PROCEDURE get_todos_from_rest_service AS
  LANGUAGE JAVA NAME 'com.test.main.Main.yourMethodName()';

To create a procedure wrapper around the java method which you can then invoke in PL/SQL.

A more detailed example can be found here: Database Java Developer's Guide - Java Stored Procedures Application Example

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM