11/10/2009

Java load properties file

如何用Java去讀取外部resource檔,要怎麼做呢?
EIF.properties是個文件檔,放在class可以讀到的路徑下,在這裡是放在/bin。

PropertyLoader class
package com.iden.resource;

import java.util.Properties;

public class PropertyLoader {

 private Properties prop = null;
 private Boolean isSet;

 PropertyLoader() {
  prop = new Properties();
  try {
   prop.load(getClass().getResourceAsStream("/EIF.properties"));
  } catch (Exception e) {
   e.printStackTrace();
  }
  if (new String(
    prop.getProperty("PropertyLoaderTest.isSet") != null ? prop
      .getProperty("PropertyLoaderTest.isSet") : "").trim()
    .equalsIgnoreCase("1"))
   isSet = true;
  else
   isSet = false;
 }
 
 public static void main(String[] args){
  PropertyLoader loader = new PropertyLoader();
  System.out.println("PropertyLoaderTest.isSet=" + loader.isSet);
  
 }
}

EIF.properties
# 1 or 0  / 1:short data model: update data when start time, end time, shift time  0:every data update model
PropertyLoaderTest.isSet=1

11/04/2009

VB Window Form 限制輸入字數 code

VB.net限制只能輸入多少字並大寫,以下是練習。
'TextBox limit character digits user input
    Public Sub limitTextBoxDigits(ByRef obj As System.Windows.Forms.TextBox, ByVal digits As Integer)
        obj.CharacterCasing = Windows.Forms.CharacterCasing.Upper
        If obj.SelectedText <> "" Then 
            Dim selIdx As Integer = obj.SelectionStart
            obj.Text = obj.Text.Remove(obj.SelectionStart, obj.SelectionLength)
            obj.Select(selIdx, 1)
        End If
        If obj.Text.Length >= digits Then obj.Text = obj.Text.Remove(digits - 1, 1) : obj.Select(digits - 1, 1)
    End Sub

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.完成

9/29/2009

Java code加入proxy

加上下面的code就可以了。這是設在系統的方式。
    System.setProperty("http.proxySet", "true");
    System.setProperty("http.proxyHost", "10.180.0.53");
    System.setProperty("http.proxyPort", "8080");