2008年12月4日 星期四

如果使用javax 的功能

要使用javax 的功能, 只要在classpath中指定dt.jar就可以了.
例如
C:\Program Files\Java\jdk1.6.0_06\lib\dt.jar

2008年12月2日 星期二

自定package sample

package flymilk;
public class u_head_java {

public static void main(String args[]) {

System.out.println("Hello world!!");

}
}

//---------------------------------------------------------
設定class
set classpath=C:\flymilk;.
//---------------------------------------------------------
編譯這個class
javac -d . u_head_java.java
//---------------------------------------------------------
執行這個class
java flymilk.u_head_java
//---------------------------------------------------------

編好的class 放到 目錄下
C:\Tomcat\webapps\ROOT\WEB-INF\classes <--放class
C:\Tomcat\webapps\ROOT\WEB-INF\lib <--放jar 檔

class 裏面的function 必須是public 的

2008年11月14日 星期五

SQL資料庫還原令

如果SQL資料庫備份檔要還原的檔案位置, 和原備份位置不相同,
就要修改指令檔備份

指令範例:

RESTORE DATABASE [DSCSYS]

FROM DISK = N'C:\Program Files\Microsoft SQL Server\MSSQL.1\MSSQL\Backup\DSCSYS_backup_200811140000.bak\DSCSYS_backup_200811140000.bak'
WITH MOVE 'DSCSYS_data' TO 'C:\Program Files\Microsoft SQL Server\MSSQL.1\MSSQL\DATA\DSCSYS.mdf',
MOVE 'DSCSYS_log' TO 'C:\Program Files\Microsoft SQL Server\MSSQL.1\MSSQL\DATA\DSCSYS.log',
REPLACE;
GO

2008年10月20日 星期一

使用 apache fileupload (含中文亂碼修正)

之前上傳是使用SmartUpload, 原本是沒問題, 後來tomcat 更新之後.
上傳就出現亂碼, 在網路上花好幾天找解決方法, 但我都設不起來,
我就想下載新版SmartUpload, 後來才發現連SmartUpload網站都消
失了, SmartUpload大概也不會有人更新了. 所以我改用apache
的fileupload , 而且它還持續在更新中. 下面文章是我從各家好手
中的文章試出來, 然後匯整在一起來. 希望能幫大家的忙.
因為找資料試程式真的很辛苦.

一. 先到apache 網站下載 io 及 fileupload 元件
解壓縮後放到C:\Tomcat\webapps\ROOT\WEB-INF\lib 目錄中,
接著重開tomcat 就算成功完裝了.




二.測試上傳功能

<html>
<head>
<title>檔案上傳</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
</head>
<body>
<b>檔案上傳</b></font></p>

<form name="UploadForm" enctype="multipart/form-data" method="post" action="test2.jsp">
<input type="file" name="File1" size="20" maxlength="20"> <br>
<input type="file" name="File2" size="20" maxlength="20"> <br>
<input type="submit"value="上傳">
</form>

</body>
</html>




接收上傳的頁面

<%@ page import="javax.servlet.*"%>
<%@ page import="javax.servlet.http.*"%>
<%@ page import="java.io.*"%>
<%@ page import="java.util.*"%>
<%@ page import="java.util.regex.*"%>
<%@ page import="org.apache.commons.fileupload.*"%>

<%@page contentType="text/html;charset=UTF-8"%>
<%
response.setContentType("text/html;charset=utf-8");
request.setCharacterEncoding("utf-8");

String enc = System.getProperty("file.encoding");

out.println(enc);
%>
<html>
<head>
<title>文件上傳處理頁面</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
</head>
<body>
<%

DiskFileUpload fu = new DiskFileUpload();

fu.setSizeMax(2*1024*1024);

fu.setSizeThreshold(4096);
//先處存到暫存區
fu.setRepositoryPath(application.getRealPath("/") + "temp/");
//開始上處
List fileItems = fu.parseRequest(request);
// 依序處理上傳文件
Iterator iter = fileItems.iterator();

//檢查上傳的檔案格式
String regExp=".+\\\\(.+)$";

