<file_sep>package com.lobinary.normal.cav.dao;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import com.lobinary.normal.cav.dto.CAVDto;

public class DateBaseUtil {
	String drivename = "com.mysql.jdbc.Driver";
	String url = "jdbc:mysql://172.16.17.32:3306/lobinary";
	String user = "root";
	String password = "<PASSWORD>";
	String insql;
	String upsql;
	String delsql;
	String sql = "select * from cav";
	Connection conn = null;
	ResultSet rs = null;
	
	public Connection ConnectMysql() {
		try {
			if(conn==null||conn.isClosed()){
				Class.forName(drivename);
				conn = (Connection) DriverManager.getConnection(url, user, password);
				if (!conn.isClosed()) {
					System.out.println("cav mysql数据库连接成功");
				} else {
					System.out.println("cav mysql数据库连接失败");
				}
			}
		} catch (Exception ex) {
			ex.printStackTrace();
		}
		return conn;
	}

	public void CutConnection(Connection conn) throws SQLException {
		try {
			if (rs != null)
				;
			if (conn != null)
				;
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			rs.close();
			conn.close();
		}
	}

	class user {// 内部类，其字段对应用来存放、提取数据库中的数据
		int userid;
		String username = "";
		String password = "";
		String email = "";

		// 通过set方法，往类的实例里“存放”数据
		// 通过get方法，从类的实例里“获得”数据，然后再通过插入数据库
		public void setId(int userid) {
			this.userid = userid;
		}

		public void setName(String username) {
			this.username = username;
		}

		public void setPassword(String password) {
			this.password = password;
		}

		public void setEmail(String email) {
			this.email = email;
		}

		public Integer getId() {
			return userid;
		}

		public String getName() {
			return username;
		}

		public String getPassword() {
			return password;
		}

		public String getEmail() {
			return email;
		}

	}
	
	public static void main(String[] args) {
		DateBaseUtil db = new DateBaseUtil();
//		CAVDto cav = new CAVDto();
//		cav.setName("测试名称");
//		cav.setImagelocalpath("暂无");
//		System.out.println(db.InsertSql(cav));
		db.SelectSql("select * from cav limit 2");
	}

	// 插入、删除、更新的方法是一样的，不一样的是数据库参数
	public boolean InsertSql(CAVDto cav) {
		ConnectMysql();
		try {
			//INSERT INTO `lobinary`.`cav` (`id`, `name`, `number`, `date`, `time`, `director`, `makers`, `publisher`, `series`, `type`, `actor`, `detailurl`, `imageurl`, `imagelocalpath`) VALUES ('1', 'testName', '1235', '日期', '时间', '导演', '制造商', '出版商', '系列号', '类型', '演员', '详细地址', '图片地址', '本地图片位置');
			insql = "insert into cav(name, number, date, time, director, makers, publisher, series, type, actor, detailurl, imageurl, imagelocalpath)"
					+ " values(?,?,?,?,?,?,?,?,?,?,?,?,?)";
			// 上面的方法比下面的方法有优势，一方面是安全性，另一方面我忘记了……
			// insql="insert into user(userid,username,password,email) values(user.getId,user.getName,user.getPassword,user.getEmail)";
			PreparedStatement ps = conn.prepareStatement(insql);
			// .preparedStatement(insql);
			// PreparedStatement ps=(PreparedStatement)
			// conn.prepareStatement(insql);
			ps.setString(1, cav.getName());
			ps.setString(2, cav.getNumber());
			ps.setString(3, cav.getDate());
			ps.setString(4, cav.getTime());
			ps.setString(5, cav.getDirector());
			ps.setString(6, cav.getMakers());
			ps.setString(7, cav.getPublisher());
			ps.setString(8, cav.getSeries());
			ps.setString(9, cav.getType());
			ps.setString(10, cav.getActor());
			ps.setString(11, cav.getDetailurl());
			ps.setString(12, cav.getImageurl());
			ps.setString(13, cav.getImagelocalpath());
			int result = ps.executeUpdate();
			// ps.executeUpdate();无法判断是否已经插入
			if (result > 0){
//				System.out.println(cav.getName() + "插入结果为：" + result);
				return true;
			}
				
		} catch (Exception e) {
			e.printStackTrace();
		}
		return false;
	}

