怎样通过XML和java实现SQL和MYSQL的异构数据交换?有代码的做好,先谢谢啦!

怎样通过XML和java实现SQL和MYSQL的异构数据交换?有代码的做好,先谢谢啦!,第1张

SAXReader

reader

=

new

SAXReader()

Document

doc=reader.read(new

File("src/Cinema.xml"))

Element

eleRoot=doc.getRootElement()

for(Iterator

its=eleRoot.elementIterator()its.hasNext()){

Element

eleCount=(Element)its.next()

Iterator

it=eleCount.elementIterator()

while(it.hasNext()){

Element

eleName=(Element)it.next()

}

用这些代码可以实现从XML中读取数据

然后建立连接用一个实体类用来临时存放SQL数据把

用List<News>存放

Class.forName("oracle.jdbc.OracleDriver")

String

url="jdbc:oracle:thin:@192.168.0.200:1521:tarena"

Connection

con=DriverManager.getConnection(url,"hsd1103","hsd1103")

String

str="insert

into

news

value(?,?,?,?,?)"//问号是对应下面你所要传递的值和

你SQL中的字段值对应

News

news=new

News()

PreparedStatement

stmt=PreparedStatement

pstm

=

con.prepareStatement()

//把xml中的数据放置到sql中

stmt.setInt(1,news.getID())

stmt.setString(2,news.getName)

stmt.setString(3,news.getAcc())

.

.

.

按照这样的方式些就可以了我用的是oracle

给你段以前写过的代码,慢慢看看吧!

/*

* Create On: 2005-12-22

* Author: small grass

* corporation: mylawcorp

*/

import java.io.IOException

import java.sql.Connection

import java.sql.DriverManager

import java.sql.PreparedStatement

import java.sql.SQLException

import javax.xml.parsers.DocumentBuilder

import javax.xml.parsers.DocumentBuilderFactory

import javax.xml.parsers.ParserConfigurationException

import org.w3c.dom.Document

import org.w3c.dom.Element

import org.w3c.dom.NodeList

import org.xml.sax.SAXException

public class XmlWriteDb {

public static void main(String[] args) throws ClassNotFoundException, SQLException, ParserConfigurationException, SAXException, IOException {

Class.forName("net.sourceforge.jtds.jdbc.Driver")

Connection conn = DriverManager.getConnection(

"jdbc:jtds:sqlserver://localhost:1433/pubs","sa","sa")

PreparedStatement stmt = conn.prepareStatement(

"insert into test values(?,?)")

DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance()

DocumentBuilder db =dbf.newDocumentBuilder()

Document d = db.parse("c:/test.xml")

NodeList nl = d.getElementsByTagName("root")

for(int i=0i<nl.getLength()i++) {

Element test = (Element) nl.item(i)

String id = test.getChildNodes().item(1).getFirstChild().getNodeValue()

String name = test.getChildNodes().item(3).getFirstChild().getNodeValue()

stmt.setInt(1,Integer.parseInt(id))

stmt.setString(2,name)

stmt.execute()

}

stmt.close()

conn.close()

}

}


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/6104030.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-03-14
下一篇 2023-03-14

发表评论

登录后才能评论

评论列表(0条)

保存