Example of JSP generation of XML file-JSP tutorial, JSP/Servlet

Source: Internet
Author: User
<% @ 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 connection to the Oracle database </center> <br> ");
Stmt = con. createstatement ();
Strsql = "select account_holder, description from account_holder ";
If (parcode! = NULL)
Strsql + = "where account_holder =" + parcode + "";
Rs1_stmt.exe cutequery (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 =/"Accounting credential. XML/"> ");
So. println ("<voucher id =/" 1234560102/"> ");

// Note: An error is reported when the XML file is displayed. Why? So. println ("<custcode>" + col1 + "</custcode> ");
So. Print ("<custcode> ");
If (col2 = NULL) {// if you do not judge whether the object col2 is null, an nullpointerexception exception may occur during encoding conversion.
So. Print (col2 );
} Else {// because the encoding display mode of the XML file has been defined earlier is gb2312, You need to convert the string col2 to gb2312 before writing it into XML, in this way, Chinese characters can be normally displayed in the XML file.
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>


Article arrangement:Webmaster skyURL: http://www.z6688.com/

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.