将XML文件写入数据库中
来源:互联网 发布:高中英语语法视频 知乎 编辑:程序博客网 时间:2024/05/22 01:41
package edu.tsinghua.jdbc;import java.sql.*;import javax.xml.parsers.*;import org.w3c.dom.*;import java.io.*;/** * 将xml文件中的数据读取后保存到数据库中 * @author admin * */public class XMLtoDB { static Connection con; static String driver = "com.microsoft.sqlserver.jdbc.SQLServerDriver"; static String url ="jdbc:sqlserver://localhost:1433;DatabaseName=mytest"; static String user = "xyn"; static String password = "xyn"; public static void main(String args[]){ Document doc; // int id,age; String name,pwd,address,age,id; String sql = "insert into Student(name,password,address,age) values(?,?,?,?)"; try{ //连接数据库,并且得到数据库中的数据,放在结果集中 Class.forName(driver); con = DriverManager.getConnection(url, user ,password); PreparedStatement ps = con.prepareStatement(sql); //创建document实例对象 DocumentBuilderFactory bf = DocumentBuilderFactory.newInstance(); DocumentBuilder db = bf.newDocumentBuilder(); doc = db.parse(new FileInputStream(new File("class2.xml"))); NodeList nlist = doc.getElementsByTagName("student"); for(int i=0;i<nlist.getLength();i++){ Element node = (Element)nlist.item(i); //此处Student表中的id是自动生成的,所以此处的id用不上// id = node.getAttributes().getNamedItem("id").getNodeValue(); name =node.getElementsByTagName("name").item(i).getFirstChild().getNodeValue(); pwd = node.getElementsByTagName("password").item(i).getFirstChild().getNodeValue(); address = node.getElementsByTagName("address").item(i).getFirstChild().getNodeValue(); age = node.getElementsByTagName("age").item(i).getFirstChild().getNodeValue(); ps.setString(1,name.trim()); ps.setString(2, pwd.trim()); ps.setString(3, address.trim()); ps.setInt(4, Integer.parseInt(age.trim())); ps.executeUpdate(); } //关闭所有的连接 if(ps!=null){ ps.close(); con.close(); } }catch(Exception e){ e.printStackTrace(); } }}
0 0
- 将XML文件写入数据库中
- 设置oracle中的blob字段,将xml文件写入到数据库中
- 将XML文档写入数据库
- 将XML文档写入数据库
- 将datatable写入XML文件
- 将数据库中读取的文件写入Excel文件
- 使用utl_file将oracle数据库中数据写入excel文件
- 使用utl_file将oracle数据库中数据写入excel文件
- .NET : 如何将大文件写入到数据库中
- 将oracle数据库中数据写入excel文件
- 使用java将mp3文件写入mysql数据库中
- ASP中查询数据库记录写入XML文件示例
- ASP中查询数据库记录写入XML文件示例
- ASP中查询数据库记录写入XML文件示例
- 将session写入数据库中
- XML:将信息写入到XML中
- 从数据库中写入XML
- 如何:将类数据写入 XML 文件
- 【BZOJ1806】[Ioi2007]Miners 矿工配餐【DP】
- java面向对象的体现下篇
- NYOJ_35 表达式求值
- tcp 自连接
- NKOI 1344 人力资源管理
- 将XML文件写入数据库中
- poj 3279 Fliptile(搜索)
- Html4和Html5之间有什么区别,Html5有什么新功能
- arcgis api for javascript 4.0 入门学习(2)创建一个3D地图
- php的日期时间函数
- javaweb网站get方式url传值中文汉字乱码问题
- Python 函数 (教程 2)
- ubuntu12.04 gcc和g++支持 c++11特性
- 【记录】PWM