JSP從資料庫中匯出資料並輸出產生XML檔案

來源:互聯網
上載者:User

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>
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.