2008年3月31日 星期一

[Java] 亂數密碼產生器

public class CreatePassWord{
public static void main(String args[]){
//產生亂數密碼
int[] word = new int[8];
int mod;
for(int i = 0; i < 8; i++){
mod = (int)((Math.random()*7)%3);
if(mod ==1){ //數字
word[i]=(int)((Math.random()*10) + 48);
}else if(mod ==2){ //大寫英文
word[i] = (char)((Math.random()*26) + 65);
}else{ //小寫英文
word[i] = (char)((Math.random()*26) + 97);
}
}
StringBuffer newPassword = new StringBuffer();
for(int j = 0; j < 8; j++){
newPassword.append((char)word[j]);
}
System.out.println(newPassword);
}
}

2008年3月28日 星期五

[SQL] 基本 SQL 語法

1. SELECT
基礎:
SELECT * FROM {表格名}


2. INSERT INTO
基礎:
INSERT INTO {表格名} ( 欄位一, 欄位二, 欄位三 )
VALUES ( '{欄位一的值}', '{欄位二的值}', '{欄位三的值}' )

一階:
INSERT INTO {表格名} ( 欄位一, 欄位二, 欄位三 )
SELECT xxx1 AS 欄位一, xxx2 AS 欄位二, xxx3 AS 欄位三 FROM {表格名二}

3. UPDATE

UPDATE {表格名}
SET
{欄位一} = {欄位一的值},

{欄位二} = {欄位二的值}

WHERE {過濾條件}


4. DELETE
DELETE FROM {表格名}
WHERE {過濾條件}

2008年3月26日 星期三

[JAVA] Sorting HashMap base on Value

Answer:
Example:
you could subclass HashMap and provide a new method 'iterator()' that returns an Iterator that will iterate over the elements in the order you require:

Code 1.

class SortedHashMap extends HashMap {
public Iterator iterator() {
Collection collection = this.values();
Object[] array = collection.toArray();
Arrays.sort(array);
return Arrays.asList(array).iterator();
}
}

Code2:

SortedHashMap map = new SortedHashMap();

map.put("111", "Fred");
map.put("222", "Bill");
map.put("333", "Harry");
map.put("444", "Alan");
map.put("555", "Dave");
map.put("666", "Jim");

System.out.println("Sort by value (name):");

Iterator iter = map.iterator();
while (iter.hasNext()) {
System.out.println(iter.next());
}

System.out.println("\nRetrieve value (name) by index:");

System.out.println("444 = " + map.get("444"));
System.out.println("222 = " + map.get("222"));
System.out.println("111 = " + map.get("111"));
System.out.println("555 = " + map.get("555"));
System.out.println("333 = " + map.get("333"));
System.out.println("666 = " + map.get("666"));

/* Output :
Sort by value (name):
Alan
Bill
Dave
Fred
Harry
Jim

Retrieve value (name) by index:
444 = Alan
222 = Bill
111 = Fred
555 = Dave
333 = Harry
666 = Jim
*/Any programming problem can be solved

[JAVA] Sorting HashMap base on Key

Question:
How to sort a hashmap using its key?

Answer:
Dump the HashMap into a TreeMap

Map yourMap= new HashMap();
// put some tuples in yourMap ...
Map sortedMap = new TreeMap(yourMap);

Example:
import java.util.*;

public class Test{

public static void main(String args[]){
Map map = new HashMap();
map.put("key3", "value1");
map.put("key1", "value2");
map.put("key2", "value3");
Map sortedMap = new TreeMap(map);
System.out.println(sortedMap);
}
}