Implementation of JSP using Jdom processing database to XML transformation (II.)

Source: Internet
Author: User
Tags date format odbc query stmt xsl xsl file
dom|js|xml| Data | database | conversion

Company

Company

varchar (30)

Not NULL

Email

TEL

varchar (15)

Not NULL

Enter the following several records into the database:

Name

Id

Company

Email

Liu Shi

001

Company 1

Liu@yantai.com

Jiang Shi

002

Company 2

Jiang@yantai.net

2. Data source Settings

The data source (ODBC source) is actually the source that defines the data. The data source is set up by: [Start]-〉[Settings]-〉[Control Panel]-〉[Admin tool]-〉[Data Source (ODBC)]-〉[system DSN]->[ADD]->[SQL Server], configure the server name (custom server) , database name (custom), data source name (set here as Db_custom), user name (lgz), user password (empty), data source is configured.

3, the Code writing

Let's start by writing JSP code transformation, which will dynamically generate XML structures by invoking Jdom and then using JDBC to access the SQL Server database to populate the XML content dynamically.

<% @page contenttype= "TEXT/HTML;CHARSET=GBK"%>

<HTML>

<HEAD>

<TITLE> Conversion of database to XML using Jdom </TITLE>

</HEAD>

<BODY>

<% @page import= "org.jdom.*"%>

<% @page import= "Java.*"%>

<%

Class.forName ("Com.microsoft.jdbc.sqlserver.SQLServerDriver"). newinstance ();

String url= "Jdbc:microsoft:sqlserver://10.40.14.54:1433;databasename=db_custom";

Drivers for loading JDBC ODBC

String user= "Lgz";

String password= "Lgz";

Connection conn= drivermanager.getconnection (Url,user,password); Connecting to a database

Statement stmt=conn.createstatement (resultset.type_scroll_sensitive,resultset.concur_updatable);

Create a statement

String sql= "SELECT * from client"; To define the SQL statement for a query

ResultSet rs=stmt.executequery (SQL); Execute Query

Document Document=new Documents (new Element ("Contact list");//Create Documentation

ResultSetMetaData RSMD = Rs.getmetadata (); Get field Name

int numberofcolumns = Rsmd.getcolumncount (); Get number of fields

int i=0;

while (Rs.next ()) {//Remove query Results

Element element0=new Element ("contact"); Creating an element to generate the Jdom tree

Document.getrootelement (). Addcontent (ELEMENT0);

for (I=1; i<=numberofcolumns;i++)

{string Date=new string (rs.getstring (i). GetBytes ("Iso-8859-1"), "gb2312");//Code conversion

Element element=new Element (Rsmd.getcolumnname (i)). SetText (date);

Element0.addcontent (Element);

}

}

Rs.close (); Close result set

Stmt.close (); Close statement

Conn.close (); Close connection

Xmloutputter OUTP = new Xmloutputter ();

Outp.output (document, New FileOutputStream ("D:\\data.xml")); Output XML document

Out.print ("XML document Generation complete!") ");

%>

<a href= "File:///d|/data.xml" > click to open the resulting XML document </a>

</BODY>

</HTML>

4, the operation of the program

Running the JSP through the browser's 8080 port will produce the expected XML file.

V. Application analysis

As the standard of data exchange, XML has been used more and more widely. This article illustrates the basic methods of database conversion to XML, which makes it possible to exchange data between heterogeneous platforms and various formats. When we can convert raw data into XML format, we can express it in rich HTML format. Here are two examples of applications:

1. XML Application Server:

XML application servers are actually Web application servers that support XML, which are typically template-driven, using SQL statements to extract data and dynamically build XML documents in a script language.

2, based on XML desktop applications

We know that through XSL we can submit the same data to end users in different data formats, an XSL file describes how the data is displayed, and you can connect many XSL and the same XML document to provide different html-based representations, so in fact, We can build XML based desktop applications

There are two main advantages to this approach, first of all, you can operate the data in a platform and language independent way, and secondly, you can implement different view representations of the same data without the need for programming.




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.