2007年3月18日 星期日

[Java] Socket



public class SocketServerTest implements java.lang.Runnable{
private int PORT = 52288;
private java.net.ServerSocket server = null;
private boolean shutdown = false;
public void run(){
try{
server = new java.net.ServerSocket(PORT);
try{
while(!shutdown){
java.net.Socket socket = server.accept();
try{
//非 140.113 就關掉
if( (( ((socket.getInetAddress()).getHostAddress()).substring(0,8)).compareTo("140.113.") != 0 ) ){
socket.close();
continue;
}

String datain = null;
java.io.BufferedReader in = new java.io.BufferedReader( new java.io.InputStreamReader( socket.getInputStream(),"UTF-8" ) );
datain = in.readLine();
}catch(java.net.SocketException e){
//socket關掉
}catch(java.lang.NullPointerException e){
//in.readLine()
}finally{
socket.close();
}
}
}catch(java.net.SocketException e){
//socket關掉
}finally{
server.close();
}
}catch(java.net.SocketException e){
//socket關掉
}catch(Exception e){
e.printStackTrace();
}
}
}

[Java] 與 MySQL 溝通




class dbMySQL{

final private String dbDriver = "com.mysql.jdbc.Driver";

public String dbAddress = null;

public String dbName = null;

public String dbUrl = null;

public String dbUser = null;

public String dbPasswd = null;

dbMySQL(String db_url,String db_name,String db_user,String db_passwd){

setupDB(db_url,db_name,db_user,db_passwd);

}

public void setupDB(String url,String dbname,String dbuser,String dbpasswd){

dbAddress = url;

setupDBUser(dbuser,dbpasswd);

setupDBName(dbname);

}

private void setupDBName(String dbname){

dbName = dbname;

//dbUrl = "jdbc:mysql://"+dbAddress+":3306/"+dbName+"?useUnicode=true&characterEncoding=Big5";

dbUrl = "jdbc:mysql://"+dbAddress+":3306/"+dbName+"?useUnicode=true&characterEncoding=utf-8";

}

private void setupDBUser(String dbuser,String dbpasswd){

dbUser = dbuser;dbPasswd = dbpasswd;

}

public java.sql.ResultSet SQLquery(String cmd){

try{

Class.forName(dbDriver);

java.sql.Connection con = java.sql.DriverManager.getConnection(dbUrl, dbUser, dbPasswd);

if(!con.isClosed()){

java.sql.PreparedStatement pstmt = con.prepareStatement(cmd);

java.sql.ResultSet rs = pstmt.executeQuery();

con.close();

return rs;

}

}catch(java.lang.ClassNotFoundException e){

e.printStackTrace();//Can't find: "+dbDriver+"

}catch(java.sql.SQLException e){

e.printStackTrace();

}

return null;

}

public String SQLupdate(String cmd){

try{

Class.forName(dbDriver);

java.sql.Connection con = java.sql.DriverManager.getConnection(dbUrl, dbUser, dbPasswd);

if(!con.isClosed()){

java.sql.PreparedStatement pstmt = con.prepareStatement(cmd);

pstmt.executeUpdate();

con.close();

return "OK";

}

}catch(java.lang.ClassNotFoundException e){

e.printStackTrace();

}catch(java.sql.SQLException e){

e.printStackTrace();

}

return "NO";

}

}



/* 使用方式
class Test{
private dbMySQL DB = null;
public Test(String db_url,String db_name,String db_user,String db_passwd){
DB = new dbMySQL(db_url,db_name,db_user,db_passwd);
DB.SQLupdate("DELETE From `MySQL_Table_Name`");

java.sql.ResultSet SQLrs =null;
SQLrs = DB.SQLquery("SELECT * FROM `MySQL_Table_Name`");
if(SQLrs != null){
while(SQLrs.next()){
//因為是用 UTF-8 存的, 所以讀出來要轉成 Big5
String data = new String( SQLrs.getString("欄位名字").getBytes(), "Big5");
}
}
}
}
*/


