www.pudn.com > spider(java).rar > Spider.java, change:2004-09-05,size:3785b


/** 
 * @author Isart MontanÚ Mogas 
 * 
  */ 
 
 
import java.sql.*; 
 
public class Spider { 
 
	static Statement stmt = null;  
	static ResultSet rs = null;  
	static Integer maxRows; 
	static int MAX_THREADS=1; 
	static Connection conn; 
 
	public static void main(String[] args)  
	{ 
		try  
		{  
			Class.forName("com.mysql.jdbc.Driver").newInstance(); 
			 
			if(args.length!=0) 
				inicialitza(new String(args[0])); 
			else 
				inicialitza(""); 
			contaQuantsQueden(); 
	 
			while(maxRows.intValue()>0) 
			{ 
				concurrencia conc = new concurrencia(); 
				SpiderBaixaURL url1= new SpiderBaixaURL(conc,conn,stmt); 
				//SpiderBaixaURL url1= new SpiderBaixaURL(vectorPerVeure,vectorVistos); 
				url1.start(); 
		 
				//lenšem 5 threads mÚs 
				int n_threads=maxRows.intValue()-1; 
				 
				if(n_threads>MAX_THREADS) 
					n_threads=MAX_THREADS; 
				 
				while((n_threads>1)&&(maxRows.intValue()>0)) 
				{			 
					url1 = new SpiderBaixaURL(conc,conn,stmt); 
					url1.sleep(5000); 
					url1.start(); 
					n_threads--; 
				} 
			 
				url1.join(); 
				contaQuantsQueden(); 
			}		 
 
			 
 
		} 
		catch (Exception ex) 
		{  
				 
			ex.printStackTrace(); 
		} 
		finally 
			{  
				// it is a good idea to release 
				// resources in a finally{} block  
				// in reverse-order of their creation  
				// if they are no-longer needed  
 
				if (rs != null)  
				{  
					try  
					{ 
						rs.close();  
					} 
					catch (SQLException sqlEx)  
					{ // ignore  
					}  
 
					rs = null;  
				} 
 
				if (stmt != null) 
				{  
					try  
					{  
						stmt.close();  
					} 
					 catch (SQLException sqlEx)  
					 { 
						// ignore  
					 }  
 
					stmt = null;  
				}  
			} 
 
	} 
	 
	 
	//If we have allready visited the web /url/ we don't put it into the database 
	public static void inicialitza(String url) 
	{ 
		try 
		{ 
	 
			conn = DriverManager.getConnection("jdbc:mysql://192.168.1.2/spider?user=spider&password=spider"); 
 
			stmt = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);  
 
			 
		//	rs=stmt.executeQuery("SELECT * FROM perVeure WHERE url='"+url+"'"); 
			 
			if((countItems(stmt.executeQuery("SELECT * FROM perVeure WHERE url='"+url+"'"))==0) 
					&&(countItems(stmt.executeQuery("SELECT * FROM vistes WHERE url='"+url+"'"))==0)&&(url!="")) 
			{ 
					 
			//		rs=stmt.executeQuery("SELECT * FROM perVeure") 
				     
				//	if(url.length()!=0) 
				//{ 
				//rs.last(); 
				stmt.execute("insert into perVeure (url) values ('"+url+"')"); 
				//rs.moveToInsertRow(); 
				//rs.updateString(2, url); 
				//rs.insertRow(); 
				//rs.moveToCurrentRow(); 
					//}		 
				//rs.close(); 
			} 
		} 
		catch(SQLException ex)  
		{ 
			   // handle any errors  
			   System.out.println("SQLException: " + ex.getMessage());  
			   System.out.println("SQLState: " + ex.getSQLState());  
			   System.out.println("VendorError: " + ex.getErrorCode());  
		} 
	 
	} 
	 
	public static void contaQuantsQueden() 
	{ 
		try 
		{ 
			ResultSet rs = stmt.executeQuery("SELECT * FROM perVeure"); 
			maxRows=new Integer(countItems(rs)); 
	/*		int i=0; 
			 while (rs.next())  
			 { 
					 i++;	 
			 } 
			maxRows=new Integer(i);*/ 
			rs.close(); 
		} 
		catch(SQLException ex)  
		{ 
			   // handle any errors  
			   System.out.println("SQLException: " + ex.getMessage());  
			   System.out.println("SQLState: " + ex.getSQLState());  
			   System.out.println("VendorError: " + ex.getErrorCode());  
		} 
 
	} 
	 
	public static int countItems(ResultSet rs) 
	{ 
		int i=0; 
		try{ 
		 
		while(rs.next()) 
			i++; 
				 
		} 
		catch(Exception e) 
		{ 
			System.out.println("ERROR: while counting items"); 
			e.printStackTrace(); 
			 
		 
		} 
		return i; 
	} 
	 
}