⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 urlaction.java

📁 本系统是一个跨平台的搜索引擎
💻 JAVA
字号:
package com.briup;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.text.SimpleDateFormat;
import java.util.Date;
import org.htmlparser.Parser;
import org.htmlparser.filters.NodeClassFilter;
import org.htmlparser.tags.LinkTag;
import org.htmlparser.util.NodeList;


public class URLAction {
	public void buildURL(String url, boolean condition) {
		try {
			String urls = url.replaceAll("'", "");
			boolean hava = getURLByURL(urls);
			if (!hava) {
				insertURLLocal(urls);
				Parser parser = new Parser();
				while (!urls.equals("")) {
					try {
						parser.setURL(urls);
						NodeList nodeList = parser.parse(new NodeClassFilter(
								LinkTag.class));
						if (nodeList != null && nodeList.size() > 0) {
							for (int i = 0; i < nodeList.size(); i++) {
								urls = ((LinkTag) nodeList.elementAt(i))
										.extractLink().replaceAll("'", "");
								if (!urls.equals("")) {
									if (condition) {
										if (urls.indexOf(url) != -1)
											insertURLPRE(urls);
									} else
										insertURLPRE(urls);
								}
							}
						}
					} catch (Exception e) {
						System.out.println("无效URL:" + urls);
					}
					urls = getPreURL();
					if (!urls.equals(""))
						insertURLLocal(urls);
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public boolean getURLByURL(String url) {
		StringBuffer sqlBuffer = new StringBuffer(
				"select url from search_pre where url='");
		sqlBuffer.append(url);
		sqlBuffer.append("' ");
		sqlBuffer
				.append(" UNION select url from search_local where url='");
		sqlBuffer.append(url).append("'");
		Statement state = null;
		Connection con = null;
		ResultSet result = null;
		boolean have = false;
		try {
			//con = JDBCUtil.getConnection();
			con = JDBCConnectionFactory.getConnection();
			state = con.createStatement();
			result = state.executeQuery(sqlBuffer.toString());
			while (result.next()) {
				have = true;
				break;
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			closeStatement(state, con, result);
		}
		return have;
	}

	public void insertURLPRE(String url) {
		
		Statement state = null;
		Connection con = null;
		try {
			boolean hava = getURLByURL(url);
			if (!hava) {
				//con = JDBCUtil.getConnection();
				con = JDBCConnectionFactory.getConnection();
				StringBuffer sqlBuffer = new StringBuffer(
				"insert into search_pre(url) values (");
				int oid = HL.nextVal_user(con);
				HL.base(con, "user");
				sqlBuffer.append(oid).append(",'").append(url);
				sqlBuffer.append("')");
				System.out.println("insertURLPRE"+sqlBuffer);
				con.setAutoCommit(false);
				state = con.createStatement();
				state.executeUpdate(sqlBuffer.toString());
				con.commit();
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			closeStatement(state, con, null);
		}

	}

	public void deletURL(String url) {
		String deleteLocalUrl = "delete from search_local where url='"
				+ url + "'";
		String deletePreURL = "delete from search_pre where url='" + url
				+ "'";
		Statement state = null;
		Connection con = null;
		try {
			//con = JDBCUtil.getConnection();
			con = JDBCConnectionFactory.getConnection();
			con.setAutoCommit(false);
			state = con.createStatement();
			state.executeUpdate(deleteLocalUrl);
			state.executeUpdate(deletePreURL);
			con.commit();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			closeStatement(state, con, null);
		}

	}

	public void insertURLLocal(String url) {
		System.out.println(url);
		
		Statement state = null;
		Connection con = null;
		try {
			//con = JDBCUtil.getConnection();
			con = JDBCConnectionFactory.getConnection();
			StringBuffer sqlBuffer = new StringBuffer(
			"insert into search_local (id,url,CreateDate,visite) values (");
			int oid = HL.nextVal_user(con);
			HL.base(con, "user");
			sqlBuffer.append(oid).append(",'").append(url);
			SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
			String date = format.format(new Date());
			sqlBuffer.append("','").append(date).append("','N')");
			System.out.println(sqlBuffer);
			StringBuffer sql = new StringBuffer(
					"delete from search_pre where url='");
			sql.append(url).append("'");
			con.setAutoCommit(false);
			state = con.createStatement();
			state.executeUpdate(sql.toString());
			state.executeUpdate(sqlBuffer.toString());
			con.commit();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			closeStatement(state, con, null);
		}
	}

	public String getIndexURL() {
		String sql1 = "select id,url from search_local where visite='N' ";
		String sql2 = "update search_local set visite='Y'where id=";
		Statement state = null;
		Connection con = null;
		ResultSet result = null;
		boolean have = false;
		int id = 0;
		String url = "";
		try {
			//con = JDBCUtil.getConnection();
			con = JDBCConnectionFactory.getConnection();
			con.setAutoCommit(false);
			state = con.createStatement();
			result = state.executeQuery(sql1);
			while (result.next()) {
				id = result.getInt("id");
				url = result.getString("url");
				break;
			}
			sql2 = sql2 + id;
			state.executeUpdate(sql2);
			con.commit();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			closeStatement(state, con, result);
		}
		return url;
	}

	public String getPreURL() {
		String sql = "select url from search_pre";
		Statement state = null;
		Connection con = null;
		ResultSet result = null;
		String url = "";
		try {
			//con = JDBCUtil.getConnection();
			con = JDBCConnectionFactory.getConnection();
			state = con.createStatement();
			result = state.executeQuery(sql);
			while (result.next()) {
				url = result.getString("url");
				break;
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			closeStatement(state, con, result);
		}
		return url;
	}

	public void closeStatement(Statement state, Connection con,
			ResultSet resultSet) {
		try {
			if (resultSet != null)
				resultSet.close();
			if (state != null)
				state.close();
			if (con != null)
				con.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

	public void delAllURL() {
		// 摧毁性的重性建设搜索DROP TABLE IF EXISTS `search_local`;
		String deleteLocalUrl = "DROP TABLE search_local";
		String createLocalUrl = "CREATE TABLE search_pre (id number(11) primary key," +
				"url varchar2(250) default NULL," +
				"CreateDate date  ," +
				"visite varchar2(1))";

		String deletePreURL = "DROP TABLE search_pre";
		String createPreURL = "CREATE TABLE search_local (id number primary key," +
				"url varchar2(250) ," +
				"CreateDate date," +
				"visite varchar2(1))";
		Statement state = null;
		Connection con = null;
		try {
			// con = JDBCUtil.getConnection();
			con = JDBCConnectionFactory.getConnection();
			con.setAutoCommit(false);
			state = con.createStatement();
			state.executeUpdate(deleteLocalUrl);
			state.executeUpdate(createLocalUrl);
			state.executeUpdate(deletePreURL);
			state.executeUpdate(createPreURL);
			con.commit();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			closeStatement(state, con, null);
		}
	}

	public static void main(String args[]) {
		URLAction action = new URLAction();
		//action.delAllURL();
		action.buildURL("http://www.qwserv.com", true);
		action.insertURLPRE("http://www.qwserv.com");
		// action.buildURL("http://www.51job.com",true);//insertURLLocal("http://www.qwserv.com");
		// String url = action.getIndexURL();
		// sSystem.out.println("url:" + url);
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -