jsp生成xml文件示例

<%@ 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>

 
posted @ 2007-09-21 20:55  hibernate3例子  阅读(156)  评论(0编辑  收藏  举报