java数据库导出xml,⽤java将数据库⾥的数据⽣成
xml(amchart⽤)
(mysql5.0)sql脚本
CREATE TABLE `hostdiskinfo` (
`hdid` bigint(20) NOT NULL auto_increment,
`begintime` datetime default '0000-00-00 00:00:00',
`hdiskinfo` longtext,
`hostid` bigint(20) default '0',
PRIMARY KEY (`hdid`)
) ;
这是查询结果:
mysql> select hdiskinfo,begintime from hostdiskinfo where hostid=11
+-------------------------+---------------------+
| hdiskinfo | begintime |
+-------------------------+---------------------+
| C:,66.4;D:,82.8;E:,80.9 | 2009-02-06 15:15:03 |
| C:,66.4;D:,82.8;E:,80.9 | 2009-02-06 15:23:45 |
| C:,66.1;D:,82.8;E:,80.9 | 2009-02-06 15:30:02 |
| C:,64.4;D:,82.8;E:,83.8 | 2009-02-09 09:37:20 |
| C:,64.4;D:,82.8;E:,83.8 | 2009-02-09 09:39:41 |java xml是什么
| C:,64.4;D:,82.8;E:,83.8 | 2009-02-09 09:48:23 |
| C:,64.4;D:,82.8;E:,83.8 | 2009-02-09 09:51:14 |
+-------------------------+---------------------+
7 rows in set (0.00 sec)
现在的要求是要将以上的查询结果以下⾯形式写⼊到xml⽂件⾥⾯:
02-06 15:15
02-06 15:23
02-06 15:30
02-09 09:37
02-09 09:39
02-09 09:48
02-09 09:51
66.4
66.4
66.1
64.4
64.4
64.4
64.4
82.8
82.8
82.8
82.8
82.8
82.8
82.8
80.9
80.9
80.9
83.8
83.8
83.8
83.8
下⾯是我之前的实现⽅法:
ilu.util.amchart;
import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement; import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
ilu.util.db.DBConnection; import org.jdom.Document;
import org.jdom.Element;
import org.jdom.output.Format;
import org.jdom.output.XMLOutputter;
public class AmchartDemo02 {
/
**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
List all = getData();
System.out.println(all.size());
createXML(all);
}
public static List getData() {
Connection conn = Connection("conf.properties");
List all = new ArrayList();
ResultSet rs = null;
Hostdiskinfo hd = null;
PreparedStatement pstmt = null;
String sql = "select hdiskinfo,begintime from hostdiskinfo where hostid=11"; try {
pstmt = conn.prepareStatement(sql);
rs = uteQuery();
while (rs.next()) {
hd = new Hostdiskinfo();
hd.String(1));
hd.Timestamp(2));
System.out.Begintime());
all.add(hd);
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
try {
rs.close();
pstmt.close();
conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return all;
}
public static boolean createXML(List all) {
Document doc = new Document();
Element chart, series, value, graphs, ce, de, ee, fe, ge, he = null; ce = new Element("graph");
de = new Element("graph");
ee = new Element("graph");
fe = new Element("graph");
ge = new Element("graph");
he = new Element("graph");
chart = new Element("chart");
series = new Element("series");
graphs = new Element("graphs");
for (int i = 0; i < all.size(); i++) {
value = new Element("value");
value.setAttribute("xid", i + "");
Hostdiskinfo hd = (Hostdiskinfo) (i);
value.Begintime().toString().substring(5, 16)); series.addContent(value);
}
chart.addContent(series);
Hostdiskinfo hd2 = (Hostdiskinfo) (0);
String st = Hdiskinfo();
if (st.contains("C:")) {
ce.setAttribute("gid", "1");
ce.setAttribute("title", "C:");
}
if (st.contains("D:")) {
de.setAttribute("gid", "2");
de.setAttribute("title", "D:");
}
if (st.contains("E:")) {
ee.setAttribute("gid", "3");
ee.setAttribute("title", "E:");
}
if (st.contains("F:")) {
fe.setAttribute("gid", "4");
fe.setAttribute("title", "F:");
}
if (st.contains("G:")) {
ge.setAttribute("gid", "5");
ge.setAttribute("title", "G:");
}
if (st.contains("H:")) {
he.setAttribute("gid", "6");
he.setAttribute("title", "H:");
}
for (int a = 0; a < all.size(); a++) { Hostdiskinfo hd3 = (Hostdiskinfo) (a); String[] st1 = Hdiskinfo().split(";"); for (int b = 0; b < st1.length; b++) {
String[] st2 = st1[b].split(",");
value = new Element("value");
value.setAttribute("xid", a + "");
value.setText(st2[1]);
if (st2[0] == "C:" || st2[0].equals("C:")) { ce.addContent(value);
}
if (st2[0] == "D:" || st2[0].equals("D:")) {
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论