網頁

2013年6月6日 星期四

[java] String.split()用法

在java,可以使用String.split(delimiter),將字串分割成數個token,得到一個回傳的String array。
例如:
String str = "aaa:bbb:ccc:ddd"; String[] tokens = str.split(":"); for (String token:tokens) { System.out.println(token); }
結果顯示:
aaa bbb ccc ddd
如果字串中有多個分隔符號時,就須加上"|"。
String str = "aaa:bbb-ccc_ddd"; String[] tokens = str.split(":|-|_"); for (String token:tokens) { System.out.println(token); }
結果顯示:
aaa bbb ccc ddd
當delimiter用到特殊字元時,如".", "|", "$",此時要在特殊字元前面加上"\\",才會得到正確的結果。
String str = "aaa-bbb-ccc.ddd"; String[] tokens = str.split("-|\\."); for (String token:tokens) { System.out.println(token); }
結果顯示:
aaa bbb ccc ddd
閱讀全文...

2013年5月20日 星期一

db2 encrypt/decrypt value for column(表格欄位值加解密)

全球最嚴個資法,台灣說第二,不知道有沒有其他國敢跳出來說第一,所以一堆公司開始了所謂的控管,免不的,資料庫有些敏感性資料也要加密,只好找找資料,試一下db2怎麼對表格中的欄位來進行加解密。

db2 針對不同型態,提供了不同的加解密函式,根據版本不同,有些函式還未提供,或是只供內部使用,所以只把我試出來可以用的列出來。
資料型態,主要針對varchar
使用下列函式:
  • encrypt(StringDataToEncrypt, PasswordOrPhrase, PasswordHint)
  • decrypt_dhar(EncryptedData, PasswordOrPhrase)
  • Set Encryption Password

對資料加密的演算法是一個 RC2 分組密碼(block cipher),它帶有一個 128 位的密鑰。這個128位的密鑰是通過消息摘要從密碼得來的。加密密碼與DB2認證無關,僅用於資料的加解密。
另外提供一個可選的參數 PasswordHint,這是一個字串,可以幫助用戶記憶用於對 PasswordOrPhrase 提示。

db2 db2 => connect to your_database db2 => create table xxx(cardno varchar(33) for bit data) db2 => set encryption password = 'test1234'; db2 => insert into xxx values(encrypt('1234567890123456')) db2 => select decrypt_char(cardno) from xxx db2 => quit
NOTE:
密碼至少要 6byte
新增表格時欄位長度需設定為原本欄位的最大長度+9bytes,否則在新增欄位值,會出現欄位值太長的錯誤。例如cardno原本最大長度為24,在新增table時,將其設定成33 byte

閱讀全文...

2013年5月13日 星期一

[Java]String.format 好好用