//拒絕文件類型
String[] errorType={".exe",".com",".cgi",".asp"};
Pattern p = Pattern.compile(regExp);
while (iter.hasNext()) {
FileItem item = (FileItem)iter.next();
//如果是request item 就在這接收
if (item.isFormField()){
String name = item.getFieldName();
String value = item.getString();
out.println("name:"+item.getFieldName());
out.println("value:"+item.getString("UTF-8"));
}

//如果是上傳檔案, 就在這接收

if (!item.isFormField()) {
String name = item.getName();
long size = item.getSize();
if((name==null||name.equals("")) && size==0)
continue;
Matcher m = p.matcher(name);
boolean result = m.find();
if (result){
for (int temp=0;temp<errorType.length;temp++){
if (m.group(1).endsWith(errorType[temp])){
throw new IOException(name+": wrong type");
}
}
try{

//儲存檔案到指定位置


item.write(new File(application.getRealPath("/") + "temp/" + m.group(1)));

out.println(name+"  "+size+"<br>");
// out.println(new String(name.toString().getBytes(), "UTF-8")+"  "+size+"<br>");
}
catch(Exception e){
out.println(e);
}

%>
<TABLE class=text width=100% border=0>
<SCRIPT LANGUAGE="JavaScript">
function back(num)
{
num = num * (-1);

window.parent.frames[1].location.reload();
history.go(num);
}
</SCRIPT>

<TR>
<TD width="10%" >
<FORM METHOD="post">
<INPUT TYPE="button" VALUE="回上一頁" onClick="back(1)">

</FORM>
</TD>


</TR>
</table>
<%

}
else
{
throw new IOException("fail to upload");
}
}
}
%>
</body>
</html>

2008年10月5日 星期日

清除SQL 2005 資料庫 LOG

2008年9月30日 星期二

使用IReport 產生EXCEL檔的範例








JasperPrint jasperPrint;
JRResultSetDataSource jrds=null;
/* 帶參數進去給報表 */
Map map = new HashMap();
map.put("name","王小明");
Date d = new Date();
String Pdf_Name= (d.getYear()+1900)+"_"+(d.getMonth()+1)+"_"+d.getDate()+"_"+d.getHours()+"_"+d.getMinutes()+"_"+d.getSeconds();


try{
StrSQL = " SELECT SN, PRODUCT_TYPE, TC001, TD004, TD005, TD008, TB005_A, TA018_A, BAD_RATE_A, TA017_A, TB005_B, TA018_B, BAD_RATE_B, TA017_B, TB005_C, TA018_C, ";
StrSQL += " BAD_RATE_C, TA017_C, TB005_4, TA018_4, BAD_RATE_4, TA017_4, SUB_BAD_RATE ";
StrSQL += " FROM CARD_MOC013R ";
StrSQL += " ORDER BY PRODUCT_TYPE, TC001 ";
//out.println(StrSQL);

rs= stmt.executeQuery(StrSQL);


rs= stmt.executeQuery(StrSQL);
if(!rs.next()){
out.println("沒有資料,請重新查詢!!");
out.println("統計表");
}else{
rs.previous() ;
String fileName = application.getRealPath("MOC013R.jasper");
String outPath = application.getRealPath("MOC013R_"+Pdf_Name+".xls");
String outPath2 = application.getRealPath("MOC013R_"+Pdf_Name+".pdf");

File reportFile1 = new File(application.getRealPath("MOC013R.jasper"));

out.println("統計表_EXCEL版");



JasperPrint report = JasperFillManager.fillReport(fileName,map, new JRResultSetDataSource(rs));
JRAbstractExporter exporter = new JExcelApiExporter();
FileOutputStream output = new FileOutputStream(outPath);

exporter.setParameter(JRExporterParameter.JASPER_PRINT, report);
exporter.setParameter(JRXlsExporterParameter.IS_WHITE_PAGE_BACKGROUND, false);
exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, output);
exporter.exportReport();
output.close();

JasperReport jasperReport = (JasperReport) JRLoader.loadObject(reportFile1.getPath());
jasperPrint = JasperFillManager.fillReport(jasperReport,map, new JRResultSetDataSource(rs));

JasperPrintManager.printReportToPdfFile(jasperPrint, outPath2);
/* 把報表轉換成 PDF, 給定 ( RecordSet, 輸出路徑 ) */
JasperExportManager.exportReportToPdfFile(jasperPrint,outPath2);
}

