2013年11月23日 星期六

hsqldb - 使用date型態時一直出現錯誤java.sql.SQLDataException: data exception: invalid datetime format: yyyyMMdd...

參考:Chapter 10. Built In Functions

使用hsqldb+mybatis執行以下的sql cmd:


UPDATE TABLE      SET TRANS_NET_DIS=0,TRANS_SALES=0,TRANS_ADJ_DIS=0,TRANS_FEE=0,TRANS_DIS=0,TRANS_NET_SALES=0,TRANS_ADJ_SALES=0,TRANS_RECEIPT=0       WHERE STMT_DATE=to_date('20121230','yyyyMMdd') AND SEQE='00' AND MERCH='999160691' AND ORGG='822'


然後出現以下的錯誤:

### Cause: java.sql.SQLDataException: data exception: invalid datetime format: yyyyMMdd
at org.apache.ibatis.exceptions.ExceptionFactory.wrapException(ExceptionFactory.java:23)
at org.apache.ibatis.session.defaults.DefaultSqlSession.update(DefaultSqlSession.java:150)
at org.apache.ibatis.binding.MapperMethod.execute(MapperMethod.java:49)
at org.apache.ibatis.binding.MapperProxy.invoke(MapperProxy.java:43)
at com.sun.proxy.$Proxy6.updateIsKeyData(Unknown Source)
at com.ctbc.transfile.sql.mybatis.UpdateIsKeyData.execute(UpdateIsKeyData.java:21)
at com.ctbc.transfile.sql.mybatis.UpdateIsKeyData.execute(UpdateIsKeyData.java:1)
at com.ctbc.util.sql.MyBatisUtil.execute(MyBatisUtil.java:42)
at com.ctbc.transfile.loadtask.check.KeyChecker.rmvAndUpdateKeyExistsBeans(KeyChecker.java:89)
at ut.com.ctbc.transfile.loadtask.check.KeyCheckerTest.testRmvKeyExistsBeans_Exists(KeyCheckerTest.java:211)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:27)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:271)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:70)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)
at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
Caused by: java.sql.SQLDataException: data exception: invalid datetime format: yyyyMMdd
at org.hsqldb.jdbc.Util.sqlException(Unknown Source)
at org.hsqldb.jdbc.Util.sqlException(Unknown Source)
at org.hsqldb.jdbc.JDBCPreparedStatement.fetchResult(Unknown Source)
at org.hsqldb.jdbc.JDBCPreparedStatement.execute(Unknown Source)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at org.apache.ibatis.logging.jdbc.PreparedStatementLogger.invoke(PreparedStatementLogger.java:55)
at com.sun.proxy.$Proxy8.execute(Unknown Source)
at org.apache.ibatis.executor.statement.PreparedStatementHandler.update(PreparedStatementHandler.java:41)
at org.apache.ibatis.executor.statement.RoutingStatementHandler.update(RoutingStatementHandler.java:66)
at org.apache.ibatis.executor.SimpleExecutor.doUpdate(SimpleExecutor.java:45)
at org.apache.ibatis.executor.BaseExecutor.update(BaseExecutor.java:100)
at org.apache.ibatis.executor.CachingExecutor.update(CachingExecutor.java:75)
at org.apache.ibatis.session.defaults.DefaultSqlSession.update(DefaultSqlSession.java:148)
... 33 more
Caused by: org.hsqldb.HsqlException: data exception: invalid datetime format: yyyyMMdd
at org.hsqldb.error.Error.error(Unknown Source)
at org.hsqldb.error.Error.error(Unknown Source)
at org.hsqldb.HsqlDateTime.toJavaDatePattern(Unknown Source)
at org.hsqldb.HsqlDateTime.toDate(Unknown Source)
at org.hsqldb.FunctionCustom.getValue(Unknown Source)
at org.hsqldb.FunctionSQL.getValue(Unknown Source)
at org.hsqldb.RangeVariable$RangeIteratorMain.getFirstRow(Unknown Source)
at org.hsqldb.RangeVariable$RangeIteratorMain.initialiseIterator(Unknown Source)
at org.hsqldb.RangeVariable$RangeIteratorMain.next(Unknown Source)
at org.hsqldb.StatementDML.executeUpdateStatement(Unknown Source)
at org.hsqldb.StatementDML.getResult(Unknown Source)
at org.hsqldb.StatementDMQL.execute(Unknown Source)
at org.hsqldb.Session.executeCompiledStatement(Unknown Source)
at org.hsqldb.Session.execute(Unknown Source)
... 47 more

後來找到hsqldb的官方說明,直接找到下方的Table 10.1. TO_CHAR, TO_DATE and TO_TIMESTAMP format elements部分,可以看到允許的都是『大寫』,因此將上述執行的yyyyMMdd改為YYYYMMDD,就可以解決這個問題了.


2013年11月18日 星期一

mybatis - 使用Oracle時出現Error updating database. Cause: java.sql.SQLSyntaxErrorException: ORA-00911: 字元無效