[Perl] 跟MySQL溝通




use DBI;



$dbh = DBI->connect('dbi:mysql:database=DM;host=140.113.55.66','5566','7788');

$sql = "select count(*) from `Games` Where `GameID`='55667788'";

$sth = $dbh->prepare($sql);

$sth->execute || die "Could not execute SQL statement ... maybe invalid? \n\n $sql \n";

#output database results

#while (@row=$sth->fetchrow_array){ print "@row\n" }

@row=$sth->fetchrow_array;

print "@row[1]\n";


[Perl] 檔案處理與簡單的判斷例子





# 檔案位置


$wwwshow="/home/user/public_html/test.htm";





# 開檔失敗,則會換成執行後面那段話


open(FD_In,$wwwshow) || die "Can't open file: $!" ;





while( defined($LineIn = ) ){


# 若有出現溫度跟F的符號,進行切字的笨手段


if($LineIn =~ /°F/){


# get thermometer F;


@tmp01Line = split($splittoken01_a,$LineIn);


@tmp02Line = split($splittoken01_b,$tmp01Line[1]);


$thermometerF{$location[$i]} = $tmp02Line[0];


$thermometerC{$location[$i]} = ($thermometerF{$location[$i]} - 32) * 5 / 9;


# get thermometer Feel Like F;


@tmp01Line = split($splittoken02_a,$LineIn);


@tmp02Line = split($splittoken02_b,$tmp01Line[1]);


$thermometerFeelLikeF{$location[$i]} = $tmp02Line[0];


$thermometerFeelLikeC{$location[$i]} = ($thermometerFeelLikeF{$location[$i]} - 32) * 5 / 9;


# get weather


@tmp01Line = split($splittoken03_a,$beLine);


@tmp02Line = split($splittoken03_b,$tmp01Line[1]);


$weather{$location[$i]} = $tmp02Line[0];


}elsif($LineIn =~ /$splittoken04/){


@tmp01Line = split($splittoken04_a,$LineIn);


@tmp02Line = split($splittoken04_b,$tmp01Line[1]);


$updatetime{$location[$i]} = $tmp02Line[0];


# last 指的就是 C/C++ 中的 break


last;


}


$beLine = $LineIn;


}


close(FD_In);


# 把檔案刪掉


unlink($wwwshow);





# 開檔準備寫入


open(WEATHER,">$wwwshow") || die "Can't open file: $!" ;


print WEATHER "Weather Information\n";


close(WEATHER);



2007年2月1日 星期四

貪婪‧末章

原本以為,這齣戲可以平靜地落幕,誰知,前一晚的紛爭道破那寧靜的夜。

這幾天大概參加了七八年的彌撒吧!常在彌撒曲的尾聲,瞧見幾位嘻鬧的臉兒穿梭在上一代的糾紛,誰也不知道,誰也不願想起,我的內心只有一個念頭:「就讓上一代的恩怨在我這一代終止吧,我實在不忍心向那嬌兒地臉旁道說著他父母,甚至祖父母的不是。」有些事還真像江謝說我女友之事,還真像八點檔勒!比扯鈴還扯!深刻地永遠烙印在我的生活環境。雖然我才二十多歲,但人生事體驗卻不輸於四十歲的天空,這齣戲我看得很難過…卻也不得不將它看完。

我常常無聊會看探索頻道、國家地理頻道或甚動物頻道等等,早在高中時就能接受大自然的物競天擇的場景,只是我萬萬也沒想到,這樣的情境會發生在兄弟姐妹,雖然在自然界中也是恆不變的定理,只是還是很難接受於人類。印象中,我有幾位朋友,他們很巧的都是父系親戚不協調,或許如戲所云,貪婪是天性。

似乎老實人總是吃虧,或許一切的一切將會於未來中慢慢得到報應。好像有說不完的故事,醜陋的人性,將一直陪伴著我,轉化為成長激素。