自己写的爬虫程序,把url保存到数据库三个表中  :tocraw,保存已经搜索过但还没爬过的表,crawled 保存已经爬过的表,result 保存包含指定字符处 searchString的网页的url,错误如下:java.sql.SQLException: [Microsoft][SQLServer 2000 Driver for JDBC]Row update failed.
at com.microsoft.jdbc.base.BaseExceptions.createException(Unknown Source)
at com.microsoft.jdbc.base.BaseExceptions.getException(Unknown Source)
at com.microsoft.jdbc.base.BaseImplUpdatableResultSet.executeStatement(Unknown Source)
at com.microsoft.jdbc.base.BaseImplUpdatableResultSet.deleteRow(Unknown Source)
at com.microsoft.jdbc.base.BaseResultSet.deleteRow(Unknown Source)
at crawler.SearchCrawler.crawl(SearchCrawler.java:378)
at crawler.SearchCrawler.run(SearchCrawler.java:88)
at java.lang.Thread.run(Thread.java:619)
378行为出错行,已用红色标出,代码如下:public synchronized void crawl(String startUrl, String searchString, boolean limithost,
boolean caseSensitive) throws SQLException { URL verifiedUrl;
String pageContents;
ArrayList<String> links;
String sql;
String s;
String str;
ResultSet rs;
Statement st = getConnection().createStatement(
ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
if (searchString.length() < 1) {
System.out.println("Missing search String");
System.exit(0);
}
// 从开始URL中移出www
startUrl = removeWwwFromUrl(startUrl);
// 已经搜索到的url放入tocrawl表
sql = "insert into tocrawl(tocrawl) values('" + startUrl + "')";
st.executeUpdate(sql);
rs = st.executeQuery("select * from tocrawl");
while (rs.next()) { s = rs.getString(1);
verifiedUrl = verifyUrl(s);
rs.deleteRow(); getConnection().commit();
if (!isRobotAllowed(verifiedUrl)) {
rs.beforeFirst();
continue;
}
// 爬过的url放入crawled表
Statement st2 = getConnection()
.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,
ResultSet.CONCUR_UPDATABLE);
sql = "insert into crawled(crawled) values('" + s + "')";
st2.executeUpdate(sql);
st2.close();

pageContents = downloadPage(verifiedUrl); if (pageContents != null && pageContents.length() > 0) {
links = retrieveLinks(verifiedUrl, pageContents, limitHost); Iterator<String> iter = links.iterator();
Statement st3=getConnection().createStatement(
ResultSet.TYPE_SCROLL_SENSITIVE,
ResultSet.CONCUR_UPDATABLE);
while (iter.hasNext()) {
st3.addBatch("insert into tocrawl(tocrawl) values('" + iter.next() + "')");

}
try{
st3.executeBatch();
}catch(Exception e){getConnection().rollback(); }
st3.close();
// 包含指定字符串的url放入result表
if (searchStringMatches(pageContents, searchString,
caseSensitive)) {
Statement st4 = getConnection().createStatement(
ResultSet.TYPE_SCROLL_SENSITIVE,
ResultSet.CONCUR_UPDATABLE);
sql = "insert into result(result) values('" + s + "')";
st4.executeUpdate(sql);
st4.close();

}
} rs.beforeFirst();
}
rs.close();
st.close();
getConnection().close(); }

解决方案 »

  1.   

    select * from tocrawl改成select * from tocrawl for update???
      

  2.   

    楼上兄弟能不能说下具体怎么改,改为select * from tocrawl for update后出现
    [Microsoft][SQLServer 2000 Driver for JDBC][SQLServer]第 1 行: FOR UPDATE 子句仅允许用于 DECLARE CURSOR。
      

  3.   

    换一个驱动试一试,jtds比微软的jdbc驱动好用很多。