【java web】--ojdbc导入xml数据
来源:互联网 发布:java源码分析工具 编辑:程序博客网 时间:2024/04/30 07:15
我们存取数据,常用的是从jsp页面获取数据,通过控件触发事件,传值到后台,持久化到数据库。最近学了一种特别好玩的新的,就是把数据以xml文件的形式,导入到数据库中。实现效果就是将如下的xml文件的节点数据,通过java代码,直接导入到已经创建好的T_XML数据表中。
一、代码目录结构
DbUtil.java : 连接数据库的工具类
TestXMLImport.java : 程序台代码
dom4j-1.6.1.jar : dom4j是一个Java的XML API,类似于jdom,用来读写XML文件
jaxen-1.1-beta-6.jar : Jaxen是一个Java编写的开源的XPath库。这是适应多种不同的对象模型,包括DOM,XOM,dom4j和JDOM。也可以作为适配器,转换Java字节代码或XML的Java bean为xml,可以使用XPath查询这些树。
ojdbc14.jar : ojdbc是oracle数据库提供的thin驱动
test01.XML : 导入的xml数据文件
二、代码详情
TestXMLImport.java
package test_xmlImport;import java.io.File;import java.sql.Connection;import java.sql.PreparedStatement;import java.util.Iterator;import java.util.List;import org.dom4j.Document;import org.dom4j.Element;import org.dom4j.io.SAXReader;/** * @ClassName:TestXMLImport * @Description:TODO * @author wm * @date 2016年1月22日上午8:48:46 */public class TestXMLImport {public static void main(String[] args){String sql="insert into T_XML(NUMERO,REPOSICION,NOMBRE,TURNOS)values(?,?,?,?)";Connection conn=null;PreparedStatement pstmt=null;try {conn=DbUtil.getConnection();pstmt=conn.prepareStatement(sql);//创建解析器,读取xml并赋值给文档Document doc=new SAXReader().read(new File("D:/MyEclipse2014--wm/workspacewm/test_xmlImport/xml/test01.XML"));//查询节点List itemList=doc.selectNodes("/ACCESOS/item/SOCIO");//遍历节点for(Iterator iter=itemList.iterator();iter.hasNext();){Element el=(Element)iter.next();//获取当前元素的内容String numero=el.elementText("NUMERO");String reposicion=el.elementText("REPOSICION");String nombre=el.elementText("NOMBRE");List turnosList=el.elements("TURNOS");//初始化StringBuffer空对象,线程安全StringBuffer sbString=new StringBuffer();//遍历turnosList下面的子标签,并获取文本,拼接for(Iterator iter1=turnosList.iterator();iter1.hasNext();){Element turnosElt=(Element)iter1.next();String lu=turnosElt.elementText("LU");String ma=turnosElt.elementText("MA");String mi=turnosElt.elementText("MI");String ju=turnosElt.elementText("JU");String vi=turnosElt.elementText("VI");String sa=turnosElt.elementText("SA");String doo=turnosElt.elementText("DO");sbString.append(lu+","+ma+","+mi+","+ju+","+vi+","+sa+","+doo);}//绑定sql,插入条件,执行sql语句pstmt.setString(1, numero);pstmt.setString(2, reposicion);pstmt.setString(3, nombre);//创建新的String对象pstmt.setString(4, sbString.toString());//批量添加到数据库pstmt.addBatch();}//批量执行更新到数据库pstmt.executeBatch();System.out.println("将XML导入数据库成功!");} catch (Exception e) {e.printStackTrace(); }finally{DbUtil.close(pstmt);DbUtil.close(conn);}}}
DbUtil.java
package test_xmlImport;import java.sql.Connection;import java.sql.DriverManager;import java.sql.PreparedStatement;import java.sql.SQLException;import java.sql.ResultSet;import java.sql.Statement;public class DbUtil {public static Connection getConnection(){Connection conn=null;try{Class.forName("oracle.jdbc.driver.OracleDriver");String url="jdbc:oracle:thin:@localhost:1521:orcl";String username="drp1";String password="drp1";conn=DriverManager.getConnection(url,username,password);}catch(ClassNotFoundException e){e.printStackTrace();}catch(SQLException e){e.printStackTrace();}return conn;}public static void close(Connection conn){if(conn !=null){try {conn.close();} catch (SQLException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}public static void close(Statement pstmt){if(pstmt!=null){try {pstmt.close();} catch (SQLException e) {e.printStackTrace();}}}}
三、调试小技巧-Expressions
调试的过程中,可以通过添加表达式,可以查看某节点的属性值。
Expressions View
右键Add Watch Expressions
输入变量get属性
http://write.blog.csdn.net/postedit
四、总结
优点
缺点
代码结构:
可读性好,
结构严谨,
简单,
搜索效率高
插入修改难,
数据量大的时候转换成二进制影响效率,
对数据的管理不够完善
对外交互:
平台间数据交换、与数据库交互方便,
可以选择性更新
平台间通信规范需要定义
面向对象:
数据显示分离
- 【java web】--ojdbc导入xml数据
- maven环境快速搭建(如何在pom.xml导入一个jar包)(导入ojdbc错误的原因)
- XML数据导入数据库
- cakephp2 XML数据导入
- R-导入XML数据
- Web利用POI导入XML
- JAVA WEB 实现文件数据导入(CSV、TXT、excel)
- DataSet 读取xml 导入数据
- Xml数据导入MySQL数据库
- Java数据导入Excel
- JAVA 数据导入Excel
- Java数据导入Excel
- java导入ecxl数据
- Java数据导入Excel
- #.java数据导入Excel
- 【Java.Web】web.xml详解
- [JAVA web]ftpClient, web.xml
- java web 3.1 web.xml
- xampp中production的php.ini与默认的有何不同
- 用于将阿拉伯数字转成中文数字
- win8.1 Offline Files、Device Association Service和Network Connections
- 选择角点
- Apple 一个我越发看不懂的公司
- 【java web】--ojdbc导入xml数据
- listview适配器的初级优化
- Android开发笔记--Dialog提示框
- android studio for mac Cannot run program Permission denied
- android studio 预览布局文件错误
- 解决Android Studio导入项目一直停留在building "your project name" gradle project info的问题
- [!] Unable to satisfy the following requirements: - `AFNetworking (~> 2.6.1)` required by `Podfile`
- Android开发笔记--调用短信,电话,E-Mail,浏览器
- 使用MapReduce实现Bayes算法