2012年8月12日 星期日

Log4j PatternLayout

好吧 = =

等我英文夠好 我會把他記起來的

http://logging.apache.org/log4j/1.2/apidocs/org/apache/log4j/PatternLayout.html

重要的部分節錄如下


The recognized conversion characters are

Conversion CharacterEffect
cUsed to output the category of the logging event. The category conversion specifier can be optionally followed by precision specifier, that is a decimal constant in brackets.If a precision specifier is given, then only the corresponding number of right most components of the category name will be printed. By default the category name is printed in full.
For example, for the category name "a.b.c" the pattern %c{2} will output "b.c".
CUsed to output the fully qualified class name of the caller issuing the logging request. This conversion specifier can be optionally followed by precision specifier, that is a decimal constant in brackets.If a precision specifier is given, then only the corresponding number of right most components of the class name will be printed. By default the class name is output in fully qualified form.
For example, for the class name "org.apache.xyz.SomeClass", the pattern %C{1} will output "SomeClass".
WARNING Generating the caller class information is slow. Thus, use should be avoided unless execution speed is not an issue.
dUsed to output the date of the logging event. The date conversion specifier may be followed by a date format specifier enclosed between braces. For example, %d{HH:mm:ss,SSS} or %d{dd MMM yyyy HH:mm:ss,SSS}. If no date format specifier is given then ISO8601 format is assumed.The date format specifier admits the same syntax as the time pattern string of the SimpleDateFormat. Although part of the standard JDK, the performance of SimpleDateFormat is quite poor.
For better results it is recommended to use the log4j date formatters. These can be specified using one of the strings "ABSOLUTE", "DATE" and "ISO8601" for specifying AbsoluteTimeDateFormat, DateTimeDateFormat and respectivelyISO8601DateFormat. For example, %d{ISO8601} or %d{ABSOLUTE}.
These dedicated date formatters perform significantly better than SimpleDateFormat.
FUsed to output the file name where the logging request was issued.WARNING Generating caller location information is extremely slow and should be avoided unless execution speed is not an issue.
lUsed to output location information of the caller which generated the logging event.The location information depends on the JVM implementation but usually consists of the fully qualified name of the calling method followed by the callers source the file name and line number between parentheses.
The location information can be very useful. However, its generation is extremely slow and should be avoided unless execution speed is not an issue.
LUsed to output the line number from where the logging request was issued.WARNING Generating caller location information is extremely slow and should be avoided unless execution speed is not an issue.
mUsed to output the application supplied message associated with the logging event.
MUsed to output the method name where the logging request was issued.WARNING Generating caller location information is extremely slow and should be avoided unless execution speed is not an issue.
nOutputs the platform dependent line separator character or characters.This conversion character offers practically the same performance as using non-portable line separator strings such as "\n", or "\r\n". Thus, it is the preferred way of specifying a line separator.
pUsed to output the priority of the logging event.
rUsed to output the number of milliseconds elapsed from the construction of the layout until the creation of the logging event.
tUsed to output the name of the thread that generated the logging event.
xUsed to output the NDC (nested diagnostic context) associated with the thread that generated the logging event.
XUsed to output the MDC (mapped diagnostic context) associated with the thread that generated the logging event. The X conversion character must be followed by the key for the map placed between braces, as in %X{clientNumber} whereclientNumber is the key. The value in the MDC corresponding to the key will be output.
See MDC class for more details.
%The sequence %% outputs a single percent sign.
By default the relevant information is output as is. However, with the aid of format modifiers it is possible to change the minimum field width, the maximum field width and justification.
The optional format modifier is placed between the percent sign and the conversion character.
The first optional format modifier is the left justification flag which is just the minus (-) character. Then comes the optional minimum field width modifier. This is a decimal constant that represents the minimum number of characters to output. If the data item requires fewer characters, it is padded on either the left or the right until the minimum width is reached. The default is to pad on the left (right justify) but you can specify right padding with the left justification flag. The padding character is space. If the data item is larger than the minimum field width, the field is expanded to accommodate the data. The value is never truncated.
This behavior can be changed using the maximum field width modifier which is designated by a period followed by a decimal constant. If the data item is longer than the maximum field, then the extra characters are removed from the beginning of the data item and not from the end. For example, it the maximum field width is eight and the data item is ten characters long, then the first two characters of the data item are dropped. This behavior deviates from the printf function in C where truncation is done from the end.
Below are various format modifier examples for the category conversion specifier.

