JSP從資料庫教程中匯出資料並輸出產生XML檔案
/*
資料庫結構
create table userTable01(
user_id int(4) NOT NULL primary key auto_increment,
user_name varchar(40),
user_password varchar(40),
user_true_name varchar(40),
user_age int, user_sex varchar(2),
user_address varchar(80),
user_telphone varchar(20),
add_time datetime
);
insert into usertable01 (
user_name,
user_password,
user_true_name,
user_age,
user_sex,
user_address,
user_telphone,
add_time)
values ('wgy','123123','gavin.wang','23','M','sdfg','321321',now());
//xml文檔結構
<?xml version="1.0" encoding="gb2312" ?>
- <users>
- <user>
<id>1</id>
<name>wgy</name>
<password>123123</password>
<true_name>www.111cn.net</true_name>
<age>23</age>
<sex>M</sex>
<address>sdfg</address>
<telphone>321321</telphone>
<add_time>2010-01-06</add_time>
</user>
</users>
*/
<%@ page contentType = "text/xml;charset =gb2312" %>
<%@ page import = "java.sql.*" %>
<?xml version = "1.0" encoding = "gb2312" ?>
<users>
<%
Connection con = null;
Statement sql = null;
ResultSet rs = null;
try
{
String url = "jdbc:mysql教程://localhost:3306/test"; ;
String username = "root";
String password = "password";
Class.forName("com.mysql.jdbc.Driver");
con = DriverManager.getConnection(url, username, password );
sql = con.createStatement();
rs = sql.executeQuery("select * from userTable01");
while(rs.next())
{
%>
<user>
<id><%=rs.getLong("user_id") %></id>
<name><%=rs.getString("user_name") %></name>
<password><%=rs.getString("user_password") %></password>
<true_name><%=rs.getString("user_true_name") %></true_name>
<age><%=rs.getInt("user_age") %></age>
<sex><%=rs.getString("user_sex") %></sex>
<address><%=rs.getString("user_address") %></address>
<telphone><%=rs.getString("user_telphone") %></telphone>
<add_time><%=rs.getDate("add_time") %></add_time>
</user>
<% }
}catch(Exception e)
{
out.print("程式出現異常"+" "+e);
//out.print(e);
}
%>
</users>