	// 与其他操作相比较，查询语句在查询后需要一个查询结果集（ResultSet）来保存查询结果
	public List<CAVDto> SelectSql(String sql) {
		List<CAVDto> cavList = new ArrayList<CAVDto>();
		try {
			ConnectMysql();
			Statement statement = conn.createStatement();
			rs = statement.executeQuery(sql);
			while (rs.next()) {
				long id = rs.getInt("id");
				String name = rs.getString("name");
				String number = rs.getString("number");
				String date = rs.getString("date");
				String time = rs.getString("time");
				String director = rs.getString("director");
				String makers = rs.getString("makers");
				String publisher = rs.getString("publisher");
				String series = rs.getString("series");
				String type = rs.getString("type");
				String actor = rs.getString("actor");
				String detailurl = rs.getString("detailurl");
				String imageurl = rs.getString("imageurl");
				String imagelocalpath = rs.getString("imagelocalpath");
				CAVDto cav = new CAVDto();
				cav.setId(id);
				cav.setName(name);
				cav.setNumber(number);
				cav.setDate(date);
				cav.setTime(time);
				cav.setDirector(director);
				cav.setMakers(makers);
				cav.setPublisher(publisher);
				cav.setSeries(series);
				cav.setType(type);
				cav.setActor(actor);
				cav.setDetailurl(detailurl);
				cav.setImageurl(imageurl);
				cav.setImagelocalpath(imagelocalpath);
				cavList.add(cav);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return cavList;

	}

	public boolean UpdateSql(String upsql) {
		try {
			ConnectMysql();
			PreparedStatement ps = conn.prepareStatement(upsql);
			int result = ps.executeUpdate();// 返回行数或者0
			if (result > 0)
				return true;
		} catch (SQLException ex) {
			ex.printStackTrace();
//			Logger.getLogger(ConnectDatabase.class.getName()).log(Level.SEVERE, null, ex);
		}
		return false;
	}

	public boolean DeletSql(String delsql) {

		try {
			PreparedStatement ps = conn.prepareStatement(upsql);
			int result = ps.executeUpdate(delsql);
			if (result > 0)
				return true;
		} catch (SQLException ex) {
//			Logger.getLogger(ConnectDatabase.class.getName()).log(Level.SEVERE, null, ex);
		}
		return false;
	}

}<file_sep>package com.lobinary.normal.cav.service;

import java.net.SocketTimeoutException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.lobinary.normal.cav.dao.CAVDao;
import com.lobinary.normal.cav.dto.CAVDto;
import com.lobinary.normal.cav.util.HttpUtil;

public class WebService {
	
	public CAVDao cavDao = new CAVDao();
	public int start = 1642;
	public int end = 5244;
	public int threadNum = 1;
	
	public void service() throws SocketTimeoutException{
		
		for(int thread = 1;thread <= threadNum;thread++){
			new Thread(){
				@Override
				public void run() {
					while(true){
						int pageNum = getPageNum();
						boolean isSuccess = false;
						int times = 1;
						while(!isSuccess){
							long startTime = System.currentTimeMillis();
							try {
								isSuccess = go(pageNum);
							} catch (SocketTimeoutException e) {
//								e.printStackTrace();
							}
							long endTime = System.currentTimeMillis();
							System.out.println("第" + pageNum +"页处理,第" + times + "次执行" + (endTime-startTime) + "毫秒,结果为：" + isSuccess);
							times ++;
						}
					}
				}
				
			}.start();
		}
		
//		String test = "alt=\"蜜のめぐり逢い 森下まゆみ\" src=\"http://pics.dmm.co.jp/digital/video/oae00091/oae00091ps.jpg";
//		System.out.println(test.substring(test.indexOf("src=\"") + 5));
//		System.out.println(cl.get(1));
	}
	
	public synchronized int getPageNum(){
		start = start + 1;
		return start - 1;
	}
	
	public boolean go(int i) throws SocketTimeoutException{
		try {
//			Thread.sleep(5000);
			List<String> cl = catchDataFromWebsite(i);
			for(String s:cl){
				boolean result = cavDao.insert(analysisData(s));
			}
			return true;
		} catch (Exception e) {
//			e.printStackTrace();
			return false;
		}
	}
	
	
	public CAVDto analysisData(String s){
		CAVDto cav = new CAVDto();
//		System.out.println(s);
		String name = s.substring(s.indexOf("title=\"") + 7, s.indexOf("\" target=\"_blank\">"));
		cav.setName(name);
		cav.setNumber(catchStringFromString(s, "番号：</span>", "</li>"));
		cav.setDate(catchStringFromString(s, "发行时间：</span>", "</li>"));
		cav.setTime(catchStringFromString(s, "影片时长：</span>", "</li>"));
		cav.setDirector(catchStringFromString(s, "的所有作品\">", "</a></li>"));
		String makers = catchStringFromString(s, "制作商：", "<li><span class=\"classTitle\">发行商");
		cav.setMakers(catchStringFromString(makers, "的所有作品\">", "</a></li>"));
		String publisher = catchStringFromString(s, "发行商：", "<li><span class=\"classTitle\">系列：");
		cav.setPublisher(catchStringFromString(publisher, "的所有作品\">", "</a></li>"));
		cav.setSeries(catchStringFromString(s, "系列：</span>", "</li>"));
		String type = catchStringFromString(s, "影片类别：</span>", "</li>");
		String[] types = type.split("</a>");
		String tpyeTemp = "";
		for(String t :types){
			if(t.contains(">")){
				tpyeTemp = tpyeTemp + t.substring(t.indexOf(">") + 1) + ";";
			}else{
//				System.out.println("无效t：" + t);
			}
		}
		cav.setType(tpyeTemp);
		String actorTemp = "";
		if(s.contains("#888888;\">")){
			String[] actors = s.split("#888888;\">");
			for(String a :actors){
				if(a.contains("番号")){
//					System.out.println("顶端数据，予以pass");
				}else{
					actorTemp = actorTemp + a.substring(0,a.indexOf("</span>")) + ";";
				}
			}
		}else{
//			System.out.println("未发现演员");
		}
		cav.setActor(actorTemp);
		cav.setDetailurl(s.substring(s.indexOf("<a href=\"") + 9, s.indexOf("\" title=\"")));
		String imageUrl = catchStringFromString(s, "<img", "/></a>").replace("\"", "").replace(" ", "");
		cav.setImageurl(imageUrl.substring(imageUrl.indexOf("src=\"") + 5));
		cav.setImagelocalpath("");
		return cav;
	}
	
	/**
	 * 捕获数据从网站
	 * @return
	 * @throws SocketTimeoutException 
	 */
	public List<String> catchDataFromWebsite(int page) throws SocketTimeoutException{
		List<String> contentsList = new ArrayList<String>();
		Map<String,String> paramMap = new HashMap<String,String>();
		//http://instsee.com/default.aspx?time=published&p1=
		//http://instsee.com/default.aspx?time=published&p1=&page=2
		//http://instsee.com/default.aspx?time=published&p1=&page=3
		String s = HttpUtil.sendPostRequest("http://instsee.com/default.aspx?time=published&p1=&page=" + page, paramMap, "UTF-8", "UTF-8");
		String contentStr = "";
		try {
			contentStr = s.substring(s.indexOf("<div id=\"list_games\">"));
		} catch (Exception e) {
//			System.out.println("发生错误：网站返回数据为：");
//			System.out.println(s);
		}
		contentStr = contentStr.substring(0, contentStr.indexOf("<div id=\"listpager\" class=\"pagination\">"));
		String[] contents = contentStr.split("<div class=\"list_game\">");
//		System.out.println(contents[0]);
//		System.out.println("=========================");
//		System.out.println(contents[19]);
		for(String content:contents){
			if(content.contains("番号")){
				contentsList.add(content);
			}
		}
//		System.out.println("从" + contents.length + "条数据中获取到" + contentsList.size() + "条有效数据");
		return contentsList;
	}
	
	public static void main(String[] args) throws SocketTimeoutException {
		WebService ws = new WebService();
		ws.service();
	}
	
	
	/**
	 * 从字符串中根据前后缀，获取中间字符串
	 * @param pre
	 * @param rear
	 */
	public static String catchStringFromString(String str,String pre,String rear) {
		
		String result = "";
		try {
			int start = str.indexOf(pre);
			int end = start + str.substring(start).indexOf(rear);
			if(start<end){
				result = str.substring(start+pre.length(),end);
			}
		} catch (Exception e) {
			System.out.println("从字符串中捕获参数异常，异常原因为：" + e);
		}
		return result;
	}

}
<file_sep>package com.boce.test;

import java.util.ArrayList;
import java.util.List;

/**
 * 本测试用于证明传入方法体中的List在方法体中如果被改变,是否外部参数会随着改变
 * 实验证明,改变方法体中传入的list,外部的list也会跟着改变
 * @author Boce
 *
 */
public class ListTest {

	public static void main(String[] args) {
		ListTest lt = new ListTest();
		List<User> userList = new ArrayList<User>();
		User u = new User();
		u.setName("boce");
		u.setAge(8);
		userList.add(u);
		System.out.println(userList.get(0).getName());
		List<User> userListed = lt.changeList(userList);
		System.out.println(userList.get(0).getName());
		System.out.println(userListed.get(0).getName());
	}
	
	public List<User> changeList(List<User> list){
		list.get(0).setName("changed");
		return list;
	}
	

	
}
<file_sep>package com.lobinary.apc.constants;

/**
 * 
 *	<pre> 
 *	状态码常量类
 *
 *	状态码分配规则：
 *	123456 状态码长度为6位
 *	1代表所属类别
 *	23代表所属子类别 [00为公共 ]
 *	4代表状态码属性    [1.状态型 4.异常型]
 *	56代表子变量
 *
 * 	</pre>
 * @author lobinary 
 * @since 2015年10月11日 下午10:16:54
 * @version V1.0.0 描述 : 创建该文件
 *
 */
public enum CodeDescConstants {

	//1***** 公共状态码

	//1004** 公用公共状态码
	PUB_SYSTEM_EXCEPTION("100400","系统异常"),

	//1011** 公用状态码
	PUB_DEALING("101100","处理中"),
	PUB_DEAL_SUCCESS("101101","处理成功"),
	PUB_DEAL_FAIL("101102","处理失败"), 
	PUB_SUBMIT_SUCCESS("101103","提交成功"), 
	PUB_SUBMIT_FAIL("101104","提交失败"), 
	PUB_UPLOAD_SUCCESS("101105","上传成功"), 
	PUB_UPLOAD_FAIL("101106","上传失败"), 
	
	//2***** 业务型状态码

	//2001** 业务公共 状态码
	SERVICE_EXECUTE_SUCCESS("200101","业务执行成功"),
	
	//2004** 业务公共 异常码
	SERVICE_NULL_BUSINESS("200401","无此业务"),
	
	//3***** 远程信息交互状态码
	
	//4***** 数据库状态码
	
	//5***** 
	
	//9***** 工具类状态码
	
	//901*** 文件类状态码
	FILE_EXCEPTION("901400","文件工具类异常"),
	
	//90140* ~ 90144*	找文件型异常
	FILE_READ_EXCEPTION("901401","读取文件内容发生未知错误"),
	FILE_READ_UNFOUNT("901402","文件未找到"),
	//90145* ~ 90149* 写文件型异常
	FILE_WRITE_EXCEPTION("901450","写入文件内容发生未知错误"),
	
	//902*** 日期类状态码
	DATE_EXCEPTION("902400","日期工具类异常"),
	
	//903*** 字符串类状态码
	
	//904*** 数字类状态码
	;
	

	
	

	;
	
	private String code;  //响应码
	private String desc;   //响应描述
	private CodeDescConstants(String code,String desc){
		this.code=code;
		this.desc=desc;
	}
	public String getCode(){
		return code;
	}
	public String getDesc(){
		return desc;
	}

}