/* 連線關閉 */
} catch (JRException e) {
e.printStackTrace();
}

ireport 的四則運算

IReport 說好用也好用,說難用也很難用!!
它的功能很強,不輸水晶,但操作起來並不直覺。
但習慣後還真不能沒有它。雖然它讓我吃盡苦頭。
如下面是兩數相除,要用下面的語法
new java.lang.Float( ($V{SUM_TA018_C_1}.floatValue()) /($V{SUM_TB005_C_1}.floatValue()) )

如下面僅僅是兩個數相加
(new BigDecimal( Double.parseDouble($V{SUM_week_1_6} +"") +Double.parseDouble($V
{SUM_week_2_4}+"") ))

這個是Boolean判斷式的寫法
new Boolean($V{REPORT_COUNT}.intValue() % 2 == 0)

這是if 判斷的寫法
$F{quantity} == null ? "No data" : String.valueOf($F{quantity})

在IReport 中的四則運算, 不能只單純使用數目的加減乘除, 所以如果忘記或不熟加減乘除的寫法,
可以照下面圖片, 去產生語法範例, 注意喔, 它僅僅是範例, 所以還有改寫成要 去產生語法範例, 注意喔, 它僅僅是範例, 所以還有改寫成要操作的運算式, 在報表中使用.

2008年5月6日 星期二

iREPORT的Sample

呼!!
之前完全沒有iREPORT的經驗, 這段語法測了好久,
現在已經可以使用兩個範例,
一個是直接使用MOC001.jasper,
一個就是這個可以把SQL語法帶入報表中.

<%@ page contentType="text/html;charset=UTF-8"%>
<%
response.setContentType("text/html;charset=utf-8");
request.setCharacterEncoding("utf-8");
%>
<%@page import="net.sf.jasperreports.engine.*"%>
<%@page import="net.sf.jasperreports.engine.JasperFillManager"%>
<%@page import="net.sf.jasperreports.engine.JasperRunManager"%>
<%@page import="net.sf.jasperreports.engine.*"%>
<%@page import="net.sf.jasperreports.engine.util.*"%>
<%@page import="net.sf.jasperreports.engine.export.*"%>
<%@page import="net.sf.jasperreports.j2ee.servlets.*"%>
<%@page import="net.sf.jasperreports.engine.JasperPrintManager"%>
<%@page import="net.sf.jasperreports.view.*"%>
<%@ page import="java.util.*"%>
<%@ page import="java.io.*"%>
<%@ page import="java.sql.*"%>



<%
request.setCharacterEncoding("UTF-8");
response.setContentType("text/html;charset=utf-8");

/* 連線部分 */
Connection conn=null;
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con = DriverManager.getConnection("jdbc:odbc:pg","pg","_pg");
Statement stmt = con.createStatement();
ResultSet rs;


try {

//SQL語法
String StrSQL = " select * from test ";
//out.println(StrSQL);

rs= stmt.executeQuery(StrSQL);
if(!rs.next()){return;}
else{

out.println(rs.getString("訂單日期"));
out.println("有資料");

JRResultSetDataSource jrds=null;

JasperPrint jasperPrint=null;

Map reportParams = new HashMap();

JasperReport jasperReport = (JasperReport) JRLoader.loadObject(application.getRealPath("MOC001.jasper"));

jrds = new JRResultSetDataSource(rs);

jasperPrint =JasperFillManager.fillReport(jasperReport, reportParams,jrds);
//產出PDF
JasperPrintManager.printReportToPdfFile(jasperPrint, application.getRealPath("MOC001.pdf"));

/* 把報表轉換成 PDF, 給定 ( RecordSet, 輸出路徑 ) */
//JasperExportManager.exportReportToPdfFile(jasperPrint,outPath);


/* 把報表轉換成 PDF, 給定 ( 輸入路徑, 輸出路徑, 參數, 連線 ) */
//JasperRunManager.runReportToPdfFile(filePath, outPath, map, con);
}
} catch (JRException e) {
e.printStackTrace();
}


