<%@ page contentType="text/html; charset=gb2312" %><%@ page language="java"%><%@ page info="database handler"%><%@ page import="java.io.*"%><%@ page import="java.lang.*"%><%@ page import="java.util.*"%><%@ page import="java.sql.*"%><%@ page import="javax.servlet.*"%><%@ page import="javax.servlet.http.*"%><html><head><meta content="text/html; charset=gb2312" http-equiv="content-type"></head><body><% int i=0; String temp=null; String parCode = request.getParameter("code"); String strSql; try{ Connection con; Statement stmt; ResultSet rs; Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); String url="jdbc:oracle:thin:@192.168.15.248:1521:ycdb"; con=DriverManager.getConnection(url,"webcfcquery","kill859"); //out.println("<center>jdbc串連Oracle資料庫</center><br><br>"); stmt=con.createStatement(); strSql = "select account_holder,description from account_holder "; if(parCode!=null) strSql += " where account_holder='"+ parCode +"'"; rs=stmt.executeQuery(strSql); FileOutputStream fo=new FileOutputStream("/usr/local/tomcat/webapps/vehicles/treasurer_voucher.xml"); PrintStream so = new PrintStream(fo);while(rs.next()){ String col1=rs.getString(1); String col2=rs.getString(2); try{if(i==0){ so.println("<?xml version=/"1.0/" encoding=/"gb2312/" ?>");so.println("<ufinterface roottag=/"voucher/" billtype=/"gl/" subtype=/"/" replace=/"Y/" receiver=/"1051/" sender=/"1101/" isexchange=/"Y/" filename=/"會計憑證.xml/">");so.println("<voucher id=/"1234560102/">");// 注意:這種寫法在xml檔案顯示時會報錯,不知道為什嗎? so.println("<custcode>"+col1+"</custcode>");so.print("<custcode>");if(col2==null) {//如果不判斷對象col2是否為null值,那麼在進行編碼轉換時可能會出現NullPointerException異常so.print(col2);} else {//因為前面已經定義xml檔案的編碼顯示方式是gb2312,所以在將字串col2寫入xml之前需轉換成gb2312,這樣在xml檔案中漢字才能正常顯示。byte[] temp_col = col2.getBytes("gb2312");col2 = new String(temp_col);so.print(col2);}so.println("</custcode>");} }catch(Exception e){out.println(e.toString());}i++;}so.println("</voucher>");so.println("</ufinterface>");so.close(); }catch(Exception e){ out.println(e); }%></body></html>