输入sql语句,将结果写入到xml文件
import java.io.FileOutputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement; import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult; import org.w3c.dom.Document;
import org.w3c.dom.Element; /*
* @ author Roger
* @date 2016/5/3
* @description 输入sql语句,将结果写入到xml文件。文件格式按数据库的字段名,字段值格式。
* */
public class pXML {
public static void main(String args[]) throws Exception{
Connection conn = null;
String sql;
String url = "jdbc:mysql://localhost:3306/test?"
+ "user=root&password=root&useUnicode=true&characterEncoding=UTF8";
try{
Class.forName("com.mysql.jdbc.Driver");
System.out.println("成功加载MySQL驱动程序");
conn = DriverManager.getConnection(url);
Statement stmt = conn.createStatement();
sql = "select * from people";
ResultSet rs = stmt.executeQuery(sql);
System.out.println("查询人员资料:"); DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
Document doc = builder.newDocument();
Element root = doc.createElement("Info");
doc.appendChild(root); while(rs.next()){
Element people = doc.createElement("People");
System.out.println(rs.getString(1) + "\t" + rs.getString(2) + "\t" + rs.getString(3) + "\t" + rs.getString(4));
Element no = doc.createElement("no");
no.appendChild(doc.createTextNode(rs.getString(1)));
people.appendChild(no);
Element name = doc.createElement("name");
name.appendChild(doc.createTextNode(rs.getString(2)));
people.appendChild(name);
Element sex = doc.createElement("sex");
sex.appendChild(doc.createTextNode(rs.getString(3)));
people.appendChild(sex);
Element age = doc.createElement("age");
age.appendChild(doc.createTextNode(rs.getString(4)));
people.appendChild(age);
root.appendChild(people);
}
TransformerFactory tf = TransformerFactory.newInstance();
try{
Transformer t = tf.newTransformer();
t.setOutputProperty(OutputKeys.INDENT,"yes");
t.setOutputProperty(OutputKeys.METHOD,"xml");
t.setOutputProperty("{http://xml.apache.org/xslt}indent-amount","2");
t.transform(new DOMSource(doc),new StreamResult(new FileOutputStream("d:\\sql.xml"))); //执行上面的设置并且输出到文件中
System.out.println("生成XML文件成功!");
}catch(Exception e){
e.printStackTrace();
}
}catch(SQLException e){
System.out.println("MySql操作错误");
e.printStackTrace();
}catch(Exception e){
e.printStackTrace();
}finally{
conn.close();
}
}
}
输入sql语句,将结果写入到xml文件的更多相关文章
- PHP如何通过SQL语句将数据写入MySQL数据库呢?
1,php和MySQL建立连接关系 2,打开 3,接受页面数据,PHP录入到指定的表中 1.2两步可直接使用一个数据库链接文件即可:conn.php <?phpmysql_connect(&qu ...
- cmd连接Oracle数据库成功后输入sql语句返回 2
解决办法 : sql语句后一定要跟分号 .
- PL/SQL Developer中输入SQL语句时如何自动提示字段
在PL/SQL Developer中编写sql语句时,如果无法自动提示字段那是一件痛苦的事情,工作效率又低,在此演示下如何在PL/SQL Developer工具中自动提示字段,让开发者省时又省心,操作 ...
- 微软BI 之SSIS 系列 - 两种将 SQL Server 数据库数据输出成 XML 文件的方法
开篇介绍 在 SSIS 中并没有直接提供从数据源到 XML 的转换输出,Destination 的输出对象有 Excel File, Flat File, Database 等,但是并没有直接提供 X ...
- 用SQL语句建库建表建约束(用SQl语句在指定盘符创建文件夹)
一 :创建数据库 创建一个数据文件和一个日志文件(MySchool) create database MySchoolon primary --默认属于primary主文件组,可省略(--数 ...
- 7.数据本地化CCString,CCArray,CCDictionary,tinyxml2,写入UserDefault.xml文件,操作xml,解析xml
数据本地化 A CCUserDefault 系统会在默认路径cocos2d-x-2.2.3\projects\Hello\proj.win32\Debug.win32下生成一个名为UserDef ...
- SQL Server 2008 r2 输入SQL语句不能自动提示的解决办法
先利用“配置工具-SQL Server 配置管理器”关闭所有MSSQLSERVER服务,利用SQL Server Installation Center,进入Maintenance,选择Repair, ...
- SQL Server中使用SQL语句关闭数据库连接和删除数据库文件
有时候我们想用DROP DATABASE语句删除数据库和数据库文件,会删不掉,因为有其他人正在使用要删除的数据库,这里有一个方法可以强制断开其它数据库连接,再删除数据库. 假如我们要删除的数据库是[T ...
- MyBatis3-topic-01 -安装/下载/官方文档 -执行输入一条已经映射的sql语句
mybatis XML 映射配置文件 (官方文档) -对象工厂(objectFactory) -配置环境(environments) -映射器(mappers) 本地IDEA搭建/测试步骤 创建数据库 ...
随机推荐
- Html5_sessionStrong和localStorage的灵活使用
谈谈这两个属性sessionStrong和localStorage是Html5新增点属性,用来记录一些数据在浏览器. 两者的区别sessionStrong存储的数据是暂时的,浏览器关掉后,存储下来的数 ...
- poj 2253 Frogger (dijkstra最短路)
题目链接:http://poj.org/problem?id=2253 Frogger Time Limit: 1000MS Memory Limit: 65536K Total Submissi ...
- poj 3751 时间日期格式转换
题目链接:http://poj.org/problem?id=3751 题目大意:按照要求的格式将输入的时间日期进行转化. #include <iostream> #include < ...
- 【DataScience学习笔记】Coursera课程《数据科学家的工具箱》 约翰霍普金斯大学——Week3 Conceptual Issues课堂笔记
Coursera课程<数据科学家的工具箱> 约翰霍普金斯大学 Week3 Conceptual Issues Types of Questions Types of Data Scienc ...
- python实战===2017年30个惊艳的Python开源项目 (转)
本文转自:http://www.sohu.com/a/216723120_115128 摘要:本文来自Mybridge,介绍了过去一年里30个惊艳的Python开源项目.点击每一个都可以在GitHub ...
- Deep Learning基础--参数优化方法
1. 深度学习流程简介 1)一次性设置(One time setup) -激活函数(Activation functions) - 数据预处理(Data Preprocessing) ...
- JS页面之间传值
父页面与子页面之间有多种传值的方式: 第一种,通过window.open的方法打开一个新的页面,在新的页面里面通过window.opener来获取对象,以下为实例 父页面: function open ...
- 196. Delete Duplicate Emails
Write a SQL query to delete all duplicate email entries in a table named Person, keeping only unique ...
- 通过IP地址和子网掩码计算主机数
知道ip地址和子网掩码后可以算出: 1. 网络地址 2. 广播地址 3. 地址范围 4. 本网有几台主机 例1:下面例子IP地址为192·168·100·5 子网掩码是255·255·255·0.算出 ...
- 导出数据到word
打野的时候,碰到一个需求,导出简历信息. 两条思路: 第一条,直接画所有的表格,填充数据. 第二条,加载一个空的模板,然后填充数据. 因为导出的有格式的,所以最后选择了使用模板进行替换,然后填充数据. ...