2015年11月9日

動態的 preparedstatment sql

因為種種原因例如安全(SQL Injection),效能等
所以使用JAVA的preparedstatment 方式
但是preparedstatment 很難使用動態方式
 例如
select * from tablea where colb in ???
例如colb是使用者可多選擇的, 例如有b1 b2 b3 ~ bN不等
使用可以任意選擇多個,因此 colb in 就不知道要幾個了
因為preparedstatment 使用 sql 跟 參數(有順序)的方式
使得SQL很難共用
例如
1.
select * from tablea
where akey = ?
and adel is null
and amrk in ('A','B')
and adate1 between ? and ?

2.
select * from tablea
where akey = ?
and adel is null
and amrk in ('A','B')
and akind in (?,?,?,...)

上面兩個SQL很明顯的重復下面
select * from tablea
where akey = ?
and adel is null
and amrk in ('A','B')
但是如果希望可以重復使用的話,就很難整合
因為 akey=? 要怎麼整合

String basicsql = "select * from tablea  where akey = ? and adel is null and amrk in ('A','B')";

List queryAdate(String akey,Date date1,Date date2){
  ...
  sql = String.format("%s and adate1 between ? and ?",basicsql );
  args = new Object[]{akey,date1,date2};
  ...
}

List queryAmrk(String akey,String [] mrklist){
  ...
  sql = String.format("%s and akind in (?,?,?,...)",basicsql );
  args = new Object[]{akey,mrklist....};--?這裡就???
  ...
}

又最常見的條件查詢
select * from tablea where atype = ? and akind in (?,?..) and aprice between ? and ? and aname like '?%'
其中 atype  akind  aprice aname  四個條件可以自由選擇
所以
sql = "select * from tablea where";
if StringUtils.isNotEmpty(atype ) {
 sql+="and atype =?";
}
if akind .length > 0 {
 sql+="and akind in (?,?..) ";
}

if aprice .length == 2 {
 sql+="aprice between ? and ? ";
}
if StringUtils.isNotEmpty(aname ) {
 sql+="and aname like '?%'";
}

其中要處理and 或是用很奇怪的 1=1
並且在參數方面也會不好用
因此針對preparedstatment,提出了模組化的需求
以下是測試範例
https://github.com/swpoker/sql/blob/master/sqltest/com/swpoker/util/sql/test/SQLTest.java

1.將SQL及參數封裝起來
2.提供一些方法使用


以上述的案例就會變成
void query(String atype , String [] akind ,int [] aprice ,String name)
sqlclause=SQLFactory.SQL().append("select * from tablea where")
.append(SQLFactory.WHERE()
.test(StringUtils.isNotEmpty(atype )).add("atype =?",atype )
.test(akind .length > 0 ).add(SQLFactory.IN("akind in (%?)",akind ))
.test(aprice .length == 2).add("aprice  between ? and ? ",aprice[0],aprice[1] )
.test(StringUtils.isNotEmpty(aname )).add("aname like '?%'",name)
)
String sql = sqlclause.sql();
Object[] args = sqlclause.args();

此套件為
1.提供preparedstatment模組化
2.提供方便的語法








2013年12月2日

jalbum-電子相簿製作軟體

jalbum 電子相簿製作軟體 變數清單 http://jalbum.net/en/developer/skins/variables

2012年12月19日

mac 使用 python

在 mac 上使用python
mac os : osx 10.6.8
python : 3.3.0

下載python官網的安裝檔後 http://www.python.org/download/
會有一個python 3.3 的app,可是這個app卻是使用mac 內建的python(2.X)
所以要修改一下執行的python的路徑

2012年12月13日

TOMCAT JNDI-datasource 設定

TOMCAT JNDI設定
JNDI設定的地方有多個,可參考
http://tomcat.apache.org/tomcat-7.0-doc/config/context.html
在此已設定下列檔案為範例
$CATALINA_BASE/conf/context.xml

oracle 10G的JNDI-datasource 設定範例
1.$CATALINA_BASE/conf/context.xml
2.$CATALINA_BASE/lib底下放置
ojdbc14.jar
commons-dbcp-1.4.jar
commons-pool-1.6.jar
需注意要連結的資料庫版本及其適用的jdbc的jar檔
3.JNDI的路徑為:java:comp/env/jdbc/db1
4.使用範例

2012年12月4日

利用python所撰寫的網頁抓取範例

利用python所撰寫的網頁抓取範例
由於我不怎麼信任網站的html的dom,所以都是用字串來擷取內容
api
python 2.4