2009年6月29日 星期一

JS instanceof判斷類型問題

判斷變量類型:一般情況下,可以先用typeof運算符,如果結果是"object",再用instanceof來判斷;
特別的地方是:
instanceof不認為原始類型值的變量是對象,

1. var temp="a string for test";
2. //下面這句返回"string"
3. alert( typeof temp);
4. //下面這句返回"false"
5. alert(temp instanceof String);
6.
7. //看看Ext內部實現,判斷String類型
8. if( typeof temp== "string"){
9. }

其他附加問題:

* null:表示尚未存在的對象,注意,儘管尚未存在,也是個對像啊,所以用typeof檢測一個null值變量的結果是Object;不過,為了便於寫 if語句,在js中,"undefined==false ", "null=false",因此,"undefined==null"。
*整數:最容易犯的錯誤就是,忘了070其實是個八進制數,相當於十進制的56;
*浮點數:“在進行運算之前,真正存儲的是字符串”——這應該是解釋執行的本質決定的吧——直接後果是,alert(0.8)這樣的語句可以正確輸出,而alert(2 *0.8)的輸出就成了"2.40000000000000003"
*數字邊界:數字有幾個邊界值,分別是Number.MAX_VALUE(最大值), Number.MIN_VALUE(最小值), Number.POSITIVE_INFINITY(正無窮), Number.NEGATIVE_INFINITY(負無窮), Infinity(無窮大,- Infinity,這個有點莫名其妙,不知道為啥又搞出一套);特別地,還有一個isFinit(iNumber)函數來判斷數字是否為無窮大。
* NaN:一些需要數字作為參數的函數,當傳入的實參無法轉換為數字時,往往會返回這個值;關於NaN,最重要的就是要記住NaN!=NaN,因此判斷一個變量是否為NaN,一定要使用isNaN(var)函數。
*將字符串轉換為數字:sVar.parseInt()是最常用的函數,也最容易出錯,為了保險起見,最好每次調用的時候,都加上“進制”的參數,比 如:a .parseInt(10),就制定了按十進制轉換字符串a;Number(sVar)也是一種轉換方式,不同的是,它要求整個字符串都得是有效數字,因 此Number("4.5.5")將返回NaN;

source: http://www.blogjava.net/oathleo

2009年6月28日 星期日

Oracle - 如何在 SELECT statement 中,實作 IF-THEN-ELSE 邏輯 ?

How does one implement IF-THEN-ELSE logic in a SELECT statement?

Submitted by admin on Sat, 2005-11-12 06:38

Oracle SQL supports several methods of coding conditional IF-THEN-ELSE logic in SQL statements. Here are some:
CASE Expressions

From Oracle 8i one can use CASE statements in SQL. Look at this example:

SELECT ename, CASE WHEN sal = 1000 THEN 'Minimum wage'
WHEN sal > 1000 THEN 'Over paid'
ELSE 'Under paid'
END AS "Salary Status"
FROM emp;
DECODE() Function

The Oracle decode function acts like a procedural statement inside an
SQL statement to return different values or columns based on the values of
other columns in the select statement. Examples:

select decode(sex, 'M', 'Male', 'F', 'Female', 'Unknown')
from employees;

select a, b, decode( abs(a-b), a-b, 'a > b',
0, 'a = b',
'a < b') from tableX;

Note: The decode function is not ANSI SQL and is rarely implemented
in other RDBMS offerings. It is one of the good things about Oracle,
but use it sparingly if portability is required.


GREATEST() and LEAST() Functions

select decode( GREATEST(A,B), A, 'A is greater OR EQUAL than B',
'B is greater than A')...



select decode( GREATEST(A,B),
A, decode(A, B, 'A NOT GREATER THAN B', 'A GREATER THAN B'),
'A NOT GREATER THAN B')...

NVL() and NVL2() Functions

NVL and NVL2 can be used to test for NULL values.

NVL(a,b) == if 'a' is null then return 'b'.

SELECT nvl(ename, 'No Name')
FROM emp;

NVL2(a,b,c) == if 'a' is not null then return 'b' else return 'c'.

