将数据库中数据写到uer。xml

package com.xml.xmldbwrite;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.Transformer;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Text;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.util.Vector;

public class Business {
  public Business() {
  }
  public static void convertToXml(ResultSet rs,String xmlFileName)
  {
    try
    {
      ResultSetMetaData rsmd = rs.getMetaData();    //获取结果集媒体数据--结果集的列信息
      Vector<String> columns=new Vector<String>();
      for(int i=1;i<=rsmd.getColumnCount();i++)
      {
        columns.addElement(rsmd.getColumnName(i));    //把列信息封装到集合变量中
      }
      DocumentBuilderFactory dfactory=DocumentBuilderFactory.newInstance();    //创建文档生成器工厂
      DocumentBuilder dbuilder=dfactory.newDocumentBuilder();      //创建文档生成器
      Document doc=dbuilder.newDocument();     //通过文档生成器创建Document
      Element root=doc.createElement("datatable");   //创建一个根元素,元素名称是datatable
      doc.appendChild(root);   //把根元素添加到文档元素的字节点
      while(rs.next())
      {
        Element datarow=doc.createElement("datarow");    //创建行元素,元素名称是datarow
        root.appendChild(datarow);        //把行元素添加为datatable的字节点
        for(int i=0;i<columns.size();i++)   //在当前行中遍历列集合
        {
          Element datacolumn=doc.createElement(columns.get(i).toString());    //创建列元素,元素名为对应列的名称
          datarow.appendChild(datacolumn);    //把列元素添加为当前行元素的字节点
          String temp=rs.getString(i+1);      //获取当前行相应列的字符串值
          temp=temp==null?"":temp;          //转换空值
          Text t=doc.createTextNode(temp);   //把当前列值创建为文本节点
          datacolumn.appendChild(t);       //把文本节点添加为当前列节点的字节点
        }
      }
      DOMSource source=new DOMSource(doc);   //封装DOM源
      StreamResult result=new StreamResult(xmlFileName);   //封装文件流结果
      TransformerFactory tfactory=TransformerFactory.newInstance();   //创建转换器工厂
      Transformer former=tfactory.newTransformer();   //创建转换器
      former.transform(source,result);   //把源中的数据转换为相应的流结果!
      System.out.println("complete!");
    }
    catch(Exception ex)
    {
      ex.printStackTrace();
    }
  }
}
//*******************************************

package com.xml.xmldbwrite;
import com.xml.xmldbwrite.Business;
import com.xml.xmldbwrite.Sql;

import java.sql.*;

public class Test {
  public Test() {
  }
  static Connection con = null;
  public static void main(String[] args) {
   
   try
   {
    Class.forName(Sql.DIRVER);
  con = DriverManager.getConnection(Sql.URL,Sql.USERNAME,Sql.PASSWORD);
     Statement stmt=con.createStatement();
     ResultSet rs=stmt.executeQuery("select * from user");
     Business.convertToXml(rs,"user.xml");
   }
   catch(Exception ex)
   {
     ex.printStackTrace();
   }
  }
}
//**********************************************

package com.xml.xmldbwrite;

public interface Sql {

 public static final String DIRVER="com.mysql.jdbc.Driver";
 public static final String URL = "jdbc:mysql://localhost:3306/testdb?useUnicode=true&characterEncoding=GBK";
 public static final String USERNAME = "root";
 public static final String PASSWORD = "321";
 
 public static final String INSERT_SCORE = "insert into score(class,results,stu_id) values(?,?,?)";
 
 public static final String STUDENT_LOGIN = "select name,password from student where name = ? and password = ?";
 
 public static final String FIND_ALLSTUDENT = "select * from student";
}

你可能感兴趣的:(sql,mysql,xml,jdbc)