路过的朋友就踩两脚
SQL的导出/导入是把数据导出/导入来了,你要想做这个就简单,你可以把数据导成XML形式,就可以把数据插入到不同数据库中了。
我有一个例子,不过是2年前的了,思路是一样的,你可以优化一下:
[CODE]
Java使XML和DB互相访问之(DB读记录到XML文件)
用JAVA把ORACLE的记录放入到XML中
ORACLE中表结构:
字段名 类型
name varchar2(10)
age number(4)
sex varchar2(10)
Java文件:
package DBToXml;
import javax.xml.parsers.*;
import org.w3c.dom.*;
import java.sql.*;
import java.io.*;
import javax.xml.transform.*;
import javax.xml.transform.dom.*;
import javax.xml.transform.stream.*;
public class DBToXml
{
public static void main(String args[])
{
String url="jdbc:Oracle:thin:@a17:1521:a17"; //此处URL为a17为本机名,1521为端口号,第二个a17为ORACLE实例名。
Connection con=null;
Statement stm=null;
ResultSet rs=null;
Document doc;
Element stus=null;
Element stu=null;
Element name=null;
Element age=null;
Element sex=null;
try
{
System.out.println("Open the DB.........");
Class.forName("oracle.jdbc.driver.OracleDriver").newInstance();
con=DriverManager.getConnection(url,"scott","tiger");
stm=con.createStatement();
rs=stm.executeQuery("select * from stu");
DocumentBuilderFactory dbf=DocumentBuilderFactory.newInstance();
DocumentBuilder db=dbf.newDocumentBuilder();
doc=db.newDocument();
File file=new File("StudentTest.xml");
stus=doc.createElement("Students");
while(rs.next())
{
stu=doc.createElement("student");
name=doc.createElement("name");
age=doc.createElement("age");
sex=doc.createElement("sex");
name.appendChild(doc.createTextNode(rs.getString("name")));
age.appendChild(doc.createTextNode(Integer.toString(rs.getInt("age"))));
sex.appendChild(doc.createTextNode(rs.getString("sex")));
stu.appendChild(name);
stu.appendChild(age);
stu.appendChild(sex);
stus.appendChild(stu);
}
doc.appendChild(stus);
TransformerFactory tf=TransformerFactory.newInstance();
Transformer t=tf.newTransformer();
t.transform(new DOMSource(doc),new StreamResult(file));
System.out.println("Write to the File Over!.........");
rs.close();
stm.close();
con.close();
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
[/CODE]
[CODE]
Java使XML和DB互访之(XML元素存入DB中)
Java读XML存入到ORACLE中去:
XML文件:
<?xml version="1.0" ?>
<students>
<student>
<name>One</name>
<age>20</age>
<sex>M</sex>
</student>
</students>
Java文件:
package XmlToDB;
import javax.xml.parsers.*;
import org.w3c.dom.*;
import java.sql.*;
import java.io.*;
public class XmlToDB
{
public static void main(String args[])
{
String url="jdbc:Oracle:thin:@a17:1521:a17";
Document doc=null;
String name;
int age;
String sex;
Statement stm;
Connection con;
try
{
System.out.println("Open the DB for Write.........");
Class.forName("oracle.jdbc.driver.OracleDriver").newInstance();
con=DriverManager.getConnection(url,"scott","tiger");
stm=con.createStatement();
DocumentBuilderFactory dbf=DocumentBuilderFactory.newInstance();
DocumentBuilder db=dbf.newDocumentBuilder();
doc=db.parse(new File("student.xml"));
NodeList nl=doc.getElementsByTagName("student");
for(int i=0;i<nl.getLength();i++)
{
Element e=(Element)nl.item(i);
name=e.getElementsByTagName("name").item(0).getFirstChild().getNodeValue();
age=Integer.parseInt(e.getElementsByTagName("age").item(0).getFirstChild().getNodeValue());
sex=e.getElementsByTagName("sex").item(0).getFirstChild().getNodeValue();
stm.executeUpdate("insert into stu values(''"+name+"'',"+age+",''"+sex+"'')");
}
System.out.println("Write ok.....");
stm.close();
con.close();
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
[/CODE]