10/29/2009

Java Reflection 動態調用物件的方法

近來接觸Java Reflection,參考良葛格的例子做了些練習,下面是實作的code。

Student Class:
package com.iden.reflection;

public class Student {
 private String name;
    private int score; 

    public Student() { 
        name = "N/A"; 
    } 

    public Student(String name, int score) { 
        this.name = name; 
        this.score = score; 
    } 

    public void setName(String name) {
        this.name = name;
    }
    
    public void setScore(int score) {
        this.score = score;
    }

    public String getName() { 
        return name; 
    } 

    public int getScore() { 
        return score; 
    } 

    public void showData() { 
        System.out.println("name: " + name); 
        System.out.println("score: " + score); 
    }
}


InvokeMethodDemo Class 調用Student object實作方法:
package com.iden.reflection;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

public class InvokeMethodDemo {
 
 private static String className;
 
    public static void main(String[] args) {
     //動態載入與呼叫方法
        try {
          
         if(args.length == 0)
          className = "com.iden.reflection.Student";
         else
          className = args[0];
          
            Class c = Class.forName(className);//呼叫Class
            Object targetObj = c.newInstance();//動態實作物件
            
            Class[] param1 = {String.class};
            Method setName = c.getMethod("setName", param1);//指定物件的方法            
            Object[] paramObjs1 = {"caterpillar"};
            setName.invoke(targetObj, paramObjs1);//將參數帶入方法
            
            
            Class[] param2 = {Integer.TYPE};
            Method setScore = c.getMethod("setScore", param2);            
            Object[] paramObjs2 = {new Integer(90)};
            setScore.invoke(targetObj, paramObjs2);
            
            
            Method showData = c.getMethod("showData", new Class[0]);
            showData.invoke(targetObj, new Object[0]);// call showData method to show name and score.
            
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SecurityException e) {
            e.printStackTrace();
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        } catch (IllegalArgumentException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        }
    }
} 


感謝良葛格做了這麼棒的教學。
良葛格範例

10/28/2009

Oracle 系統 object 相關

--查出所有的objects:
select * from all_objects

--找Column名稱是ID的所有table
select * from all_tab_columns
where column_name = 'ID';

updated: Nov 12, 2009
--Oracle show Year, Month and Date
select TO_CHAR(sysdate,'Y') as YY ,TO_CHAR(sysdate,'MM') as MM,TO_CHAR(sysdate,'DD') as DD from dual

updated:Dec 17, 2009
--查詢自建Table: PT_LTDFT 有沒有欄位 DF_TYPE
SELECT COLUMN_NAME from USER_TAB_COLUMNS p WHERE p.table_name = 'PT_LTDFT' and p.column_name = 'DF_TYPE'

10/26/2009

VirtualBox中 GuestOS:Ubuntu 自動掛載 shared folder

Ubuntu開機後要自動掛載磁碟需設定fstab,做法約如下:

1.進入command console,etc裡用sudo vi打開fstab檔(權限不足會不能修改fstab)
cd /
cd etc
sudo vi fstab

2.在最下面加入以下資訊
Download /media/Download vboxsf defaults 0 0

其中Download是VirtualBox中設定的shared folder名稱
/media/Download是要掛載的folder

3.reboot,完成。

VirtualBox裡掛載Shared Folder在GuestOS Ubuntu 9.10

Host OS: windows
Guest OS: Ubuntu 9.10

前提:
在VirtualBox設定裡先設定好HostOS的shared folder

1. 安裝VirtualBox Additions:
cd /media/cdrom
sudo ./VBoxLinuxAdditions-x86.run

2. vboxvfs module載入核心:
sudo modprobe vboxvfs

3.建立要掛載的folder
mkdir /media/Download

4.掛載Shared Folder
sudo mount -t vboxsf [SharedFolderName] /media/Download

5.完成