当前位置:编程学习 > XML/UML >>

从数据库中读地址,并发http请求得到xml并保存

[java]
package com.panpan.datatools; 
 
import java.io.FileWriter; 
import java.sql.Connection; 
import java.sql.DriverManager; 
import java.sql.PreparedStatement; 
import java.sql.ResultSet; 
import java.util.LinkedList; 
 
import junit.framework.TestCase; 
 
import org.apache.commons.lang.StringUtils; 
import org.dom4j.Document; 
import org.dom4j.DocumentHelper; 
import org.dom4j.io.XMLWriter; 
import org.junit.After; 
import org.junit.Before; 
import org.junit.Test; 
 
import com.panpan.util.HttpClientUtils; 
 
public class MsgGenerateXMLhelper extends TestCase { 
 
    @Test 
    public void testOp() throws Exception { 
        pstmt = con.prepareStatement("select id,url from t_user"); 
        rs = pstmt.executeQuery(); 
        LinkedList<Msg> msg_l = new LinkedList<Msg>(); 
        while (rs.next()) { 
            Msg msg = new Msg(); 
            msg.setId(rs.getInt(1)); 
            msg.setUrl(rs.getString(2)); 
            msg_l.add(msg); 
        } 
        for (Msg msg : msg_l) { 
            if(StringUtils.isNotBlank(msg.getUrl())){ 
                String xml = HttpClientUtils.sendGetRequest(msg.getUrl(), "UTF-8"); 
                XMLWriter writer = null; 
                if (StringUtils.isNotBlank(xml)) { 
                    try{ 
                        Document doc = DocumentHelper.parseText(xml); 
                        writer = new XMLWriter(new FileWriter("C:\\xmldata\\"+msg.getId()+".xml")); 
                        writer.write(doc); 
                    }catch(Exception ex){ 
                         
                    }finally{ 
                        if(writer != null){ 
                            writer.close(); 
                        } 
                    } 
                } 
            } 
        } 
    } 
 
    @Before 
    public void setUp() throws Exception { 
        String CLASSNAME = "com.mysql.jdbc.Driver"; 
        String URL = "jdbc:mysql://local:3306/database"; 
        String UNAME = "root"; 
        String PWD = "root"; 
        Class.forName(CLASSNAME); 
        con = DriverManager.getConnection(URL, UNAME, PWD); 
    } 
 
    @After 
    public void tearDown() throws Exception { 
        if (rs != null) { 
            rs.close(); 
        } 
        if (pstmt != null) { 
            pstmt.close(); 
        } 
        if (con != null) { 
            con.close(); 
        } 
    } 
 
    Connection con = null; 
    PreparedStatement pstmt = null; 
    ResultSet rs = null; 
 
    class Msg { 
        public Integer getId() { 
            return id; 
        } 
        public void setId(Integer id) { 
            this.id = id; 
        } 
        Integer id; 
        String url; 
        public String getUrl() { 
            return url; 
        } 
        public void setUrl(String url) { 
            this.url = url; 
        } 
    } 

补充:软件开发 , Java ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,