XML与数据库的交互

【第1种】从数据库中将数据写入XML中

    import java.sql.*;
    import javax.xml.parsers.*;
    import org.w3c.dom.*;
    import org.xml.sax.*;
    import java.io.*;

    public class Students{
       public Students() {
       }
       public static void main(String args[]){
          String headerstring="<?xml version=/"1.0/"?>";
          String root="<students>";
          String rootend="<students>";
          try{
                File ff=new File("students.xml");
                ff.createNewFile();
                FileWriter fw=new FileWriter(ff);
                fw.write(headerstring);
                fw.write(root);

                Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
                Connection con=DriverManager.getConnection("jdbc:odbc:Prod");
                Statement st=con.createStatement();
                ResultSet rs=st.executeQuery("select * from students");
                while(rs.next()){

                   fw.write("<student>");
                   fw.write("<stud_id>");
                   String stud_id=rs.getString("stud_id");
                   fw.write(stud_id);
                   fw.write("</stud_id>");

                   fw.write("<stud_name>");
                   String stud_name=rs.getString("stud_name");
                   fw.write(stud_name);
                   fw.write("</stud_name>");

                   fw.write("</students>");

                }
                  fw.write(rootend);
                  fw.close();
          }
          catch(Exception e){
             System.out.println(e);;
          }
       }
    }


   【第2种】从XML中将数据写入数据库中

    package employees;

    import java.sql.*;
    import javax.xml.parsers.*;
    import org.w3c.dom.*;
    import org.xml.sax.*;
    import java.io.*;

    public class Employee {
        public Employee() {
        }
        public static void main(String args[]){
            try{
                String empid;
                String empname;
                String sql;
                Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
                Connection con=DriverManager.getConnection("jdbc:odbc:Prod");
                Statement st=con.createStatement();
                File ff=new File("emp1.xml");
           
                DocumentBuilderFactory dbf=DocumentBuilderFactory.newInstance();
                DocumentBuilder db=dbf.newDocumentBuilder();
                Document d=db.parse(ff);
                NodeList n1=d.getElementsByTagName("Employee");
           
                for(int i=0;i<n1.getLength();i++){
                    Element node=(Element)n1.item(i);
                    empid=node.getElementsByTagName("empid").item(0).getFirstChild().getNodeValue();
                    empname=node.getElementsByTagName("empname").item(0).getFirstChild().getNodeValue();
                    sql="insert into emp1 values('"+empid+"','"+empname+"')";
                    st.execute(sql);
                }
                st.close();
                con.close();
            }
            catch(Exception e){
                System.out.println(e);
            }
        }
     }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值