Read the address from the database, get xml for concurrent http requests, and save

Source: Internet
Author: User

[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. Collections list;
 
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.exe cuteQuery ();
Vertex list <Msg> msg_l = new vertex list <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 = incluenthelper. 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;
}
}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.