forked from wujun728/jun_java_plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathXMLToDatabaseWithDom.java
More file actions
107 lines (89 loc) · 3.03 KB
/
XMLToDatabaseWithDom.java
File metadata and controls
107 lines (89 loc) · 3.03 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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
import java.io.File;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
/**
* 讲XML文件导入到数据库
* @author licheng
*
*/
public class XMLToDatabaseWithDom {
/**
* 讲 学生XML文件导入到数据库中
*/
public void toDatabase(){
Connection connection=null;
PreparedStatement statement=null;
ResultSet rs=null;
connection=JDBCUtilSingle.getInitJDBCUtil().getConnection();
String sql="INSERT INTO `xmlanddb`.`student` (`number`, `name`, `date`, `height`) VALUES (?,?,?,?);";
try {
statement=connection.prepareStatement(sql);
DocumentBuilderFactory factory=DocumentBuilderFactory.newInstance();
factory.setIgnoringElementContentWhitespace(true); //忽略空白缩进
DocumentBuilder domParser=factory.newDocumentBuilder();
Document document=domParser.parse(new File("student2.xml")); //通过已经存在的文件创建Document对象
Element root=document.getDocumentElement();
NodeList list1=root.getElementsByTagName("学号");
NodeList list2=root.getElementsByTagName("姓名");
NodeList list3=root.getElementsByTagName("出生日期");
NodeList list4=root.getElementsByTagName("身高");
int size=list1.getLength(); //获取长度
String[] number=new String[4];
String[] name=new String[4];
String[] date=new String[4];
double[] height=new double[4];
for(int k=0;k<size;k++){
Node numberNode=list1.item(k);
Node nameNode=list2.item(k);
Node dateNode=list3.item(k);
Node heightNode=list4.item(k);
number[k]=numberNode.getTextContent().trim();
name[k]=nameNode.getTextContent().trim();
date[k]=dateNode.getTextContent().trim();
height[k]=Double.parseDouble(heightNode.getTextContent().trim());
statement.setString(1, number[k]);
statement.setString(2, name[k]);
statement.setString(3, date[k]);
statement.setDouble(4, height[k]);
statement.executeUpdate();
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally{
JDBCUtilSingle.getInitJDBCUtil().closeConnection(rs, statement, connection);
}
}
/**
* 读取数据库中的数据
*/
public void getItemFromDatabase(){
Connection connection=null;
PreparedStatement statement=null;
ResultSet rs=null;
connection=JDBCUtilSingle.getInitJDBCUtil().getConnection();
String sql="SELECT * FROM `student` ";
try {
statement=connection.prepareStatement(sql);
rs=statement.executeQuery();
while(rs.next()){
System.out.println(rs.getString(1)+rs.getString(2)+rs.getString(3)+rs.getDouble(4));
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void main(String[] args){
//new XMLToDatabase().toDatabase();
new XMLToDatabaseWithDom().getItemFromDatabase();
}
}