以下示例用java实现ping,然后用PLSQL存储过程调用该方法,可用来测试dblink服务器网络状态
1.java调用系统功能实现ping,返回ping后获取的字符(以下代码在Linux上通过,windows的话需要修改参数)
create or replace and compile java source named "CheckNet" asimport java.net.*;import java.io.*;import java.util.*;public class CheckNet{ public static String ping(String host) { String pingResult = ""; String pingCmd = "ping -c 2 " + host; BufferedReader in; try { Runtime r = Runtime.getRuntime(); Process p = r.exec(pingCmd); in = new BufferedReader(new InputStreamReader(p.getInputStream())); String inputLine; while ((inputLine = in.readLine()) != null) { pingResult += inputLine; } in.close(); return pingResult; } catch (IOException e) { return e.getMessage(); } }}
2.用PLSQL封装java方法
CREATE OR REPLACE FUNCTION CheckNet_ping(p_host in varchar2) RETURN varchar2 AS LANGUAGE JAVA NAME 'CheckNet.ping(java.lang.String) return java.lang.String';
3.测试
select CheckNet_ping('127.0.0.1') from dual