本程序用到两个jar包,一个是链接Oracle数据库的jar包,另一个是dom4j的jar包。
程序源代码如下:
package com.swanson.myjdbc;
import java.io.File;
import java.io.FileWriter;
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.XMLWriter;
import oracle.jdbc.driver.OracleTypes;
public class CreateXML {
/**
* 从数据库中导出表emp,生成emp.xml文件
*/
public void startCreate(File file) {
//链接数据库,获取结果集,此处我调用了一个存储过程,当然也可以直接获得一个结果集
Connection con = null;
CallableStatement cst = null;
ResultSet rs = null;
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
String url = "jdbc:oracle:thin:@localhost:1521:ORCL";
String user = "scott";
String password = "tiger";
con = DriverManager.getConnection(url, user, password);
String sql = "{call my_proc02(?)}";
cst = con.prepareCall(sql);
cst.registerOutParameter(1, OracleTypes.CURSOR);
cst.execute();
rs = (ResultSet) cst.getObject(1);
//获取结果集的元数据
ResultSetMetaData rsmd = rs.getMetaData();
//调用创建XML文件方法
createXmlFile(rs, rsmd, file);
// while(rs.next()){
// System.out.println(rs.getString(2));
// }
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (rs != null) {
rs.close();
}
if (cst != null) {
cst.close();
}
if (con != null) {
cst.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
public void createXmlFile(ResultSet rs, ResultSetMetaData rsmd, File file) {
XMLWriter xmlWriter = null;
String elementValue = "";
Document doc = DocumentHelper.createDocument();
Element rootElement = doc.addElement("ROWDATA");
//rootElement.addComment("创建员工表的xml文件!");
try {
while (rs.next()) {
Element rowElement = rootElement.addElement("ROW");
for (int i = 1; i <= rsmd.getColumnCount(); i++) {
Element elementName = rowElement.addElement(rsmd
.getColumnName(i));
// 获取每一列的值
// System.out.println(rsmd.getColumnCount());
Object obj = rs.getObject(rsmd.getColumnName(i));
//对于元数据为null的数据进行过滤
if (obj != null) {
elementValue = rs.getObject(rsmd.getColumnName(i))
.toString().trim();
} else {
elementValue = "";
}
// System.out.println(elementValue);
elementName.setText(elementValue);
}
}
xmlWriter = new XMLWriter(new FileWriter(file));
xmlWriter.write(doc);
System.out.println("数据库导入xml完成,文件emp.xml已经生成!");
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (xmlWriter != null) {
xmlWriter.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) {
File file = new File("d:\\emp.xml");
CreateXML cx = new CreateXML();
cx.startCreate(file);
}
}
程序源代码如下:
package com.swanson.myjdbc;
import java.io.File;
import java.io.FileWriter;
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.XMLWriter;
import oracle.jdbc.driver.OracleTypes;
public class CreateXML {
/**
* 从数据库中导出表emp,生成emp.xml文件
*/
public void startCreate(File file) {
//链接数据库,获取结果集,此处我调用了一个存储过程,当然也可以直接获得一个结果集
Connection con = null;
CallableStatement cst = null;
ResultSet rs = null;
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
String url = "jdbc:oracle:thin:@localhost:1521:ORCL";
String user = "scott";
String password = "tiger";
con = DriverManager.getConnection(url, user, password);
String sql = "{call my_proc02(?)}";
cst = con.prepareCall(sql);
cst.registerOutParameter(1, OracleTypes.CURSOR);
cst.execute();
rs = (ResultSet) cst.getObject(1);
//获取结果集的元数据
ResultSetMetaData rsmd = rs.getMetaData();
//调用创建XML文件方法
createXmlFile(rs, rsmd, file);
// while(rs.next()){
// System.out.println(rs.getString(2));
// }
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (rs != null) {
rs.close();
}
if (cst != null) {
cst.close();
}
if (con != null) {
cst.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
public void createXmlFile(ResultSet rs, ResultSetMetaData rsmd, File file) {
XMLWriter xmlWriter = null;
String elementValue = "";
Document doc = DocumentHelper.createDocument();
Element rootElement = doc.addElement("ROWDATA");
//rootElement.addComment("创建员工表的xml文件!");
try {
while (rs.next()) {
Element rowElement = rootElement.addElement("ROW");
for (int i = 1; i <= rsmd.getColumnCount(); i++) {
Element elementName = rowElement.addElement(rsmd
.getColumnName(i));
// 获取每一列的值
// System.out.println(rsmd.getColumnCount());
Object obj = rs.getObject(rsmd.getColumnName(i));
//对于元数据为null的数据进行过滤
if (obj != null) {
elementValue = rs.getObject(rsmd.getColumnName(i))
.toString().trim();
} else {
elementValue = "";
}
// System.out.println(elementValue);
elementName.setText(elementValue);
}
}
xmlWriter = new XMLWriter(new FileWriter(file));
xmlWriter.write(doc);
System.out.println("数据库导入xml完成,文件emp.xml已经生成!");
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (xmlWriter != null) {
xmlWriter.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) {
File file = new File("d:\\emp.xml");
CreateXML cx = new CreateXML();
cx.startCreate(file);
}
}