《XML基础教程2版第7章的代码》由会员分享,可在线阅读,更多相关《XML基础教程2版第7章的代码(6页珍藏版)》请在金锄头文库上搜索。
1、第7章 XML与数据库7.3 连接数据库例子1InsertAndQuery.javaimport java.sql.*; public class InsertAndQuery public static void main(String args) Connection con; Statement sql; ResultSet rs; try Class.forName(sun.jdbc.odbc.JdbcOdbcDriver); catch(ClassNotFoundException e) System.out.println(+e); try con=DriverManager.ge
2、tConnection(jdbc:odbc:company,); sql=con.createStatement(); sql.executeUpdate (INSERT INTO person VALUES(a1004,张三,1995-12-12,3000); sql.executeUpdate (INSERT INTO person VALUES(a1005,李四,1996-09-10,5000); rs=sql.executeQuery(SELECT * FROM person); while(rs.next() String number=rs.getString(1); String
3、 name=rs.getString(2); Date birth=rs.getDate(3); double salary=rs.getDouble(4); System.out.println(number+,+name+,+birth+,+salary); con.close(); catch(SQLException e) System.out.println(e); 7.4 XML至数据库例子2example7_2.xml A1006 王经路 1995-11-11 6789 A1007 赵懂知 1995-06-28 5673 XMLToDatabase.javaimport java
4、x.xml.xpath.*;import org.xml.sax.*;import org.w3c.dom.*; import java.sql.*;public class XMLToDatabase public static void main(String args) Connection con=null; Statement sql=null; ResultSet rs=null; try Class.forName(sun.jdbc.odbc.JdbcOdbcDriver); catch(Exception e) System.out.println(+e); try con=D
5、riverManager.getConnection(jdbc:odbc:company,); sql=con.createStatement(); XPathFactory xPathFactory=XPathFactory.newInstance(); XPath xPath=xPathFactory.newXPath(); InputSource source=new InputSource(example9_2.xml); String path=/雇员列表/雇员; NodeList nodeList=(NodeList)xPath.evaluate(path,source,XPath
6、Constants.NODESET); int size=nodeList.getLength(); for(int i=0;isize;i+) int m=i+1; path=/雇员列表/雇员+m+/*/text(); nodeList=(NodeList)xPath.evaluate(path,source,XPathConstants.NODESET); int length=nodeList.getLength(); String a=new Stringlength; for(int k=0;klength;k+) Node node=nodeList.item(k); ak=nod
7、e.getTextContent().trim(); String insertData= INSERT INTO person VALUES(+a0+,+a1+,+a2+,+a3+); sql.executeUpdate(insertData); con.close(); catch(Exception exp) System.out.println(exp); 7.5 数据库至XML例子3DatabaseToXML.javaimport javax.xml.transform.*;import javax.xml.transform.stream.*;import javax.xml.tr
8、ansform.dom.*;import org.w3c.dom.*; import javax.xml.parsers.*;import java.io.*;import java.sql.*;public class DatabaseToXML public static void main(String args) Connection con; Statement sql; ResultSet rs; String number=; String name=; String date=; String salary=; try Class.forName(sun.jdbc.odbc.J
9、dbcOdbcDriver); catch(ClassNotFoundException e) System.out.println(+e); try con=DriverManager.getConnection(jdbc:odbc:company,); sql=con.createStatement (ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_READ_ONLY); rs=sql.executeQuery(SELECT * FROM person); rs.last(); int recordAmount=rs.getRow(); n
10、umber=new StringrecordAmount; name=new StringrecordAmount; date=new StringrecordAmount; salary=new StringrecordAmount; int k=0; rs.beforeFirst(); while(rs.next() numberk=rs.getString(1); namek=rs.getString(2); datek=rs.getDate(3).toString(); salaryk=String.valueOf(rs.getDouble(4); k+; con.close(); catch(SQLException e) System.out.println(e); try DocumentBuilderFactory factory= DocumentBuilderFactory.newInstance(); DocumentBuilder domPaser=factory.newDocumentBuilder(); Document document=domPaser.newDocument(); document.setXm