java 1.5 String.format 以前就覺得很好用,本想查查有多少 Conversion 可以用,沒想到一查 Formatter 中的各種 Conversion ,稍微試了一下,以前像 Date 處理、add leading zero、金額加上","等情況,都變得很容易處理了。
import java.util.Date; public class StringFormatT { public static void main(String[] args) { System.out.println("===== test argument index ======================================================"); System.out.println("original\t10\t20"); System.out.println("format\t\t%1$s\t%1$d\t%2$s\t%2$d"); System.out.println(String.format("result\t\t\"%1$s\"\t%1$d\t\"%2$s\"\t%2$d", 10, 20)); System.out.println("original\t10\t20\t30\t40"); System.out.println("format\t\t%1$s\t%3$d\t%4$s\t%2$d"); System.out.println(String.format("result\t\t\"%1$s\"\t%3$d\t\"%4$s\"\t%2$d", 10, 20, 30, 40)); System.out.println("================================================================================"); System.out.println("===== formatted as string ======================================================"); System.out.println("original\t\"abc\"\tnull\t(byte)1\t3.14"); System.out.println("format\t\t%s\t%S\t%s\t%S"); System.out.println(String.format("result\t\t%s\t%S\t%s\t%S", "abc", null, (byte)1, 3.14)); System.out.println("================================================================================"); System.out.println("===== formatted as boolean ====================================================="); System.out.println("original\t\"abc\"\tnull\t(byte)1\t3.14"); System.out.println("format\t\t%b\t%B\t%b\t%B"); System.out.println(String.format("result\t\t%b\t%B\t%b\t%B", "abc", null, (byte)1, 3.14)); System.out.println("================================================================================"); System.out.println("===== formatted as hex ========================================================="); System.out.println("original\t\"100\"\tnull\t100\tnew Integer(100)\t3.14"); System.out.println("format\t\t%h\t%H\t%h\t%H\t\t\t%h"); System.out.println(String.format("result\t\t%h\t%H\t%h\t%H\t\t\t%h", "100", null, 100, new Integer(100), 3.14)); System.out.println("================================================================================"); System.out.println("===== formatted as character ==================================================="); System.out.println("original\t'A'\t97\t97"); System.out.println("format\t\t%s\t%C\t%c"); System.out.println(String.format("result\t\t%s\t%C\t%c", 'A', 97, 97)); System.out.println("================================================================================"); System.out.println("===== formatted as integer ====================================================="); System.out.println("original\t100\t100\t100"); System.out.println("format\t\t%d\t%o\t%h"); System.out.println(String.format("result\t\t%d\t%o\t%h", 100, 100, 100)); System.out.println("================================================================================"); System.out.println("===== formatted as big number =================================================="); System.out.println("original\t123456789L"); System.out.println("format\t\t%1$d\t\t%1$,d"); System.out.println(String.format("result\t\t%1$d\t%1$,d", 123456789L)); System.out.println("================================================================================"); System.out.println("===== formatted as float ======================================================="); System.out.println("original\t123456789.98765"); System.out.println("format\t\t%1$.2a\t\t%1$.2e\t\t%1$.2f\t\t%1$.2g"); System.out.println(String.format("result\t\t%1$.2a\t%1$.2e\t%1$.2f\t%1$.2g", 123456789.98765)); System.out.println("================================================================================"); System.out.println("===== formatted as date ========================================================"); long currentTime = System.currentTimeMillis(); Date date = new Date(); System.out.println("original\tCurrent datetime use System.currentTimeMillis()"); System.out.println("format\t\t%1$tY-%1$tm-%1$td %1$tH:%1$tM:%1$tS"); System.out.println(String.format("result\t\t%1$tY-%1$tm-%1$td %1$tH:%1$tM:%1$tS", currentTime)); System.out.println("format\t\t%1$tF %1$tT"); System.out.println(String.format("result\t\t%1$tF %1$tT", currentTime)); System.out.println("original\tCurrent datetime use java.util.Date"); System.out.println("format\t\t%1$tY-%1$tm-%1$td %1$tH:%1$tM:%1$tS"); System.out.println(String.format("result\t\t%1$tY-%1$tm-%1$td %1$tH:%1$tM:%1$tS", date)); System.out.println("================================================================================"); System.out.println("===== What's the effect of width.precision on String? =========================="); System.out.println("original\tabcdefg"); System.out.println("format\t\t[%1$s]\t\t[%1$10s]\t[%1$10.4s]\t[%1$-10s]"); System.out.println(String.format("result\t\t[%1$s]\t[%1$10s]\t[%1$10.4s]\t[%1$-10s]", "abcdefg")); System.out.println("================================================================================"); System.out.println("===== padding easy =========================="); System.out.println("original\tadd leading zero"); System.out.println("format\t\t%012d"); System.out.println(String.format("result\t\t%012d", 123)); System.out.println("================================================================================"); System.out.println("===== test new line ============================================================"); System.out.println("original\t[%n]"); System.out.println("format\t\t%n"); System.out.println(String.format("result\t\t[%n]")); System.out.println("================================================================================"); } }

結果顯示:
===== test argument index ====================================================== original 10 20 format %1$s %1$d %2$s %2$d result "10" 10 "20" 20 original 10 20 30 40 format %1$s %3$d %4$s %2$d result "10" 30 "40" 20 ================================================================================ ===== formatted as string ====================================================== original "abc" null (byte)1 3.14 format %s %S %s %S result abc NULL 1 3.14 ================================================================================ ===== formatted as boolean ===================================================== original "abc" null (byte)1 3.14 format %b %B %b %B result true FALSE true TRUE ================================================================================ ===== formatted as hex ========================================================= original "100" null 100 new Integer(100) 3.14 format %h %H %h %H %h result bdf1 NULL 64 64 11e29ba7 ================================================================================ ===== formatted as character =================================================== original 'A' 97 97 format %s %C %c result A A a ================================================================================ ===== formatted as integer ===================================================== original 100 100 100 format %d %o %h result 100 144 64 ================================================================================ ===== formatted as big number ================================================== original 123456789L format %1$d %1$,d result 123456789 123,456,789 ================================================================================ ===== formatted as float ======================================================= original 123456789.98765 format %1$.2a %1$.2e %1$.2f %1$.2g result 0x1.d7p26 1.23e+08 123456789.99 1.2e+08 ================================================================================ ===== formatted as date ======================================================== original Current datetime use System.currentTimeMillis() format %1$tY-%1$tm-%1$td %1$tH:%1$tM:%1$tS result 2013-05-13 16:08:57 format %1$tF %1$tT result 2013-05-13 16:08:57 original Current datetime use java.util.Date format %1$tY-%1$tm-%1$td %1$tH:%1$tM:%1$tS result 2013-05-13 16:08:57 ================================================================================ ===== What's the effect of width.precision on String? ========================== original abcdefg format [%1$s] [%1$10s] [%1$10.4s] [%1$-10s] result [abcdefg] [ abcdefg] [ abcd] [abcdefg ] ================================================================================ ===== padding easy ========================== original add leading zero format %012d result 000000000123 ================================================================================ ===== test new line ============================================================ original [%n] format %n result [ ] ================================================================================

