forked from wujun728/jun_java_plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDatabaseToXML.java
More file actions
65 lines (48 loc) · 1.53 KB
/
DatabaseToXML.java
File metadata and controls
65 lines (48 loc) · 1.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.XMLWriter;
public class DatabaseToXML {
public static void toXML(){
Connection connection=null;
PreparedStatement statement=null;
ResultSet rs=null;
connection=JDBCUtilSingle.getInitJDBCUtil().getConnection();
String sql="SELECT * FROM `dict`";
Document document = DocumentHelper.createDocument();
Element root = document.addElement("dict");// 创建根节点
try {
statement=connection.prepareStatement(sql);
rs=statement.executeQuery();
while(rs.next()){
Element word = root.addElement("word");
Element name = word.addElement("name");
name.setText(rs.getString("word"));
Element mean=word.addElement("mean");
mean.addCDATA(rs.getString("meaning"));
Element lx=word.addElement("lx");
lx.addCDATA(rs.getString("lx"));
}
XMLWriter writer=new XMLWriter(new FileWriter(new File("dict2.xml")));
writer.write(document);
writer.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
toXML();
}
}