2011年1月21日金曜日

Apache Derbyでドライブの空き容量を返す関数を作成する

Apache Derbyでドライブの空き容量を返す関数を作成するには、以下の手順を実行します。

1.以下のクラスをantでコンパイルします。
FsGetFreeSpaceFunction.java
package com.serverarekore.derby;
import java.io.*;
import java.sql.*;
import java.net.*;

public class FsGetFreeSpaceFunction
{
public static Long fs_get_free_space(String path)
{
if( path == null )return null;
File file = new File(path);
return new Long(file.getFreeSpace());
}
}

build.xmlファイル例
<project name="DerbyFunctions" default="compile" basedir=".">
<path id="lib.classpath">
<fileset dir="lib">
<include name="*.jar"/>
</fileset>
</path>
<target name="compile">
<echo message="project: ${ant.project.name}"/>
<mkdir dir="build/" />
<javac srcdir="src/" destdir="build/"
deprecation="on" debug="on">
<classpath>
<path refid="lib.classpath"/>
</classpath>
</javac>
<jar destfile="c:\share\derby_funcs\FsGetFreeSpaceFunction.jar"
basedir="build"/>
</target>
<target name="clean">
<delete dir="build" />
</target>
</project>


2.derbyにjarをインストール
call sqlj.install_jar('c:\share\derby_funcs\FsGetFreeSpaceFunction.jar', 'APP.func1', 0);


3.クラスパスに通す
call syscs_util.syscs_set_database_property('derby.database.classpath', 
'APP.func1');


4.関数を作成
create function fs_get_free_space(path varchar(1024)) returns bigint
parameter style java no sql language java
external name 'com.serverarekore.derby.FsGetFreeSpaceFunction.fs_get_free_space';


5.実行
select fs_get_free_space('c:\') from sysibm.sysdummy1;


○動作環境
Apache Derby 10.7.1.1, JDK6 Update23
○関連情報
・Apache Derbyに関する他の記事はこちらを参照してください。

0 件のコメント:

コメントを投稿