SELECT nvl2(ename, 'Do have a name', 'No Name')
FROM emp;

COALESCE() Function

COALESCE() returns the first expression that is not null. Example:

SELECT 'Dear '||COALESCE(preferred_name, first_name, 'Sir or Madam')
FROM emp2;

NULLIF() Function

NULLIF() returns a NULL value if both parameters are equal in value. The following query would return NULL:

SELECT NULLIF(ename, ename)
FROM emp;

source: http://robertvmp.pixnet.net/blog/post/24147936

2009年6月23日 星期二

Startup Oracle listener

[root@localhost ~]# su - oracle
[oracle@localhost ~]$ cd
[oracle@localhost ~]$ source .bash_profile
\[oracle@localhost ~]$ lsnrctl start

LSNRCTL for Linux: Version 10.2.0.1.0 - Production on 24-JUN-2009 10:58:29

Copyright (c) 1991, 2005, Oracle. All rights reserved.

Starting /u01/app/oracle/product/10.2.0/db_1/bin/tnslsnr: please wait...

TNSLSNR for Linux: Version 10.2.0.1.0 - Production
System parameter file is /u01/app/oracle/product/10.2.0/db_1/network/admin/listener.ora
Log messages written to /u01/app/oracle/product/10.2.0/db_1/network/log/listener.log
Listening on: (DESCRIPTION=(ADDRESS=(PROTOCOL=ipc)(KEY=EXTPROC1)))
Listening on: (DESCRIPTION=(ADDRESS=(PROTOCOL=tcp)(HOST=localhost.localdomain)(PORT=1521)))

Connecting to (DESCRIPTION=(ADDRESS=(PROTOCOL=IPC)(KEY=EXTPROC1)))
STATUS of the LISTENER
------------------------
Alias LISTENER
Version TNSLSNR for Linux: Version 10.2.0.1.0 - Production
Start Date 24-JUN-2009 10:58:31
Uptime 0 days 0 hr. 0 min. 0 sec
Trace Level off
Security ON: Local OS Authentication
SNMP OFF
Listener Parameter File /u01/app/oracle/product/10.2.0/db_1/network/admin/listener.ora
Listener Log File /u01/app/oracle/product/10.2.0/db_1/network/log/listener.log
Listening Endpoints Summary...
(DESCRIPTION=(ADDRESS=(PROTOCOL=ipc)(KEY=EXTPROC1)))
(DESCRIPTION=(ADDRESS=(PROTOCOL=tcp)(HOST=localhost.localdomain)(PORT=1521)))
Services Summary...
Service "PLSExtProc" has 1 instance(s).
Instance "PLSExtProc", status UNKNOWN, has 1 handler(s) for this service...
The command completed successfully
[oracle@localhost ~]$ sqlplus sys/oracle as sysdba

SQL*Plus: Release 10.2.0.1.0 - Production on Wed Jun 24 10:58:45 2009

Copyright (c) 1982, 2005, Oracle. All rights reserved.

Connected to an idle instance.

SQL> startup
ORACLE instance started.

Total System Global Area 285212672 bytes
Fixed Size 1218992 bytes
Variable Size 100664912 bytes
Database Buffers 180355072 bytes
Redo Buffers 2973696 bytes

2009年6月17日 星期三

簡單獲取google天氣報告

簡單獲取google天氣報告

http://www.google.com/ig/api?hl=zh_tw&weather=hk


−

−









−








−







−







−







−