con.close();

%>
<% out.println("讀取ERP每日進度表");%>

2008年4月30日 星期三

APACHE2.2搭配TOMCAT6

先裝APACHE2.2, 再裝TOMCAT6
然後只需要修改APACHE2.2\conf\httpd.conf 這個檔案,
在這個檔案最底層加入下列程式碼就可以了


SetHandler server-status
Order Deny,Allow
Deny from all
Allow from all


SetHandler balancer-manager
Order Deny,Allow
Deny from all
Allow from all

ProxyRequests Off

ProxyPass /examples http://192.168.42.1:8080/examples/

2008年4月28日 星期一

iReportJasperreportsample

要在jsp上產生pdf 檔 ,
第一步是安裝iReport , 然後編輯報表檔.

第二步使用iReport編譯生成報表名.jasper檔

然後在jsp上讀取.jasper然後生成 *.pdf

jsp sample

<%@ page contentType="text/html;charset=BIG5"%>
<%@ page import="net.sf.jasperreports.engine.*"%>
<%@ page import="java.util.*"%>
<%@ page import="java.io.*"%>
<%@ page import="java.sql.*"%>
<%
/* 連線部分 */
Connection conn=null;
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con = DriverManager.getConnection("jdbc:odbc:pg","pg","pgusr");

/* 取得 *.jasper 檔案的路徑, 並轉換成 String 物件 */
File inputFile = new File(application.getRealPath("AlanTest.jasper"));
String filePath = inputFile.getPath();

/* 設定輸出 PDF 檔的路徑位置 */
String outPath = application.getRealPath("BBB.pdf");
//String outPath = application.getRealPath("AlanTest.jasper") "C:\\BBB.pdf";
/* 帶參數進去給報表 */
Map map = new HashMap();
map.put("name","王小明");

/* 把報表轉換成 PDF, 給定 ( 輸入路徑, 輸出路徑, 參數, 連線 ) */
JasperRunManager.runReportToPdfFile(filePath, outPath, map, con);

/* 連線關閉 */
con.close();

%>
<% out.println("讀取生產報表");%>
如果產生的PDF裏面中文字是空白, 要做以下的設定去處理
去下載iTextAsian.jar
將iTextAsian.jar copy 至 iReport 及tomcat中的Lib中

要顯示中文的文字欄位屬性修改:
Font name:選用新細明體
PDF font name: 選用MHei-Medium
最下面的PDF Embeded要打勾
PDF encoding換用有(Chinese Traditional)的
這樣設置後就能產生正確無誤的中文PDF檔了。



下面談到很多安裝步驟,但其實是不需要使用jasperreports-2.0.5. 及裝ant的,
只要裝iReport 就可以了。

1. 下載 iReport-2.0.5
一個window執行檔, 在視寫介面下可以無痛安裝,但要使用java jdk 1.6以上版本,

iReport官方網站:http://ireport.sourceforge.net
iReport函式庫下載:http://0rz.net/8e0zZ

2. 下載 JasperReport
放到C槽底下
JasperReports官方網站:http://jasperreports.sourceforge.net
JasperReports函式庫下載:http://0rz.net/f10xQ

iTextAsian.jar函式庫下載:http://0rz.net/fe0to

3. 下載 Ant 工具

放到C槽底下, 需要指定CLASSPATH

4. 安裝
目錄解壓縮後就算安裝完成了, 第一步要start Server.
先開啟一個 console ,然後到 C:\jasperreports\demo\hsqldb 下

輸入以下指令:
# ant runServer

看到以下訊息:
Buildfile: build.xml

runServer:
[java] server.properties not found, using command line or default properties
[java] Opening database: test
[java] HSQLDB server 1.7.1 is running
[java] Use SHUTDOWN to close normally. Use [Ctrl]+[C] to abort abruptly
[java] Mon Apr 07 16:22:21 CST 2008 Listening for connections ...

