2014-04-04 40 views
0

我正在試圖從網站價值,在我的數據庫插入值,當我要在我的分貝我得到exeception線程「main」中的異常java.util.NoSuchElementException?

例外是插入數據:異常線程「main」 java.util.NoSuchElementException

這裏是我的代碼

public class ScrapCom { 
    Statement st = null; 
    Connection cn = null; 
    public static void main(String args[]) throws InterruptedException, ClassNotFoundException, SQLException { 
     int j = 0; 
     WebDriver driver = new HtmlUnitDriver(BrowserVersion.getDefault()); 
     String sDate = "27/03/2014"; 
     String url = "http://www.upmandiparishad.in/commodityWiseAll.aspx"; 
     driver.get(url); 
     Thread.sleep(5000); 
     new Select(driver.findElement(By.id("ctl00_ContentPlaceHolder1_ddl_commodity"))).selectByVisibleText("Jo"); 
     driver.findElement(By.id("ctl00_ContentPlaceHolder1_txt_rate")).sendKeys(sDate); 
     Thread.sleep(3000); 
     driver.findElement(By.id("ctl00_ContentPlaceHolder1_btn_show")).click(); 
     Thread.sleep(5000); 
     WebElement findElement = driver.findElement(By.id("ctl00_ContentPlaceHolder1_GridView1")); 
     String htmlTableText = findElement.getText(); 
     // do whatever you want now, This is raw table values. 
     htmlTableText = htmlTableText.replace("S.No.DistrictMarketPrice", ""); 
     htmlTableText = htmlTableText.replaceAll("\\s(\\d+\\s[A-Z])", "\n$1"); 
     htmlTableText = htmlTableText.replaceAll("(?=(.*?[ ]){4,}).*?[\n\r]", ""); 
     System.out.println(htmlTableText); 
     StringTokenizer str = new StringTokenizer(htmlTableText); 
     while (str.hasMoreElements()) { 
      for (int i = 0; i < 4; i++) { 
       String no = str.nextElement().toString(); 
       String city = str.nextElement().toString(); 
       String mandi = str.nextElement().toString(); 
       String price = str.nextElement().toString(); 
       Class.forName("com.mysql.jdbc.Driver"); 
       Connection cn = DriverManager.getConnection("jdbc:mysql://localhost:3306/mandi", "root", ""); 
       //insert them into the database 
       PreparedStatement ps = cn.prepareStatement("insert into commoditywise values(?,?,?,?)"); 
       ps.setString(1, no); 
       ps.setString(2, city); 
       ps.setString(3, mandi); 
       ps.setString(4, price); 
       j = ps.executeUpdate(); 
       cn.close(); 
      } 
     } 
     if (j == 1) { 
      System.out.println("data inserted"); 
     } else { 
      System.out.println("not inserted"); 
     } 
     driver.close(); 
     driver.quit(); 
    } 
} 

更新例外

Exception in thread "main" java.util.NoSuchElementException 
    at java.util.StringTokenizer.nextToken(StringTokenizer.java:349) 
    at java.util.StringTokenizer.nextElement(StringTokenizer.java:407) 
    at Getdata1.main(Getdata1.java:48) 
Java Result: 1 

缺少什麼我在這裏?

如何刪除這個異常提前

+0

將異常堆棧追蹤添加到您的問題。 – MockerTim

+0

對不起,我沒有得到你的意思 – user3493831

+0

意思是當你運行你的程序時,在控制檯上打印的所有異常數據,不僅僅是你指定的單行「Exception in thread」main「java.util.NoSuchElementException」。 – MockerTim

回答

1

感謝我猜你的問題不是分貝插入。如果下一個元素可用,則您正在使用​​而不帶testig。

while (str.hasMoreElements()) { 
    for (int i = 0; i < 4; i++) { 
     String no = str.nextElement().toString(); 
     String city = str.nextElement().toString(); 
     String mandi = str.nextElement().toString(); 
     String price = str.nextElement().toString(); 

如果你做多的nextElement()電話一排,你應該先檢查是否預期的令牌計數可通過使用StringTokenizer.countTokens()

公衆詮釋countTokens()

計算的次數該標記器的nextToken方法在生成異常之前可以調用它>。當前位置不提前。

返回: 使用當前分隔符集保留在字符串中的令牌數。 另請參閱: nextToken()

+0

+1似乎OP不需要niether while,也不需要for循環。在每個nextElement()調用之前,OP只需檢查'str'是否有下一個元素。 – MockerTim

相關問題