<%@page contentType="text/html;charset=GBK"%> <HTML> <HEAD> <TITLE>用JDOM實(shí)現(xiàn)數(shù)據(jù)庫到XML的轉(zhuǎn)換</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"; //載入jdbc odbc的驅(qū)動(dòng)程序 String user="lgz"; String password="lgz"; Connection conn= DriverManager.getConnection(url,user,password); //連接數(shù)據(jù)庫 Statement stmt=conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE); //創(chuàng)建一個(gè)statement String sql="select * from client"; //定義查詢的SQL語句 ResultSet rs=stmt.executeQuery(sql); //執(zhí)行查詢 Document document=new Document(new Element("聯(lián)系人列表"));//創(chuàng)建文檔 ResultSetMetaData rsmd = rs.getMetaData(); //獲取字段名 int numberOfColumns = rsmd.getColumnCount(); //獲取字段數(shù) int i=0; while(rs.next()){ //將查詢結(jié)果取出 Element element0=new Element("聯(lián)系人"); //創(chuàng)建元素 生成JDOM樹 document.getRootElement().addContent(element0); for (i=1; i<=numberOfColumns;i++) { String date=new String(rs.getString(i).getBytes("ISO-8859-1"),"gb2312"); //代碼轉(zhuǎn)換 Element element=new Element(rsmd.getColumnName(i)).setText(date); element0.addContent(element); } } rs.close(); //關(guān)閉結(jié)果集 stmt.close(); //關(guān)閉statement conn.close(); //關(guān)閉連接 XMLOutputter outp = new XMLOutputter(); outp.output(document, new FileOutputStream("d:\\data.xml")); //輸出XML文檔 out.print("XML 文檔生成完畢!"); %> <a href="file:///d /data.xml">點(diǎn)擊打開產(chǎn)生的XML文檔 </a> </BODY> </HTML> |