jdom创建xml文件

package com.cjq.xml;

import java.io.FileOutputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.Statement;

import org.jdom2.Document;
import org.jdom2.Element;
import org.jdom2.output.XMLOutputter;


public class DBtoXml {

	public static void main(String[] args) throws Exception{
		Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
		Connection conn = DriverManager.getConnection("jdbc:odbc:book");
		Statement stmt = conn.createStatement();
		ResultSet rs = stmt.executeQuery("select * from customer");
		ResultSetMetaData rsmd = rs.getMetaData();
		int numcols = rsmd.getColumnCount();
		
		org.jdom2.Element root,ele,name;
		root = new Element("customer");
		while(rs.next()){
			ele = new Element("第"+rs.getRow()+"条纪录");
			for(int i=1;i<numcols;i++){
				name = new Element("a"+rsmd.getColumnLabel(i));
				name.setContent(new Element("a"+rs.getString(i)));
				ele.addContent(name);
			}
			root.addContent(ele);
		}
		Document doc = new Document(root);
		FileOutputStream f = new FileOutputStream("db.xml");
		XMLOutputter out = new XMLOutputter();
		out.output(root, f);
		rs.close();
		conn.close();
	}

}

你可能感兴趣的:(jdom创建xml文件)