1 package com.pmjava.util;
2
3 import java.io.BufferedReader;
4 import java.io.BufferedWriter;
5 import java.io.FileWriter;
6 import java.io.IOException;
7 import java.io.InputStream;
8 import java.io.InputStreamReader;
9 import java.net.MalformedURLException;
10 import java.net.URL;
11
12
13 import java.io.*;
14 import org.w3c.dom.*;
15 import javax.xml.parsers.*;
16
17 public class GetWeather {
18
19
20 public String getweather(String city)
21 {
22 try {
23 URL ur = new URL("http://www.google.com/ig/api?hl=zh_cn&weather="+city);
24 InputStream instr = ur.openStream();
25 String s, str;
26 BufferedReader in = new BufferedReader(new InputStreamReader(instr));
27 StringBuffer sb = new StringBuffer();
28
29 Writer out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream("weather.txt"), "utf-8"));
30 while ((s = in.readLine()) != null) {
31 sb.append(s);
32 }
33 str = new String(sb);
34 out.write(str);
35 out.close();
36 in.close();
37
38
39 } catch (MalformedURLException e) {
40 e.printStackTrace();
41 } catch (IOException e) {
42 e.printStackTrace();
43 }
44 File f=new File("cityweather.txt");
45 DocumentBuilderFactory factory=DocumentBuilderFactory.newInstance();
46 String str=null;
47 try{
48 DocumentBuilder builder=factory.newDocumentBuilder();
49 Document doc = builder.parse(f);
50 NodeList nl = (NodeList) doc.getElementsByTagName("forecast_conditions");
51 NodeList n2=nl.item(0).getChildNodes();
52
53 str=n2.item(4).getAttributes().item(0).getNodeValue()+","+n2.item(1).getAttributes().item(0).getNodeValue()+"℃-"+n2.item(2).getAttributes().item(0).getNodeValue()+"℃";
54 }catch(Exception e)
55 {
56
57 }
58
59 return str;
60 }
61
62
63
64 }

2009年5月23日 星期六

JQuery 年度調查

在眾多 AJAX Framework 中,jQuery 只能算是後起之秀,但近來卻一路竄紅,成為許多網站開發人員注目的焦點。

jQuery 的發展
jQuery 的簡潔吸引了許多開發者,這可以從 Ajaxian.com 的年度調查中看出來。

2006 年 jQuery 問市首年的排名是第六名.


2007 年已升到第三名

2007 年已升到第三名



在 2008 年 3 月的一份問卷調查中,jQuery 已躍升到首位。
在 2008 年 3 月的一份問卷調查中,jQuery 已躍升到首位。

2009年5月20日 星期三

Screen Capture in Java

通過java.awt.Robot的createScreenCapture截屏.

public static void captureScreen(String fileName) throws Exception {

Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
Rectangle screenRectangle = new Rectangle(screenSize);
Robot robot = new Robot();
BufferedImage image = robot.createScreenCapture(screenRectangle);
ImageIO.write(image, "png", new File(fileName));
}

2009年5月18日 星期一

java/oracle日期處理

public class Test{
public static void main (String args []){
java.util.Date a = new java.util.Date();
System.out.println(a);
java.sql.Date b = new java.sql.Date(a.getTime());
System.out.println(b);
java.sql.Time c = new java.sql.Time(a.getTime());
System.out.println(c);
java.sql.Timestamp d=new java.sql.Timestamp(a.getTime());
System.out.println(d);
}
}

Mon Apr 03 18:00:34 CST 2006
2006-04-03
18:00:34
2006-04-03 18:00:34.388

1. oracle默認的系統時間就是sysdate函數,儲存的數據形如25-3-200510:55:33
2. java中取時間的對像是java.util.Date。
3. oracle中對應的時間對像是java.util.Date,java.sql.Time,java.sql.Timestamp、它們都是是java.util.Date的子類。
4. oracle中與date操作關係最大的就是兩個轉換函數:to_date(),to_char()。 to_date()一般用於寫入日期到數據庫時用到的函數。 to_char()一般用於從數據庫讀入日期時用到的函數。

DATE、TIME和TIMESTAMP:
SQL定義了三種與時間有關的數據類型:DATE由日、月和年組成。 TIME由小時、分鐘和秒組成。 TIMESTAMP將DATE和TIME結合起來,並添加了納秒域。
標準Java類java.util.Date可提供日期和時間信息。但由於該類包含DATE和TIME信息而沒有TIMESTAMP所需的納秒,因此並不與上述三種SQL類型完全相配。
因此我們定義了java.util.Date的三種子類。它們是:
1.有關SQL DATE信息的java.sql.Date
2.有關SQL TIME信息的java.sql.Time
3.有關SQL TIMESTAMP信息的java.sql.Timestamp
對於java.sql.Time,java.util.Time基本類的小時、分鐘、秒和毫秒域被設置為零。對於 java.sql.Date,java.util.Date基本類的年、月和日域被分別設置為1970年1月1日。這是在Java新紀元中的“零”日期。 java.sql.date中的日期可以和標準的SQL語句中含有日期的字段進行比較.java.sql.Timestamp類通過添加納秒域來擴展 java.util.Date。

