package com.tb.action;import java.io.BufferedReader;  
import java.io.IOException;  
import java.io.InputStreamReader;  
import java.net.MalformedURLException;  
import java.net.URL;  
import java.util.ArrayList;  
import java.util.List;  
import java.util.regex.Matcher;  
import java.util.regex.Pattern;  
import com.opensymphony.xwork2.ActionSupport;
import com.tb.model.test.Test;
import com.tb.service.test.TestService;
   
public class TestAction extends ActionSupport {
private static  TestService testService;
public static Test t1=new Test();
  
    public String getHtmlContent(String htmlurl) {  
        URL url;  
        String temp;  
        StringBuffer sb = new StringBuffer();  
        try {  
            url = new URL(htmlurl);  
            BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream(), "UTF-8")); 
            while ((temp = in.readLine()) != null) {  
                sb.append(temp);  
            }  
            in.close();  
        } catch (final MalformedURLException me) {  
            System.out.println("您输入的URL有问题!");  
            me.getMessage();  
        } catch (final IOException e) {  
            e.printStackTrace();  
        }  
        return sb.toString();  
    }  
  
    /**  
     *  
     * @param s  
     * @return 获得网页标题  
     */  
    public String getTitle(String s) {  
        String regex;  
        String title = "";  
        List<String> list = new ArrayList<String>();  
        regex = "<title>.*?</title>";  
        Pattern pa = Pattern.compile(regex, Pattern.CANON_EQ);  
        Matcher ma = pa.matcher(s);  
        while (ma.find()) {  
            list.add(ma.group());  
        }  
        for (int i = 0; i < list.size(); i++) {  
            title = title + list.get(i);  
        }  
        return outTag(title);  
    }  
  
    /**  
     *  
     * @param s  
     * @return 获得链接  
     */  
    public List<String> getLink(String s) {  
        String regex;  
        List<String> list = new ArrayList<String>();  
        regex = "<a[^>]*href=(\"([^\"]*)\"|\'([^\']*)\'|([^\\s>]*))[^>]*>(.*?)</a>";  
        Pattern pa = Pattern.compile(regex, Pattern.DOTALL);  
        Matcher ma = pa.matcher(s);  
        while (ma.find()) {  
            list.add(ma.group());  
        }  
        return list;  
    }  
  
    /**  
     *  
     * @param s  
     * @return 获得脚本代码  
     */  
    public List<String> getScript(String s) {  
        String regex;  
        List<String> list = new ArrayList<String>();  
        regex = "<SCRIPT.*?</SCRIPT>";  
        Pattern pa = Pattern.compile(regex, Pattern.DOTALL);  
        Matcher ma = pa.matcher(s);  
        while (ma.find()) {  
            list.add(ma.group());  
        }  
        return list;  
    }  
      
    public List<String> getNewsurl(String s) { 
        String regex = "<a.*?</a>";  
        
        Pattern pa = Pattern.compile(regex, Pattern.DOTALL);  
        Matcher ma = pa.matcher(s);  
        List<String> list = new ArrayList<String>();  
        while (ma.find()) {  
            list.add(ma.group());
           
        }
        
        return list;  
    }
    
  
    /**  
     *  
     * @param s  
     * @return 获得CSS
     */  
    public List<String> getCSS(String s) {  
        String regex;  
        List<String> list = new ArrayList<String>();  
        regex = "<style.*?</style>";  
        Pattern pa = Pattern.compile(regex, Pattern.DOTALL);  
        Matcher ma = pa.matcher(s);  
        while (ma.find()) {  
            list.add(ma.group(1));  
        }  
        return list;  
    }  
  
    /**  
     *  
     * @param s  
     * @return
     */  
    public String outTag(final String s) {  
        return s.replaceAll("<.*?>", "");  
    }  
    
    
    
    
    public  static void main(String[] args) {  
     TestAction t = new TestAction();
     int i=1;
        String content = t.getHtmlContent("http://www.baidu.com");  
        //content = content.replaceAll("(<br>)+?", "\n");//转化换行  
        //content = content.replaceAll("<p><em>.*?</em></p>", "");// 去图片注释 
        System.out.println(content);
        List<String> a = t.getNewsurl(content);
        List<String> news = new ArrayList<String>();  
        for (String s : a) {
            news.add(s.replaceAll("<.*?>", "")); //正则表达式 
            t1.setNews_title(s);
            i++;
            String str=t1.getNews_title();
            Pattern p=Pattern.compile("<a.*?>(.+?)</a>");
            Matcher m=p.matcher(str);
            while(m.find()){            
             t1.setNews_title(m.group(1));
             System.out.println(t1.getNews_title());
             t1.setNews_id(i);
            }
        }
        testService.save(t1);
    }
public static TestService getTestService() {
return testService;
}
public static void setTestService(TestService testService) {
TestAction.testService = testService;
}    


怎么将代码取得的数据存入数据库