[英]Exception in thread “main” java.util.NoSuchElementException?
i am trying to extract value from web site and inserting value in my db when i am going to insert data in my db i am getting exeception 当我要在数据库中插入数据时,我试图从网站中提取值并在数据库中插入值。
Exception is : Exception in thread "main" java.util.NoSuchElementException 异常是:线程“ main”中的异常java.util.NoSuchElementException
here is my code 这是我的代码
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();
}
}
updated exception 更新的例外
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
What am I missing here? 我在这里想念什么?
How can i remove this exception 我如何删除此例外
Thanks in advance 提前致谢
I guess your problem is not the db insert. 我猜你的问题不是数据库插入。 You are using
StringTokenizer.nextElement()
without testig if a next element is available. 如果下一个元素可用,则使用不带testig的
StringTokenizer.nextElement()
。
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();
If you do multiple nextElement()
calls in a row you should first check if the expected token count is available by using StringTokenizer.countTokens()
如果您连续执行多个
nextElement()
调用,则应首先使用StringTokenizer.countTokens()
检查预期的令牌计数是否可用
public int countTokens()
public int countTokens()
Calculates the number of times that this tokenizer's nextToken method can be called > before it generates an exception.
计算此令牌生成器的nextToken方法在生成异常之前可以被调用的次数。 The current position is not advanced.
当前位置未提前。
Returns: the number of tokens remaining in the string using the current delimiter set.
返回:使用当前定界符集保留在字符串中的令牌总数。 See Also: nextToken()
另请参见:nextToken()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.