2009-06-01

自動增加字段

動增加這個語法真的是好用,新增一筆資料進去,自動幫你產生一個id

Ex:
create table TableName (
SurveyId int unsigned not null auto_increment,
primary key(SurveyId))
};

auto_increment 的那個參數一定要為primary key ,如果還有其他的primary key則會發生錯誤

若已存在table:
alter table TableName add SurveyId int unsigned auto_increment , add primary key(SurveyId);


可用此語法移除
alter table OtherPrimarykeyName drop primary key;

ps. 型態不一定要用 int unsigned ,int 也行

取得MySQL中的自動字串

EX:
Table 型態 : survey ( SurveyId int unsigned not null auto_increment ,
SurveyName varchar(20) not null ,
SurveyTemplateId varchar(5) not null)

Statement stmt;
stmt.executeUpdate ( " insert into survey ( SurveyName , SurveyTemplateId ) values( ' Name ' , ' Id ' );

int autoIncKeyFromFunc = -1;
ResultSet rs = stmt.executeQuery("SELECT LAST_INSERT_ID()");
if (rs.next())
autoIncKeyFromFunc = rs.getInt(1);
rs.close();

autoIncKeyFromFunc就是自動字串

取的方法好像好幾種,這只是其中一種

POI之Hssf的formula bug

問題是:
使用 countif 這個function 之後所產生該欄位的值是 #value!
使用辜狗大神找了很久都沒找到,有的是有人發問題但是沒有解...
後看發現問題點是在於該欄位沒有計算所導致,因為我點選fx之後值就算出來了
官網FormulaEvaluation的說明
所以猜測應該是沒計算所導致 依照官網的範例
就在程式碼中加上這行 "evaluator.evaluateFormulaCell(c);"
但是根本沒有這個"evaluateFormulaCell()" 傻眼...難道版本不同??
找到只有evaluate()能用,所以就將就用一下,試出來的結果還是一樣...囧...

後來一直反覆看都不知道為什麼??
在最上頭看到一個Note
In versions of POI before 3.0.3, this code lived in the scratchpad area of the POI SVN repository. If using an such an older version of POI, ensure that you have the scratchpad jar or the scratchpad build area in your classpath before experimenting with this code. Users of all versions of POI may wish to make use of a recent SVN checkout, as new functions are currently being added fairly frequently.
版本問題??
接著去湯姆貓看一下lib的版本,發現我真是個笨蛋ㄚ,因為我湯姆貓中poi的版本竟然有3個....
把舊的移除,留最新的版本一試
眼淚都要留下來了....
卡了好久終於有值了,而且我也把evaluator.evaluate()拿掉也可以,就照一般寫formula的方法
最後確定3.1沒有這個問題而舊的2個版本都不行(3.0.2和3.0)

POI之Hssf的跨欄置中

跨欄置中這個功能是常常被用到的
剛好專研有用到,查了一下用法大概如下:
HSSFSheet s;
s.addMergedRegion(new Region(0,(short)0,1,(short)0));
new Region() 裡面有4個參數
1.row起點位置
2.cell起點位置
3.row結束位置
4.cell結束位置
蠻直觀的,應該很好理解

Java-壓縮檔案之中文檔名也行

由於java內建的zip壓縮無法解決中文的檔名
拜辜狗大神所賜輕鬆找到解答:
使用apache的tool.zip "ant.jar"
辜狗大神是說湯姆貓有內健但我沒看到可能是阿帕契吧
由於不太相信湯姆貓沒有(因為我之前好像看過這個jar)
後來搜尋一下
竟然在eclipse中找到....
真神奇

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.OutputStream;
import org.apache.tools.zip.ZipOutputStream;

private void zip(String zipFileName, File inputFile) throws Exception {
ZipOutputStream out = new ZipOutputStream(new FileOutputStream(zipFileName));
zip(out, inputFile, "");
out.close();
}
private void zip(ZipOutputStream out, File f, String base) throws Exception {
if (f.isDirectory()) {
File[] fl = f.listFiles();
out.putNextEntry(new org.apache.tools.zip.ZipEntry(base + File.separator));
base = base.length() == 0 ? "" : base + File.separator;
for (int i = 0; i < fl.length; i++)
zip(out, fl[i], base + fl[i].getName());
}else {
out.putNextEntry(new org.apache.tools.zip.ZipEntry(base));
FileInputStream in = new FileInputStream(f);
int b;
while ( (b = in.read()) != -1)
out.write(b);
in.close();
}
}

中文檔案下載

編碼的問題
只要是編碼就是大問題
很難解決 欲哭無淚 傷透腦筋 差點睡著
卡了我一整個下午
最後終於在javaworld 找到答案 爽! javaworld文章
要先準備3個檔案
commons-codec-1.3.jar
commons-httpclient-3.0.1.jar
commons-logging-1.1.jar

這很好找 辜狗問一下就有官網可以下載



import org.apache.commons.httpclient.util.URIUtil;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;

String Filename = "xxx"; //下載時的檔案名稱
String FileSrc = "yyy"; //檔案在硬碟的路徑
try{
response.setHeader("Content-disposition","attachment; filename=" + URIUtil.encodeQuery(Filename));
} catch (URIException e){
out.println ("error = " + e.toString());
}

FileInputStream fis = new FileInputStream( filesrc );

OutputStream os=response.getOutputStream();
int byteRead;
while(-1 != (byteRead = fis.read()))
os.write(byteRead);
os.close();
if (fis != null) {
response.setStatus(response.SC_OK);
response.flushBuffer();
fis.close();
}