资源预览内容
第1页 / 共6页
第2页 / 共6页
第3页 / 共6页
第4页 / 共6页
第5页 / 共6页
第6页 / 共6页
亲,该文档总共6页全部预览完了,如果喜欢就下载吧!
资源描述
第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.getConnection(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 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 javax.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=DriverManager.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,XPathConstants.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=node.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.transform.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.JdbcOdbcDriver); 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(); number=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
收藏 下载该资源
网站客服QQ:2055934822
金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号