Format modifierleft justifyminimum widthmaximum widthcomment
%20cfalse20noneLeft pad with spaces if the category name is less than 20 characters long.
%-20ctrue20noneRight pad with spaces if the category name is less than 20 characters long.
%.30cNAnone30Truncate from the beginning if the category name is longer than 30 characters.
%20.30cfalse2030Left pad with spaces if the category name is shorter than 20 characters. However, if category name is longer than 30 characters, then truncate from the beginning.
%-20.30ctrue2030Right pad with spaces if the category name is shorter than 20 characters. However, if category name is longer than 30 characters, then truncate from the beginning.
Below are some examples of conversion patterns.

%r [%t] %-5p %c %x - %m%n
This is essentially the TTCC layout.
%-6r [%15.15t] %-5p %30.30c %x - %m%n
Similar to the TTCC layout except that the relative time is right padded if less than 6 digits, thread name is right padded if less than 15 characters and truncated if longer and the category name is left padded if shorter than 30 characters and truncated if longer.

2012年8月11日 星期六

xml定義 DTD & XSD


iBatis
iBatis Config
<!DOCTYPE sqlMapConfig
      PUBLIC "-//ibatis.apache.org//DTD SQL Map Config 2.0//EN"
      "http://ibatis.apache.org/dtd/sql-map-config-2.dtd">
iBatis SqlMap
<!DOCTYPE sqlMap
      PUBLIC "-//iBATIS.com//DTD SQL Map 2.0//EN"  
      "http://www.ibatis.com/dtd/sql-map-2.dtd">

Struts2
<!DOCTYPE struts
      PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
      "http://struts.apache.org/dtds/struts-2.0.dtd">

Web-app (web.xml)

XML Doctype Definition (dtd before servlet 2.3)
<!DOCTYPE web-app
      PUBLIC "-//Sun Microsystems, Inc.//DTD WebApplication 2.2//EN"
      "http://java.sun.com/j2ee/dtds/web-app_2.2.dtd">

<!DOCTYPE web-app
      PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
      "http://java.sun.com/dtd/web-app_2_3.dtd">

XML Schema Definition (xsd after servlet 2.4)
<web-app
      xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee    http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
version="2.4">


<web-app

      xmlns="http://java.sun.com/xml/ns/javaee"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="http://java.sun.com/xml/ns/javaee    http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
      version="2.5">

Spring


beans (2.0 dtd)

<!DOCTYPE beans
      PUBLIC "-//SPRING//DTD BEAN 2.0//EN"
      "http://www.springframework.org/dtd/spring-beans-2.0.dtd">


beans (2.0 xsd)

<beans
      xmlns="http://www.springframework.org/schema/beans"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">

beans (3.0 xsd)
<
beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">



http://www.springframework.org/schema/aop
         http://www.springframework.org/schema/aop/spring-aop-3.0.xsd

http://www.springframework.org/schema/context
         http://www.springframework.org/schema/context/spring-context-3.0.xsd

http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd

http://www.springframework.org/schema/jee 
     http://www.springframework.org/schema/jee/spring-jee-3.0.xsd

http://www.springframework.org/schema/tx
     http://www.springframework.org/schema/tx/spring-tx-3.0.xsd

http://www.springframework.org/schema/oxm
    http://www.springframework.org/schema/oxm/spring-oxm-3.0.xsd

http://www.springframework.org/schema/jms http://www.springframework.org/schema/jms/spring-jms-3.0.xsd

http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang-3.0.xsd

http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache.xsd

http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd





2012年7月31日 星期二

LDAP基本查詢語法

LDAP基本查詢語法


開始工作真的在搞一些有的沒有

而且每個客戶的做法也都不一樣 太有趣了 ="=

先前我們使用的方式是使用帳號密碼 試著登入ldap server如果能登入就是合法的使用者

今天看到某客戶(!!!!)的demo code

事先使用一個類似admin的帳號 可以取得(查詢)所有的user

再連線至LDAP比較密碼 是否相同 (很怪吧!!!)

總之 今天有用到在LDAP的查詢語法 XDD