閱讀全文...

2013年4月30日 星期二

DB2 sequence 簡單語法

剛好遇上要用db2 sequence, 將試過的語法做個備忘。

1. create
CREATE SEQUENCE XX_SEQ START 1 INCREMENT BY 1 MAXVALUE 999999 CYCLE NO CACHE
說明:
START WITH:起始值
INCREMENT BY:每次增加多少
MAXVALUE:設定最大值,不設定就設成 NO MAXVALUE
CYCLE:當到達最大值時,是否從頭開始,不循環設成NO CYCLE
CACHE:一次產生多個值於記憶體中,方便快速取用,如 CACHE 3 ,就會產生3個值於記憶體中,若不使用,則設成NO CACHE

2. alter (重新設定起始值) ALTER SEQUENCE XX_SEQ RESTART WITH 10
3. 使用 next value for seq_name 取值
SELECT NEXT VALUE FOR XX_SEQ FROM sysibm.sysdummy1
4. drop(刪除)
DROP SEQUENCE XX_SEQ RESTRICT
閱讀全文...

2013年4月26日 星期五

Customize eclipse new class template

囧,會想研究一下這東西的出發點,是因為我懶,以前新增新類別後,一些註解說明都是複製之前的類別內容,在來慢慢改,其中大部份的內容完全一模一樣,為了省下這一段工,以懶為出發點,就來研究如何客製化eclipse code template。

Step 1. 從eclipse menu 點擊 Window -> Preferences -> Java -> Code Style -> Code Templates


Step 2. 展開 Code 或 Comment ,選擇要修改的項目,在點擊 Edit 即可進行修改。


Note: 測試後發現 Comment 這部份怎麼修改都沒有反應。
最後我修改了 Code 中的兩個項目:
New java files(原始內容,其中filecomment, typecomment對應到Comment那部分,修改無效) ${filecomment} ${package_declaration} ${typecomment} ${type_declaration}

New java files(修改後,其中的log4j因為都會用到,懶到最高點,把它加進來)
${filecomment} /* * @(#)${file_name} ${date} * * Copyright (C) 2004 Your Company. * All right reserved. */ ${package_declaration} import org.apache.log4j.Logger; ${typecomment} /** * 類別說明請新增 * @author ${user} * @version 1.0 ${date} */ ${type_declaration}

Class body(原本是空的,我加了log4j和Constructor) /** for log4j */ static Logger log = Logger.getLogger(${type_name}.class.getName()); /** Constructor */ public ${type_name}() {}

經過這樣修改後,每次新增類別時,我又可以省打好多字 ^_______________^
閱讀全文...

2013年4月25日 星期四

安裝 Skype 時,錯誤訊息:先前的安裝並未完成,而且需要重新開機

msn 雖然說4/8停止服務,但一直到今天才被強制要求更新至 skype,沒想到要安裝的時候一直出現『先前的安裝並未完成,而且需要重新開機』,可是重開機,錯誤訊息還是一樣,不給裝就是不給裝。


解決方式:
以下解決方式會修改到登錄編輯程式,請自行承擔使用「登錄編輯程式」的一切風險。
1. 按一下 [開始],再按一下 [執行]/windows 7是[搜尋程式與檔案]。
2. 輸入 regedit,再按一下 [確定]。
3. 找出並按一下下列登錄機碼:
   HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager
4. 找出並刪除機碼中的任何 PendingFileRenameOperations 值。
5. 提示您確認刪除時,按一下 [是]。
6. 結束 [登錄編輯程式]。
7. 重新執行 Skype 安裝程式。
閱讀全文...