很好!跑起來了。再另外開一個 console 介面:
再到隨便一個範例中,擺放範例的目錄C:\jasperreports\demo\samples,
到 barbecue 看看
# cd C:\jasperreports\demo\samples\barbecue

給他 ant -p 一下,看看目前 build.xml 有定義了哪些 task 可以玩:
# ant –p

Buildfile: build.xml
Shows how barcodes could be included in reports using specilized barcode APIs.
Main targets:

clean Deletes all the generated files.
compile Compiles the XML report design and produces the .jasper file.
csv Generates a CSV version of the report by converting the .jrprint file.

fill Fills the compiled report design with data and produces the .jrprint file.

html Generates an HTML version of the report by converting the .jrprint file.

javac Compiles the java source files of the test application.

jxl Generates an XLS version of the report by converting the .jrprint file using the JExcelApi library.

odt Generates an ODT version of the report by converting the .jrprint file.

pdf Generates a PDF version of the report by converting the .jrprint file.

print Sends the report in the .jrprint file to the printer.

rtf Generates an RTF version of the report by converting the .jrprint file.

run Generates a PDF file by filling the .jasper report design. No intermediate .jrprint file is produced.

view Launches the report viewer to preview the report stored in the .jrprint file.

viewDesign Launches the design viewer to preview the compiled report design.

viewDesignXML Launches the design viewer to preview the XML report design.

viewXML Launches the report viewer to preview the generated report stored in an XML file.

xls Generates an XLS version of the report by converting the .jrprint file using the POI library.

xml Generates an XML version of the report by converting the .jrprint file.

xmlEmbed Generates an XML version of the report by converting the .jrprint file.

Default target: javac

嘿嘿,還真多項目啊,每個項目都有說明,看看就大概知道他在幹嘛了。預設 task 是 javac。
好吧,繼續照說明書走吧。

# ant javac
因為預設項目是 javac
所以要直接下 ant 省略 javac 也行。

# ant compile

Buildfile: build.xml

compile:



來吧,把該塞進去的資料塞一塞:
# ant fill

看一看成品
# ant view

看一看還沒塞資料的時候的羞澀模樣
# ant viewDesign

把成品轉成 pdf 的型式看看
# ant run

把剛剛產生的東西都清光光
# ant clean


這篇文章是參考一個很棒的原創網站 http://blog.xuite.net/wildwindjen/engineer而寫的,若
要看全文請到這個網站參觀http://blog.xuite.net/wildwindjen/engineer。

2008年2月12日 星期二

oracle -資料相疊處理計算法

運用在同一個資料表中, 這一筆減去上一筆的時間差,
或兩筆資料間的資料處理

select a.v_order,
to_char(a.time_log,'YYYY/MM/DD HH24:MI') start_time,
b.v_order,
to_char(b.time_log,'YYYY/MM/DD HH24:MI') stop_time,

from ( select rownum v_order,
t1
from test1 ) a,
( select rownum v_order,
t2
from test1 ) b

where a.v_order=b.v_order-1

2008年1月3日 星期四

3COM L3 switch 說明文件

1. L3 指的是OSI 七層中的第三層, 具有路由功能的SWITCH
2. #telnet 192.168.0.254 用Telnet 連進SWITCH
3. #system-view 進入config 模式
4. #display current-configuration 列出目前switch config 狀態
5. #Vlan n 建立一個VLAN , 如vlan 1
6. #undo vlan n 刪除一個VLAN, 如UNDO VLAN 1
7. #Interface vlan-interface 15 設定VLAN 15
8. #Ip address 192.168.1.1 255.255.255.0 設定VLAN 15的IP
9. #? 顯示可以用的指令
10. 一個網路PORT對應一個VLAN
11. 兩台L3 SWITCH可以做TRUNK
12. Trunk 設定方法
每台L3 SWITCH如果可以做TRUNK, 會有專門的網路PORT
設定方法如下
#Interface GigabitEthernet1/0/16
#port link-type trunk
#port trunk port vlan all
13. 設default route
#Ip route-static 0.0.0.0 0.0.0.0 192.168.1.254 perference 60
Default rotue 可以指定給對外路由器或防火牆