Ref: http://technet.microsoft.com/zh-tw/library/dd159860.aspx
Ref: http://www.novell.com/documentation/developer/jldap/jldapenu/api/




SQL Server中與Metadata相關的預設table

SQL Server 2000中(之後可以改用VIEW來完成相同的工作)

有一些系統的table來儲放資料的metadata

System Tables in Every Database

  store database-level system information for each database.
syscolumns
Sysindexkeys
syscomments
sysmembers
sysconstraints
sysobjects
sysdepends
syspermissions
sysfilegroups
sysprotects
sysfiles
sysreferences
sysforeignkeys
systypes
sysfulltextcatalogs
sysusers
sysindexes


查詢某TABLE的某COLUMN存不存在


use [DB_NAME]
GO
DECLARE @tb_name VARCHAR(512), @col_name VARCHAR(512)
SET @tb_name = 'TABLE_NAME'
SET @col_name = 'COL_NAME'

IF NOT EXISTS(
SELECT 1
from sys.sysobjects so
INNER JOIN sys.syscolumns sc ON sc.id = so.id
where so.name = @tb_name AND sc.name = @col_name
)
BEGIN
PRINT 'COLUMN ''' + @col_name+ '''  is NOT EXISTS!!'
END
ELSE
BEGIN
PRINT 'COLUMN  ''' + @col_name+ '''  is  EXISTS!!'
END
GO




http://msdn.microsoft.com/en-us/library/aa260604(v=sql.80)

SQL Server中與Metadata相關的預設View

SQL Server 2005, 2008, 2008 R2, 2012
在INFORMATION_SCHEMA中有一些預設的system view可以來查詢一些跟資料庫相關的資訊
像是table名稱、column名稱等


CHECK_CONSTRAINTS
REFERENTIAL_CONSTRAINTS
COLUMN_DOMAIN_USAGE
ROUTINES
COLUMN_PRIVILEGES
ROUTINE_COLUMNS
COLUMNS
SCHEMATA
CONSTRAINT_COLUMN_USAGE
TABLE_CONSTRAINTS
CONSTRAINT_TABLE_USAGE
TABLE_PRIVILEGES
DOMAIN_CONSTRAINTS
TABLES
DOMAINS
VIEW_COLUMN_USAGE
KEY_COLUMN_USAGE
VIEW_TABLE_USAGE
PARAMETERS
VIEWS


Ref: http://msdn.microsoft.com/en-us/library/ms186778.aspx



2012年6月12日 星期二

用一維陣列解n皇后問題

用一維陣列解n皇后問題


  使用 2 維的陣列來儲存棋盤 似乎是比較直覺的做法

  所以我們會在 map[n][n] 這樣的陣列裡面填上0跟1來表示皇后放在哪裡

  接著檢查的方式 就是檢查 每一個行、每一個列、斜角有沒有被放上皇后

  OK~ 簡單Easy~

  接下來 討論一下利用一維陣列 來儲存

  直接將 map[i][j] = 1 取代成 arr[i] = j

  檢查的方式呢

  檢查map[i][j]第 i 列的部分 由於我們填入的時候 是一列一列的填入 (依照index) 所以可以跳過了

  檢查map[i][j]第 j 行的部分 就檢查目前放入陣列的數字有沒有重複就可以了

   至於斜角也很簡單 原先map[i][j]與map[[k][l]是否是在斜線的關系 

   原先使用 |i-k| == |j-l| if (i,j ) != (j, k)來檢查

   現在改用 arr[i] = j 與 arr[k] = l 就會變成 |i - k| == |arr[i] - arr[k]|  if (i,j ) != (j, k)

   OK~ 所以完成使用一維陣列取代 二維陣列 解 n皇后問題~

a160: 柏森想要下棋!!!
http://zerojudge.tw/ShowProblem?problemid=a160

import java.util.Arrays;
import java.util.Scanner;

public class JAVA {
    public static void main(String[] args) {
        Scanner cin = new Scanner(System.in);
        int n = 0 ;
        while (cin.hasNext()) {
            n = cin.nextInt();
            if(n > 0)
             printQueen(n);
            else break ;
        }
    }
    
    static void printQueen(int n){
  int count = 0 ;
  int map[] = new int[n];
  int level = 0;
  Arrays.fill(map, -1);
     for(int i = 0 ; i < n ; i++){
      map[i] = level;
      count += dfs(n, i, level, map);
      map[i] = -1 ;
     }
     System.out.println(count);
     System.out.println();
    }

    // map[i] = j -->> map[i][j] -> checked
    // map[i][j], map[k][l] are conflicted >>> |i-k| == |j-l|
 private static int dfs(int n, int i, int level, int[] map) {
  if(level == n-1){
   printQueenMap(n, map);
   return 1 ;
  }else{
   int count = 0;
   for(int k = 0 ; k < n ; k++){
    if(map[k] != -1) continue;
    boolean flag = true;
    for(int ii = 0 ; flag && ii < n ;ii++){
     if(map[ii] != -1)
      if(Math.abs(ii-k) == Math.abs(map[ii]-level-1)){
       flag = false ;
      }
    }
    if(flag){
     map[k] = level+1;
     count += dfs(n,k,level+1, map);
     map[k] = -1;
    } 
   }
   return count;
  }
  
 }

 private static void printQueenMap(int n, int[] map) {
  for(int x = 0 ; x < n ; x++){
   int tmp = -1;
   for(int y = 0; y < n ; y++){
    if(map[y] == x){
     tmp = y ;
     break;
    }
   }
   String XX = repeat("x",n);
   System.out.print(XX.substring(n-tmp));
   System.out.print("Q");
   System.out.println(XX.substring(tmp+1));
  }
  System.out.println();
 }
    public static String repeat(String s, int times) {
        if (times <= 0) return "";
        else return s + repeat(s, times-1);
    }
}

2012年3月4日 星期日

Closures

JavaScript - Closures are means through which inner functions can refer to the variables present in their outer enclosing function after their parent functions have already terminated.

2012年2月10日 星期五

歷史紀錄

原始資料表

歷史資料表 << 原始資料表
  + 更新序號
  + 更新時間
  + 更新方式 (A, U, D)
  (+ 更新人)
  (+ 更新原因)

trigger
新增
  insert into 原始資料表
修改
  update 原始資料表
刪除
  delete from 原始資料表

------------------

歷史資料表只能新增 再 藉由trigger對 原始資料表作更動

由於所以無法指藉由資料來顯示是新增或是刪除
所以需要一個變更方式的欄位

------------------ update @ 2012/2/10 05:43
java 可以建立一個歷史紀錄的類別(介面)

public interface HistoricalData{

  void setUpdateSeq(int updateSeq);
  int getUpdateSeq();
  void setUpdateDatetime(Date datetime);
  Date getUpdateDatetime();
  void setUpdateType(String updateType);
  String getUpdateType();

}

2011年12月15日 星期四

libraries

Spring framework將Java的Reflection做了很厲害的包裝
用來完成IoC和DI
IoC : Inversion of Control
DI : Dependency Injection

Struts2 來自 WebWork2 Framework是用來建立Java web application的framework

struts2-spring : Spring與Struts2可以透過 struts2-spring-plugin 來連接 在Struts2中使用Spring

Apache commons dbcp - Database Connection Pooling
http://commons.apache.org/dbcp/
包裝了常用的連接資料庫用的功能
dbcp依賴 jakarta commons-pool

Json-lib能將beans, maps, collections, java arrays, XML等轉成JSON物件 與 把它反轉成 beans and DynaBeans
依賴

  • jakarta commons-lang 2.5
  • jakarta commons-beanutils 1.8.0
  • jakarta commons-collections 3.2.1
  • jakarta commons-logging 1.1.1
  • ezmorph 1.0.6


2011年10月13日 星期四

JDBC String 的參數列表

JDBC String 的參數列表
用到 zeroDateTimeBehavior 所以找到這個東東
因為資料庫填的日期是0000-00-00沒有辦法給Java 的Date類別轉換
這個參數有三種值 exception, round, convertToNull分別是丟出例外, 轉換成0000-01-01, 回傳null三種

What should happen when the driver encounters DATETIME values that are composed entirely of zeros (used by MySQL to represent invalid dates)? Valid values are "exception", "round" and "convertToNull".

http://dev.mysql.com/doc/refman/5.0/en/connector-j-reference-configuration-properties.html