Ⅰ *.xml文件如何导入access数据库,并在窗口中显示
Access写代码导出袭/导入XML文件
'导出时:
Private Sub 导出_Click()
Application.ExportXML acExportTable, "表名", "D:\Data.XML", "DataSchema.xml"
End Sub
'导入时:
Private Sub 导入_Click()
Application.ImportXML "D:\Data.xml", acAppendData
'把数据添加到原有的表
Application.ImportXML "D:\Data.xml", acStructureAndData
'默认,建立Data表并导入数据
Application.ImportXML "D:\Data.xml", acStructureOnly
'只建立Data表结构,无数据
End Sub
Ⅱ 从oracle数据库查询数据生成xml,java解析xml文件的数据批量插入到oracle数据库(用SAX解析)
我自己前段时间写的有事就问
数据库建了一个表study
id student class teacher age 这5个字段
package com.sql.xml;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.XMLWriter;
//导出数据存入XML
public class ReadDb {
public static void main(String args[]){
//连接数据库
try{
//数据库连接
String url="jdbc:oracle:thin:@10.0.3.18:1521:hxtest18";
String classforname="oracle.jdbc.driver.OracleDriver";
String usename="yong";
String password="password";
Class.forName(classforname);
Connection con=DriverManager.getConnection(url,usename,password);
Statement sta=con.createStatement();
//查询数据
ResultSet result=sta.executeQuery(
"SELECT * FROM study");
//创建根节点
Document document=DocumentHelper.createDocument();
Element studyInfo=DocumentHelper.createElement("studyInfo");
document.setRootElement(studyInfo);
//Element studyInfo=document.addElement("studyInfo");
//当有值时查询
while(result.next()){
//增加每个节点
Element study=studyInfo.addElement("study");
Element id=study.addElement("id");
Element student=study.addElement("student");
Element classe=study.addElement("class");
Element teacher=study.addElement("teacher");
Element age=study.addElement("age");
//将数据库值通过SETTEXT为XML值
id.setText(result.getString("id"));
student.setText(result.getString("student"));
//System.out.println(id.getText());
classe.setText(result.getString("class"));
teacher.setText(result.getString("teacher"));
age.setText(result.getString("age"));
}
//将树导出到文件中:两种输出方法
XMLWriter write=new XMLWriter(new FileOutputStream(new File("studyInfo.xml")));
write.write(document);
write.close();
}catch(Exception e){
e.printStackTrace();
}
}
}
插入数据库,改了几个条件
bookstore.xml文件如下
<?xml version="1.0" encoding="UTF-8"?>
<bookstore>
<book has="100">
<bname>数据库</bname>
<price>33.80元</price>
<date>2009-10-1</date>
</book>
<book has="95">
<bname>三国演义</bname>
<price>40.00元</price>
<date>2005-1-7</date>
</book>
<book has="86">
<bname>西游记</bname>
<price>30.00元</price>
<date>2004-3-8</date>
</book>
<cd have="15">
<id>1</id>
<cname>beyound</cname>
<owner>小黎</owner>
</cd>
<cd have="17">
<id>2</id>
<cname>kugou</cname>
<owner>小强</owner>
</cd>
</bookstore>
解析如下
package com.sql.xml;
import java.io.File;
import java.io.FileWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.util.Iterator;
import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.DocumentException;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;
import org.dom4j.io.OutputFormat;
//XML的解析与转换后存入数据库
public class Fenkai {
public static void main(String args[]){
String sql,BNAME=null,PRICE=null,DAT=null;//定义数据库表段
String sqlatt,CNAME=null,ID=null,OWNER=null;
//读取xml文件
SAXReader reader=new SAXReader();
File file=new File("bookstore.xml");
try{
//数据库连接
String url="jdbc:oracle:thin:@10.0.3.18:1521:hxtest18";
String classforname="oracle.jdbc.driver.OracleDriver";
String usename="yong";
String password="password";
Class.forName(classforname);
Connection con=DriverManager.getConnection(url,usename,password);
//解析xml文件
Document document=reader.read(file);
Element root=document.getRootElement();//根节点
System.out.println("输出根节点:"+root.getName());
//下面用迭代得到子节点和孙节点
for(Iterator child=root.elementIterator();child.hasNext();){
Element childEle=(Element)child.next();
System.out.println("子:"+childEle.getName());
System.out.println("孙:");
for(Iterator grand=childEle.elementIterator();grand.hasNext();){
Element grandEle=(Element)grand.next();
System.out.println(grandEle.getName()+"值:"+grandEle.getText());
//转换数据
while(grandEle.getText().equals("数据库")){
grandEle.setText("001");
}
while(grandEle.getText().equals("三国演义")){
grandEle.setText("002");
}
while(grandEle.getText().equals("西游记")){
grandEle.setText("003");
}
while(grandEle.getText().equals("beyound")){
grandEle.setText("004");
}
while(grandEle.getText().equals("kugou")){
grandEle.setText("005");
}
}}
//得到各节点值插入数据库
for(Iterator it= root.elementIterator("book");it.hasNext();){
Element ch=(Element)it.next();
BNAME=ch.elementText("bname");
PRICE=ch.elementText("price");
DAT=ch.elementText("date");
// while(childEle.getName().equals("cd")){}
sql="insert into book values('"+BNAME+"','" +PRICE
+"','"+DAT+"')";
//sql="select * from book";//加入本语句运行不改变数据表
//执行
con.createStatement().executeUpdate(sql);}
for(Iterator itt= root.elementIterator("cd");itt.hasNext();){
Element cht=(Element)itt.next();
CNAME=cht.elementText("cname");
ID=cht.elementText("id");
OWNER=cht.elementText("owner");
sqlatt="insert into cd values('"+CNAME+"','" +ID
+"','"+OWNER+"')";
//sqlatt="select * from cd";
con.createStatement().executeUpdate(sqlatt);}
//将转换后文件存入txt
OutputFormat format=OutputFormat.createPrettyPrint();
format.setEncoding("gbk");
XMLWriter writer = new XMLWriter( new FileWriter(new File("bookchange.txt")),format);
writer.write(document);
writer.close();
System.out.println(document.asXML());//打印改变后的树
}catch(Exception e){
e.printStackTrace();
System.out.println("有错啊!");
}
}
}
Ⅲ Java中用sql语句将xml文件导入 Access 数据库,急用!谢谢
sql语句好像没这导入的功能吧,插入那是人工的方法,的自己写,不过数据库好像都版可以使用客户端进行权导入的;想mysql就有Natcat for mysql 可以直接导入xml文件到数据库的,好像Access要写程序导入。
Ⅳ 如何用java解析xml文档,然后将数据存到数据库里
package test11;
import javax.xml.parsers.*;
import org.w3c.dom.*;
import java.io.*;
public class XMLUtil
{
//该方法用于从XML配置文件中提取具体类类名,并返回一个实例对象
public static Object getBean()
{
try
{
//创建文档对象
DocumentBuilderFactory dFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = dFactory.newDocumentBuilder();
Document doc;
doc = builder.parse(new File("config.xml"));
//获取包含类名的文本节点
NodeList nl = doc.getElementsByTagName("className");
Node classNode=nl.item(0).getFirstChild();
String cName=classNode.getNodeValue();
//通过类名生成实例对象并将其返回
Class c=Class.forName(cName);
Object obj=c.newInstance();
return obj;
}
catch(Exception e)
{
e.printStackTrace();
return null;
}
}
}
<?xml version="1.0"?>
<config>
<className>test11.CatAdapter</className>
</config>
然后你吧解析处理的值放到数组或LIST或其他的你能存放的对象中。再写SQL插入到数据库就好了啊。主要数据库事务处理或用批处理
Ⅳ java 服务器端扫描文件夹下xml文件,并导入到Oracle数据库
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;
import javax.xml.parsers.*;
import javax.xml.transform.*;
import org.w3c.dom.*;
import org.xml.sax.*;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
public class CreateXMLForEnterServelet extends HttpServlet {
private static final String CONTENT_TYPE = "text/html; charset=GBK";
//Initialize global variables
public void init() throws ServletException {
}
//Process the HTTP Get request
public void doGet(HttpServletRequest request, HttpServletResponse response) throws
ServletException, IOException {
response.setContentType(CONTENT_TYPE);
PrintWriter out = response.getWriter();
//获取传递过来的值
String databaseType = request.getParameter("select");
String userName = request.getParameter("userName");
String userPassword = request.getParameter("userPassword");
String dbName = request.getParameter("dbName");
String dbSouse = request.getParameter("dbSouces");
int port = Integer.parseInt(request.getParameter("portNum"));
Document doc;
Element root;
try
{
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
String path = request.getRealPath("/");
File file = new File( path + "/" + "DBInfo.xml");
if(!file.exists())
{
doc = db.newDocument();
root = doc.createElement("DBInfo");
doc.appendChild(root);
}
else
{
doc = db.parse(file);
root = doc.getDocumentElement();
}
//添加子节点
Element rowNode = doc.createElement(databaseType);
root.appendChild(rowNode);
Node node = doc.createElement("Driver");
Node nodestr = doc.createElement("ConnectString");
Node serverName = doc.createElement("ServerName");
Node nodeName = doc.createElement("UserName");
Node nodePWD = doc.createElement("PassWord");
Node nodePort = doc.createElement("Port");
Node nodeDB = doc.createElement("DataBase");
rowNode.appendChild(node);
rowNode.appendChild(nodestr);
rowNode.appendChild(serverName);
rowNode.appendChild(nodeDB);
rowNode.appendChild(nodeName);
rowNode.appendChild(nodePWD);
rowNode.appendChild(nodePort);
String nodeValue;//Driver
String nodeConStr;//连接字符串
if(databaseType.equals("ORACLE"))
{
nodeValue = "oracle.jdbc.driver.OracleDriver";
nodeConStr = "jdbc:oracle:thin";
}
else
{
nodeValue = "com.microsoft.jdbc.sqlserver.SQLServerDriver";
nodeConStr = "jdbc:microsoft:sqlserver";
}
Text text = doc.createTextNode(nodeValue);
Text textstr = doc.createTextNode(nodeConStr);
Text textName = doc.createTextNode(userName);
Text textPWD = doc.createTextNode(userPassword);
Text textServer = doc.createTextNode(dbName);
Text textPort = doc.createTextNode(String.valueOf(port));
Text textDB = doc.createTextNode(dbSouse);
node.appendChild(text);
nodestr.appendChild(textstr);
nodeName.appendChild(textName);
nodePWD.appendChild(textPWD);
serverName.appendChild(textServer);
nodePort.appendChild(textPort);
nodeDB.appendChild(textDB);
DOMSource doms = new DOMSource(doc);
StreamResult sr = null;
TransformerFactory tff = TransformerFactory.newInstance();
Transformer tf = tff.newTransformer();
sr = new StreamResult(file);
tf.transform(doms,sr);
out.println("<h1>Create XMLFile Is Ok!!</h1>");
request.getRequestDispatcher("index.jsp").forward(request,response);
}
catch(Exception ex)
{
out.println("<h1>生成 XML 配置文件时出现错误:</h1><br/><h4>"+ex+"</h4>");
out.println("<br/><br/>");
out.print("<a href='index.jsp'>【点这里回到首页】</a>");
}
out.close();
}
//Process the HTTP Post request
public void doPost(HttpServletRequest request, HttpServletResponse response) throws
ServletException, IOException {
doGet(request, response);
}
//Clean up resources
public void destroy() {
}
}