oracle中兩個轉換函數:
1. to_date()作用將字符類型按一定格式轉化為日期類型:
具體用法:to_date(''2004-11-27'',''yyyy-mm-dd''),前者為字符串,後者為轉換日期格式,注意,前後兩者 要以一對應。如;to_date(''2004-11-27 13:34:43'', ''yyyy-mm-dd hh24:mi:ss'')將得到具體的時間。
2. to_char():將日期轉按一定格式換成字符類型:
具體用法:to_char(sysdate,''yyyy-mm-dd hh24:mi:ss'')

to_date()與24小時製表示法及mm分鐘的顯示:
在使用Oracle的to_date函數來做日期轉換時,很多Java程序員也許會直接的採用“yyyy-MM-dd HH:mm:ss”的格式作為格式進行轉換,但是在Oracle中會引起錯誤:“ORA 01810格式代碼出現兩次”。
如:select to_date('2005-01-01 13:14:20','yyyy-MM-dd HH24:mm:ss') from dual;
原因是SQL中不區分大小寫,MM和mm被認為是相同的格式代碼,所以Oracle的SQL採用了mi代替分鐘。 oracle默認的系統時間就是sysdate函數,儲存的數據形如2005-3-2510:55:33,java中取時間的對像是 java.util.Date。
select to_date('2005-01-01 13:14:20','yyyy-MM-dd HH24:mi:ss') from dual

在java對oracle的操作中,對日期字段操作的例子:
表book中有name varchar2(20)//書籍名稱,buydate Date //購買日期兩個字段。
已經創建了數據庫連接Connection conn;

方法一、使用java.sql.Date實現比較簡單的yyyy-mm-dd格式日期。 java.sql.Date不支持時間格式。切記不要使用new java.sql.Date(int year,int month,int date),因為還要處理時間差問題。
PreparedStatement pstmt = conn.prepareStatement("insert into book (name,buydate) values (?,?)");
java.sql.Date buydate=java.sql.Date.valueOf("2005-06-08");
pstmt.setString(1, "Java編程思想");
pstmt.setDate(2,buydate );
pstmt.execute();
方法二、使用java.sql.Timestamp,同上不使用new Timestamp(....)
PreparedStatement pstmt = conn.prepareStatement("insert into book (name,buydate) values (?,?)");
java.sql.Timestamp buydate=java.sql.Timestamp.valueOf("2004-06-08 05:33:99");
pstmt.setString(1, "Java編程思想");
pstmt.setTimestamp(2,buydate );
pstmt.execute();
方法三、使用oracle的to_date內置函數
PreparedStatement pstmt = conn.prepareStatement("insert into book (name,buydate) values (?,to_date(?, 'yyyy-mm-dd hh24:mi:ss')");
String buydate="2004-06-08 05:33:99";
pstmt.setString(1, "Java編程思想");
pstmt.setString(2,buydate );
pstmt.execute();
附:oracle日期格式參數含義說明
d:一周中的星期幾
day:天的名字,使用空格填充到9個字符
dd:月中的第幾天
ddd:年中的第幾天
dy:天的簡寫名
iw: ISO標準的年中的第幾週
iyyy:ISO標準的四位年份
yyyy:四位年份
yyy,yy,y:年份的最後三位,兩位,一位
hh:小時,按12小時計
hh24:小時,按24小時計
mi:分
ss:秒
mm:月
mon:月份的簡寫
month:月份的全名
w:該月的第幾個星期
ww:年中的第幾個星期

source: http://blog.csdn.net/senmon2004/archive/2006/04/07/653936.aspx