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

2009年4月27日 星期一

解決Eclipse中Java工程間循環引用而報錯的問題

如果我們的項目包含多個工程(project),而它們之間又是循環引用的關係,那麼Eclipse在編譯時會拋出如下一個錯誤信息:
A cycle was detected in the build path of project: XXX

解決方法非常簡單:

Eclipse Menu -> Window -> Preferences... -> Java -> Compiler -> Building -> Building path problems -> Circular dependencies ->將Error改成Warning

The Solution of the Problem That the Java Proejcts Have the Cycle References in Eclipse
If our project contains multiple proejcts, and the cycle references among them, Eclipse will throw out following error message while compiling:
"A cycle was detected in the build path of project: XXX"
The solution is quite simple:
Eclipse Menu -> Window -> Preferences... -> Java -> Compiler -> Building -> Building path problems -> Circular dependencies -> Change it from "Error" to "Warning".

source: http://yichen914.spaces.live.com/blog/cns!723590D920FAF62B!534.entry

2009年4月17日 星期五

一個經典的hibernate錯誤:a different object with the same identifier...

hibernate3.x上使用merge()來合併兩個session中的同一對象
a different object with the same identifier value was already associated with the session

一個經典的hibernate錯誤:a different object with the same identifier value was already associated with the session xxxx

hibernate3.x上使用merge()來合併兩個session中的同一對象,具體的Code就是

public Object getDomain(Object obj) {
getHibernateTemplate().refresh(obj);
return obj;
}
public void deleteDomain(Object obj) {
obj = getHibernateTemplate().merge(obj);
getHibernateTemplate().delete(obj);
}

或是
record = HibernateUtil.getCurrentSession().merge(record);
session.beginTransaction();
session.saveOrUpdate(record);
session.getTransaction().commit();



==========================

今天出现一点小问题,使用内存中的游离状态实体,因为它没有和 当前的session 相关,所以在进行维护该实体操作的时候,出现了

a different object with the same identifier value was already associated with the session 异常,

原因 用到数据库中持久的实体A时候,用它的标识直接在内存中生成,没有从session中取得,这样当维护这个实体相关信息的时候,session会发现,游离的实体和与session保存是查找到的相关的实体虽然标识一样,但是状态 不一样的冲突,从而不能完成事务,

而有同事介绍使用merge,后来查阅文档发现,使用merge的 时候,会发生一些隐形的问题:如果merge从新把游离的实体和session建立关联的时候,当这个游离的实体真的不存在,merge会创建一个非法的 实体,所以最好的办法应该是先从 当前的session中查询出实体,然后对这个相关的实体进行维护,就ok了。

Hibernate的用户曾要求一个既可自动分配新持久化标识(identifier)保存瞬时(transient)对象,又可更新/重新关联脱管(detached)实例的通用方法。 saveOrUpdate()方法实现了这个功能。

// in the first session
Cat cat = (Cat) firstSession.load(Cat.class, catID);
// in a higher tier of the application
Cat mate = new Cat();
cat.setMate(mate);
// later, in a new session
secondSession.saveOrUpdate(cat); // update existing state (cat has a non-null id)
secondSession.saveOrUpdate(mate); // save the new instance (mate has a null id)

saveOrUpdate()用途和语义可能会使新用户感到迷惑。 首先,只要你没有尝试在某个session中使用来自另一session的实例,你应该就不需要使用update()saveOrUpdate(),或merge()。有些程序从来不用这些方法。

通常下面的场景会使用update()saveOrUpdate()

  • 程序在第一个session中加载对象

  • 该对象被传递到表现层

  • 对象发生了一些改动

  • 该对象被返回到业务逻辑层

  • 程序调用第二个session的update()方法持久这些改动

saveOrUpdate()做下面的事:

  • 如果对象已经在本session中持久化了,不做任何事

  • 如果另一个与本session关联的对象拥有相同的持久化标识(identifier),抛出一个异常

  • 如果对象没有持久化标识(identifier)属性,对其调用save()

  • 如果对象的持久标识(identifier)表明其是一个新实例化的对象,对其调用save()

  • 如果对象是附带版本信息的(通过) 并且版本属性的值表明其是一个新实例化的对象,save()它。

  • 否则update() 这个对象

merge()可非常不同:

  • 如果session中存在相同持久化标识(identifier)的实例,用用户给出的对象的状态覆盖旧有的持久实例

  • 如果session没有相应的持久实例,则尝试从数据库中加载,或创建新的持久化实例 (可能创建垃圾数据)

  • 最后返回该持久实例

  • 用户给出的这个对象没有被关联到session上,它依旧是脱管的



ref: http://ideas.javaeye.com/blog/371103

2009年4月13日 星期一

Oracle/PLSQL: Months_Between Function

The syntax for the months_between function is:

months_between( date1, date2 )

date1 and date2 are the dates used to calculate the number of months.

If a fractional month is calculated, the months_between function calculates the fraction based on a 31-day month.


Applies To:

  • Oracle 8i, Oracle 9i, Oracle 10g, Oracle 11g

Example #1:

months_between (to_date ('2003/01/01', 'yyyy/mm/dd'), to_date ('2003/03/14', 'yyyy/mm/dd') )

would return -2.41935483870968


Example #2

months_between (to_date ('2003/07/01', 'yyyy/mm/dd'), to_date ('2003/03/14', 'yyyy/mm/dd') )

would return 3.58064516129032


Example #3

months_between (to_date ('2003/07/02', 'yyyy/mm/dd'), to_date ('2003/07/02', 'yyyy/mm/dd') )

would return 0


Example #4

months_between (to_date ('2003/08/02', 'yyyy/mm/dd'), to_date ('2003/06/02', 'yyyy/mm/dd') )

would return 2

source: http://www.techonthenet.com/oracle/functions/months_between.php

2009年3月25日 星期三

Marshalling

http://www.faqs.org/docs/artu/ch05s03.html#id2908194
http://www.codeproject.com/KB/IP/Marshal.aspx
http://blog.carrion.ws/2006/11/06/marshalling-problem-explained/