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

📄 companysize.java

📁 利用多线程从搜索引擎下载网页并提取数据到数据库。
💻 JAVA
字号:
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;


public class CompanySize {

	public int id;

	public String name;

	static Connection cnn;
	
	static int dbCount;
	static int maxDbCount;
	static {
		fillBuffer();
	}
	private static HashMap<String, Integer> map;

	private static SQLException e;

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		try {
		 System.out.println(	new CompanySize("10,001 or more employees").getID());
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	private static void fillBuffer() {
		try {
			cnn = DataAccess.getConnection();
			map = new HashMap<String, Integer>();
			PreparedStatement stmt;
			stmt = cnn.prepareStatement("select * from company_sizes");
			ResultSet rs = stmt.executeQuery();
			while (rs.next()) {
				map.put(rs.getString("name"), new Integer(rs.getInt("id")));
			}
		} catch (SQLException e1) {
			e = e1;
		}
	}

	
	public CompanySize(String sizeType) throws SQLException {
		this.name = sizeType;
		cnn = DataAccess.getConnection();
		if (e != null)
			throw e;	
	}

	private int getIDLocal() {
		if (map.containsKey(this.name))
			return map.get(this.name).intValue();
		else
			return 0;
	}
	
	public int getID() throws SQLException {
		if (name == null || name == "")
			return 0;
		if (id != 0)
			return id;
		id=getIDLocal();
		if (id != 0)
			return id;
		if (!populateFromDB())
			insert();
		populateFromDB();
		map.put(name, id);
		return id;
	}

	public static synchronized boolean exists(String companySize) throws SQLException
	{cnn = DataAccess.getConnection();
		return new CompanySize(companySize).populateFromDB();
	}
	
	public boolean populateFromDB() throws SQLException {
		PreparedStatement stmt;
			stmt = cnn
					.prepareStatement("select id from company_sizes where name=?");
			stmt.setString(1, name);
			ResultSet rs = stmt.executeQuery();
			if (rs.next()) {
				this.id = rs.getInt("id");
				return true;
			} else
				return false;
	}

	public void insert() {
		PreparedStatement stmt;
		try {
			stmt = cnn
					.prepareStatement("insert into company_sizes(name) select ?");
			stmt.setString(1, name);
			stmt.execute();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

}

⌨️ 快捷键说明

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