Java从数据库读取表头,Java从数据库中读取数据到xml文件

操做xml须要的jar包:crimson.jarjava

package myXmlep1;mysql

import org.w3c.dom.Document;

import org.w3c.dom.Element;

import org.apache.crimson.tree.XmlDocument;

import javax.xml.parsers.DocumentBuilderFactory;

import javax.xml.parsers.DocumentBuilder;

import javax.xml.parsers.ParserConfigurationException;

import java.sql.Connection;

import java.sql.DriverManager;

import java.sql.Statement;

import java.sql.ResultSet;

import java.sql.SQLException;

import java.io.File;

import java.io.FileOutputStream;

import java.io.IOException;

import java.io.FileNotFoundException;

import java.io.IOException;

import org.xml.sax.SAXException;sql

public class test

{

static Connection conn=null;

static String sql;

//static String url="jdbc:oracle:oci8:@hydb";

static String url="jdbc:mysql://localhost:3306/test";

public static void main(String[] args)

{

try

{   //连接数据库,取得数据

//Class.forName("oracle.jdbc.driver.OracleDriver");//oracle数据库

Class.forName("com.mysql.jdbc.Driver");//mysql数据库数据库

conn=DriverManager.getConnection(url,"root","");

Statement st=conn.createStatement();

ResultSet rs=st.executeQuery("select * from users");

DocumentBuilderFactory factory=DocumentBuilderFactory.newInstance();

DocumentBuilder builder=factory.newDocumentBuilder();

Document doc=builder.newDocument();

Element comp=doc.createElement_x("COMP");

while(rs.next())

{

Element person=doc.createElement_x("PERSON");

Element deptno=doc.createElement_x("DEPTNO");

deptno.appendChild(doc.createTextNode(String.valueOf(rs.getInt(1))));

person.appendChild(deptno);

Element dname=doc.createElement_x("DNAME");

dname.appendChild(doc.createTextNode(new test().xmlReplace(rs.getString(2))));

person.appendChild(dname);

Element loc=doc.createElement_x("LOC");

loc.appendChild(doc.createTextNode(new test().xmlReplace(rs.getString(3))));

person.appendChild(loc);

comp.appendChild(person);

}

rs.close();

st.close();

conn.close();

doc.appendChild(comp);

((XmlDocument)doc).write(new FileOutputStream("src/dept.xml"));

System.out.println("操做成功!!!");

}catch(ClassNotFoundException e)

{

e.printStackTrace();

}catch(SQLException e1)

{

e1.printStackTrace();

}catch(ParserConfigurationException e2)

{

e2.printStackTrace();

}catch(FileNotFoundException e3)

{

e3.printStackTrace();

}catch(IOException e4)

{

e4.printStackTrace();

}

}

public String xmlReplace(String value){

System.out.println("value---old"+value);

//value=value.replace("&", "&");

//   value=value.replace("

//   value=value.replace(">", ">");

//   value=value.replace("'", "'");

value=value.replace( "

value=value.replace( ">",">");

value=value.replace( "'","'");

// value.replace("""", """);

System.out.println("value---new"+value);

return value;

}

}apache

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值