存入mysql数据库

gqtygdpgf 2013-09-25 05:25:26
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;
}

}
怎么将代码取得的数据存入数据库
...全文
148 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
kobe8free 2013-09-26
  • 打赏
  • 举报
回复
建议你学习一下关于jdbc的知识 存入数据库 必然需要先连接 connect后再去insert
rucypli 2013-09-25
  • 打赏
  • 举报
回复
insert
發糞塗牆 2013-09-25
  • 打赏
  • 举报
回复
帮你移到mysql专区了

56,677

社区成员

发帖
与我相关
我的任务
社区描述
MySQL相关内容讨论专区
社区管理员
  • MySQL
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