2007年11月20日 星期二

PostgreSQL 資源簡介

PostgreSQL 為完全免費的資料庫, 並且支援友善的client 介面,
下面列了一些簡單的資源提供各位參閱.

Client Tools

pgAdmin III
該工具為在 8.0 以後的版本為內建,不需要另外安裝
http://www.pgadmin.org/

Aqua Data Studio
http://www.aquafold.com/downloads.html

Development Reference

國際中文版社群
http://postgresql.oss.tw/
PostgreSQL 8.2.5 Documentation
http://www.postgresql.org/docs/8.2/interactive/index.html

PostgreSQL 8.0.0 中文文件
http://twpug.net/docs/postgresql-doc-8.0-zh_TW/

PostgreSQL 資料型態
http://twpug.net/docs/postgresql-doc-8.0-zh_TW/datatype.html

PostgreSQL安裝及基本備份操作
http://blog.roodo.com/shihty/archives/2364269.html

PostgreSQL 備份筆記
http://www.javaworld.com.tw/roller/page/ingramchen?entry=2007_5_25_SimplePostgreSQLBackup

Using the PostgreSQL Driver
http://www.faqs.org/docs/ppbook/x20856.htm

PostgreSQL Driver
http://jdbc.postgresql.org/download.html#current

2007年11月14日 星期三

[轉貼]BAT 批次檔的備份方式

在此分享一個利用Bat批次檔備份資料的方式,包含檔案的複製、刪除、FTP下載與壓縮,也可以利用LOG檔記錄執行的內容。

BACKUP.BAT

rem ----- 檔案名稱 : BACKUP.BAT -------------------------------------------------------
rem ----- 用途 : 利用 XCOPY 和 .BAT 功能做備份 ----------------------------------------

rem ----- 將 BACKUP.BAT ?#91;入 系統排?#123; ( ?#93;可以手動?#91;入 [?#125;始][?#123;式集][啟動] 中 ) ------
rem at 23:00 /every:s,m,t,w,th,f,sa "d:\LU\BACKUP.bat"

rem ----- XCOPY 用法 可用 "XCOPY /?" 查詢 ---------------------------------------------
rem XCOPY 差異備份常用參數
rem /D:m-d-y 複製在指定日期當天或之後發生變更的檔案。如果沒有給日期,只複製那些來源檔案日期比目的檔案日期為新的檔案。
rem /H 時複製隱藏檔和系統檔。
rem /I 如果目的不存在且複製一個以上的檔案的話,就假?#93;指定的目的一定是目錄。
rem /S 複製每個目錄及其?#93;含的子目錄,不複製空目錄。
rem /E 複製每個目錄及其?#93;含的子目錄,?#93;複製空目錄。/S 與 /E 相同,能夠用來修改 /T。
rem /Y 不要提示您確?#123;是否要覆蓋一個已經存在的檔案。

rem ----- 增?#91; route 資料, 廢除 ---------------
rem route add 140.127.177.17 192.168.0.2

rem ----- 將?#125;始備份時間寫入 log 紀錄中 -----------------------------------------------------------------------
rem %date% 表示日期環境變數, %date:~0,4% 表示 日期環境變數中從第 0 個位置抓出 4 個字
echo %date:~0,4%%date:~5,2%%date:~8,2% 網頁資料備份紀錄檔 > D:\Backup\log\%date:~0,4%%date:~5,2%%date:~8,2%.log
echo 始備份時間 >> D:\Backup\log\%date:~0,4%%date:~5,2%%date:~8,2%.log
TIME /T >> D:\Backup\log\%date:~0,4%%date:~5,2%%date:~8,2%.log

rem ------ [完整備份] 將網站資料備份到 以 日期為目錄名稱 的目錄中 --------------------------------------------------------
rem xcopy c:\inetpub D:\Backup\%date:~0,4%%date:~5,2%%date:~8,2% /h /i /s /k /o /y >> D:\Backup\log\%date:~0,4%%date:~5,2%%date:~8,2%.log

rem ----- [差異備份] 備份 MSN 表情符號, 改名成 .gif 方便?#91;看, 更名失敗表示重複, 可刪除
XCOPY "C:\Documents and Settings\lu\Application Data\Microsoft\MSN Messenger\1061698994\CustomEmoticons" "D:\CustomEmoticons" /D /H /I /S /Y
REN D:\CustomEmoticons\*.dat *.gif
DEL D:\CustomEmoticons\*.dat

rem ------ [差異備份] 備份 MSN 表情符號 到 File Server 的個人備份區
XCOPY D:\CustomEmoticons Z:\CustomEmoticons\ /D /H /I /S /Y

rem ------ [差異備份] 備份 我的最愛 到自己的 D: 磁碟 --------------------------------------------------------------
rem 使用 %USERPROFILE% 環境變數, 它代表 "C:\Documents and Settings\(使用者登入帳號)"
XCOPY %USERPROFILE%\Favorites "z:\Favorites\" /D /H /I /S /Y
rem XCOPY "C:\Documents and Settings\lu\Favorites" "D:\Favorites\" /D /H /I /S /Y