假設我有以下的Mapper.xml:
  
        INSERT INTO UPDATETIME (FILE_NAME, LASTUPDATED) 
     VALUES ( #{filename}, SYSDATE);
  

執行時出現錯誤:
### The error may involve com.ctbc.transfile.sql.mybatis.IKeyCheckMapper.updateUpdateTime-Inline
### The error occurred while setting parameters
### SQL: UPDATE UPDATETIME      SET    LASTUPDATED = SYSDATE      WHERE  FILE_NAME=?;
### Cause: java.sql.SQLSyntaxErrorException: ORA-00911: 字元無效


原因很簡單,尾端不要加上分號(;)即可.

(不知道為什麼oracle語法明明就是要加分號在mybatis上卻不需要加...)


mybatis - Mapper.xml語法參數使用方式

參考:Mapper XML Files

有兩種使用方式:

  1. 使用#{data}
    使用$就等同使用JDBC中的PreparedStatement方式設定參數,如下:
     
     < select id="selectPerson" parametertype="int" resulttype="hashmap">
      SELECT * FROM PERSON WHERE ID = #{id}
     </ select>
    

    類似於我們直接使用以下的方式:
     
    // Similar JDBC code, NOT MyBatis…
    String selectPerson = "SELECT * FROM PERSON WHERE ID=?";
    PreparedStatement ps = conn.prepareStatement(selectPerson);
    ps.setInt(1,id);
    

    此種方式最明顯的好處就是防止SQL injection
  2. 使用${data}
    此種方式等於簡單的字串相加,所以假設以上的ID是字串形態的參數,那麼我們就必須在前後加上單引號('):
     
      < select id="selectPerson" parameterType="int" resultType="hashmap">
        SELECT * FROM PERSON WHERE ID = '${id}'
      < /select>
    

    所以上述的方式僅僅是組成SQL COMMAND:SELECT * FROM PERSON WHERE ID = 'iddata'直接送到sql去執行,
    最明顯的問題就是SQL injection,因此在mybatis的官方說明上也標注僅可能避免使用此方式.

2013年11月12日 星期二

Quartz - 出現org.quartz.SchedulerException: Problem instantiating class...


問題:出現org.quartz.SchedulerException: Problem instantiating class 導致JOB無法順利被初始化執行


請檢查Job Class是否有實做default construction

2013年11月9日 星期六

maven - 如何新增自定義的3rd party dependency library

參考:guide-3rd-party-jars-local

需求:我需要新增一個自己定義的library jar(非公開),讓maven pom.xml可以在dependency中使用

做法:
   首先打開cmd,使用以下指令將自定義的jar加入

mvn install:install-file -Dfile=<path-to-file> -DgroupId=<group-id> \ -DartifactId=<artifact-id> -Dversion=<version> -Dpackaging=<packaging>

以上參數說明如下:

  • -Dfile:欲加入的jar所在位置
  • -DgroupId:group-id  
  • -DartifactId:artifact-id
  • -Dversion:version
  • -Dpackaging:壓縮的方式,在這裡直接填入jar

範例如下:
 mvn install:install-file -Dfile=/Users/wuanne/workspace/3rd-java-lib/piwi/piwi.jar -DgroupId=com.tw.piwi -DartifactId=piwi -Dversion=1.0.0 -Dpackaging=jar

之後在pom.xml中可如下方式使用:


  
             
  
   com.tw.piwi
   piwi
   1.0.0
  
            
       

maven - 使用eclipse執行時出現 copy-dependencies is not supported by m2e

參考:http://stackoverflow.com/questions/8271106/m2e-copy-dependencies-is-not-supported-by-m2e

需求:maven的pom.xml中使用maven-dependency-plugin,來使用第三方jar,卻報以下錯誤:
copy-dependencies is not supported by m2e


方式:
  1.
  先在pom.xml 中的<build></build>中加入

     
 
            
                
                    org.eclipse.m2e
                    lifecycle-mapping
                    1.0.0
                    
                        
                            
                                
                                    
                                        org.apache.maven.plugins
                                        maven-dependency-plugin
                                        [2.0,)
                                        
                                            copy-dependencies
                                        
                                    
                                    
                                        
                                    
                                
                            
                        
                    
                
            
          


2.右鍵->run as->Maven Install

  等待步驟2跑完後即可解決

2013年11月4日 星期一

android - logcat只記錄ERROR訊息(不使用eclipse)


  1. 在console執行adb logcat *:E
  2. 將手機連上電腦(一定要在執行完畢1後再連接)

Android - 出現 E/AndroidRuntime(25261): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo....java.lang.ClassNotFoundException


Q:出現 E/AndroidRuntime(25261): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo....java.lang.ClassNotFoundException

A:
首先檢查AndroidManifest.xml中的Activity name參數是否有設定錯誤(package name or activity class name),但發現並無錯誤,
最後找到是因為我有使用第三方的SDK Project,但是並沒有正確設定好,請參照下圖: