使用pl/sql獲得網頁內容~~~

junsansi發表於2007-04-12

首先建立java sources並編譯~~

create or replace and compile java source named stringutil as
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URLConnection;
import java.net.URL;

public class StringUtil
{

public static String getUrlString(String myUrl) {
String result = null;
InputStreamReader ISR = null;
BufferedReader in= null;

if (myUrl != null){
try{
URL url= new URL(myUrl); //得到檔案的URL地址
ISR = new InputStreamReader(url.openStream(),"GB2312");
in = new BufferedReader(ISR);
String line;
StringBuffer content = new StringBuffer(); //檔案內容
while ((line = in.readLine()) != null) { //讀取檔案
content.append(line+"n");
}
result = content.toString();
}
catch(Exception ex){
return "@err:"+ex.getMessage();
}
finally{
try{
if (in != null)in.reset();
}catch(Exception e){

}
try{
if (ISR != null)ISR.reset();
}catch(Exception e){

}
}
}
return result;
}
}

定義一個function

create or replace function Java_getUrlString(str In Varchar) return varchar2 as
LANGUAGE JAVA
NAME 'StringUtil.getUrlString(java.lang.String) return java.lang.String';

賦予當前使用者執行許可權

call dbms_java.grant_permission(username, 'SYS:java.net.SocketPermission','*', 'connect,resolve' );

最後測試看看是否有返回:

Select Java_getUrlString('http://www.5ienet.com/online.asp') From dual;


 

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/7607759/viewspace-22213/,如需轉載,請註明出處,否則將追究法律責任。

相關文章