rem ------ [差異備份] 備份 Outlook Express 6.x 通訊錄 到自己的 D: 磁碟 -------------------------------------------------------------------
XCOPY "%USERPROFILE%\Application Data\Microsoft\Address Book" "D:\Address Book\" /D /H /I /S /Y
rem XCOPY "C:\Documents and Settings\lu\Application Data\Microsoft\Address Book" "D:\Address Book\" /D /H /I /S /Y

rem ------ [差異備份] 備份 我的最愛 到 File Server 的個人備份區 -----------------------------------------------------------
XCOPY %USERPROFILE%\Favorites "z:\Favorites\" /D /H /I /S /Y
rem XCOPY "C:\Documents and Settings\lu\Favorites" "z:\Favorites\" /D /H /I /S /Y

rem ------ [差異備份] 備份 Outlook Express 6.x 通訊錄 到 File Server 的個人備份區 -----------------------------------------------------------
XCOPY "%USERPROFILE%\Application Data\Microsoft\Address Book" "z:\Address Book\" /D /H /I /S /Y
rem XCOPY "C:\Documents and Settings\lu\Application Data\Microsoft\Address Book" "z:\Address Book\" /D /H /I /S /Y

rem ----- [壓縮完整備份] 將 D:\lu 用 WinRAR 壓縮放在 d:\lu.rar 或 d:\日期.rar ----------------------
rem WinRar 是商業軟體, 網?#125; http://www.rarsoft.com/
rem c:\progra~1\winrar\rar a -r d:\lu.rar d:\lu\*
rem c:\progra~1\winrar\rar a -r d:\%date:~0,4%%date:~5,2%%date:~8,2%.rar d:\lu\*
rem "C:\Program Files\WinRar\rar a -r d:\%date:~0,4%%date:~5,2%%date:~8,2%.rar d:\lu\*

rem ----- [壓縮完整備份] 將 D:\lu 用 7-zip 壓縮 ----------------------
rem 7-Zip 是免費軟體, 網?#125; http://www.7-zip.org/
rem "C:\program Files\7-zip\7z" a -r c:\%date:~0,4%%date:~5,2%%date:~8,2%.7z d:\lu\*.*
rem 壓縮成為 .zip 格式
rem "C:\program Files\7-zip\7z" a -r -tzip c:\%date:~0,4%%date:~5,2%%date:~8,2%.zip d:\lu\*.*

rem ----- 差異備份, 異動檔案輸出到 1.log -----
rem XCOPY D:\LU Z:\LU\ /D /H /I /S /Y >> c:\1.log
XCOPY D:\LU Z:\LU\ /D /H /I /S /Y

rem ------ 備份 Outlook 信件 -------------------------------------------------
rem echo 請先關閉 Outlook , 否則 Outlook 信件無法備份
rem pause
XCOPY "D:\Outlook" "z:\Outlook\" /D /H /I /S /Y

rem ------ 備份 hosts 檔案 -------------------------------------------------
XCOPY C:\WINDOWS\system32\drivers\etc\hosts z:\ /D /H /I /S /Y

rem ------ 輸出備份完成時間 -----------------------------------------------
echo 結束備份時間 >> D:\Backup\log\%date:~0,4%%date:~5,2%%date:~8,2%.log
TIME /T >> D:\Backup\log\%date:~0,4%%date:~5,2%%date:~8,2%.log
FTPSCEIPT.bat
FTP -s:ftp.txt 201.80.100.182
ftp.txt
inqgendoh  //使用者名稱
inqgen
//密碼
cd backup //切換目錄
bin
prompt
mget *.* //取得檔案
bye


參考來源:資安論壇

2007年9月12日 星期三

Collections API 簡介

Collections 提供了很多好用的method讓您操作資料結構
以下用一個簡單的範例來介紹:

double array[] = {123, 112, 57, 355, 255};
double subArr[] = {57};

List<Double> list = new ArrayList();
List subList = new ArrayList();

for (int i = 0; i < array.length; i++) {
list.add(new Double(array[i]));
}

for (int j = 0; j < subArr.length; j++) {
li.add(new Double(subArr[j]));
}

//排序, 印出 57,112,123,255,355
Collections.sort(list);

//反向排序
Collections.sort(list, Collections.reverseOrder());

//亂數排序
Collections.shuffle(list);

//倒轉元素, 印出 255,355,57,112,123
Collections.reverse(list);

//移動元素, 正數元素會往右移動, 負數則往左移動
Collections.rotate(list, 1);

//元素出現的頻率, 該例回傳 1
Collections.frequency(list, new Double(112));

//搜尋該元素的位置, 但使用該 method 必須是先排序過的
//例如使用Collections.sort(), 不然會回傳負值, 本例印出 1
Collections.binarySearch(list, new Double(112));

//將新元素取代原來的物件內容, 該例會全部印出 100
Collections.fill(list, new Double(100));

//比較子元素最後的位置, 本例印出 2(若找不到回傳-1)
Collections.lastIndexOfSubList(list,subList)

//比較子元素的位置
Collections.indexOfSubList(list,subList)

JOIN 的簡易操作

要連接兩張資料表, JOIN 是不可或缺的指令, 以下用兩張很簡單的表格,
來簡介如何操作 JOIN.

Table1: test1

Table1: test2

id

name

id

name

1

Allen

1

Allen

2

Bill

2

Bill

5

Jack

4

Jack


若我們的SQL指令如下:

select *
from test1 as t1 INNER JOIN test2 as t2
on t1.id = t2.id

則表示兩張表格的權重相等, 必須兩張表格都有相同 id 的資料, 才會取出
(在本例中, 兩張表格都有 id 1,2 這幾筆資料已達到 inner join 的條件)

再看下一條指令:

select *
from test1 as t1 LEFT JOIN test2 as t2
on t1.id = t2.id

使用 left join 表示合併的權重以左邊的表格為主, 條件會以左邊表格的 id 去比對
在本例中, 會列出 id 值為 5 的record.

至於 RIGHT JOIN 的原理跟 LEFT JOIN 是完全相同的,
只是差在它是以右邊表格的權重為主.

2007年9月10日 星期一

指定編譯器版本(eclipse, IDEA)

在進行不同的專案時,有時會遇到專案的編譯版本是不同的,
若沒有加以指定、則會遇到版本錯誤的訊息;以下簡易的分享eclipse、IDEA的指定方式。

eclipse: project-->properties-->java complier-->complier compliance level

idea: project-->module settings-->source-->language level

只要設定完成、加以重新編譯,專案就很容易可以切換到不同版本的JDK了。

2007年9月5日 星期三

Composing Methods

1. Extract Method
動機: 函示過於冗長, 或一眼無法得知該函示的用意
方法: 將程式碼組織, 並獨立出來, 注意在 refactoring 的過程, 需取有意義的函示與變數名稱

2. Inline Method
動機: 若該函示屬於不必要的, 或多餘的
方法: 將函示的程式碼整合進來, 並移除該函示

3. Inline Temp
動機: 暫時變數只被賦值一次
方法: 去除暫時變數, 直接使用運算結果, 可以在變數前加上 final 檢驗之

4. Replace Temp with Query
動機: 暫時變數只被賦值一次, 但可能被多次運用
方法: 去除暫時變數, 將運算的內容提煉到一個 private method

5. Introduce Explaining Variable
動機: 運算式複雜又難以閱讀時
方法: 將運算的內容提煉成變數, 來解釋該內容

6. Slipt Temporary Variable
動機: 程式有些變數不只被被賦值一次
方法: 針對每次賦值, 提供一個獨立、暫時的變數

7. Remove Assignments Parameters
動機: 有參數被直接賦值, 會降低程式的清晰度
方法: 以一個暫時變數, 代替參數的位置

8. Replace Method with Method Object
動機: 有一個大型函示, 無法直接對區域變數採行 Extract Method, 或有很多的區域變數是可以重複利用的方法: 將函示放進一獨立物件, 並將這些區域變數變成物件的 field

9. Substitute Algorithm
動機: 想把某個演算法替換為更清晰的演算法
方法: 將函示本體的演算法做替換

2007年8月28日 星期二

簡易的效能測試方式

以下是一支簡易的 method,可以讓您了解程式的運作花了多少資源, 以便調整.

並介紹 Runtime.getRuntime().maxMemory(), Runtime.getRuntime().totalMemory()
Runtime.getRuntime().freeMemory() 是如何被使用的.
public class FreeMemoryTest {
public static void main(String[] args) {
//JVM 能從OS得到最大的記憶體,如果下了 -Xmx 參數,將以該參數為主
long maxMem = Runtime.getRuntime().maxMemory();
//JVM 已經從OS得到可使用的記憶體,會越取越多,直到 maxMemory 的最大容量,超過則會丟出 java.lang.OutOfMemoryError
long totalMem = Runtime.getRuntime().totalMemory();
//目前使用完剩下的記憶體
long startMem = Runtime.getRuntime().freeMemory();
long startTime = System.currentTimeMillis();

for (int i = 0; i <= 10000; i++) {
String str = "foo";
System.out.println(str);
}
long endMem = Runtime.getRuntime().freeMemory();
System.err.println("使用記憶體(Bytes): " + (startMem - endMem));
long endTime = System.currentTimeMillis();
System.err.println("使用時間(毫秒): " + (endTime - startTime));
System.err.println("總共記憶體(Bytes):" + totalMem);
System.err.println("目前最大記憶體(Bytes):" + maxMem);
}
}

詳細可再參考
透過 Java 參數來改善 Java 效能