我得到以下错误:
java.lang.NullPointerException
at oracle.jdbc.driver.ScrollableResultSet.cacheRowAt(ScrollableResultSet.java:2086)
at oracle.jdbc.driver.ScrollableResultSet.isValidRow(ScrollableResultSet.java:2060)
at oracle.jdbc.driver.ScrollableResultSet.next(ScrollableResultSet.java:347)
at website.web.InboxReader.getkeywordImportance(InboxReader.java:832)
at website.web.InboxReader.main(InboxReader.java:54)
Mail
表中有53
行,keyword
表中有1
行。在调试时,一旦执行了kstmt.executeUpdate("UPDATE KEYWORD SET IMPORTANCE = IMPORTANCE + 1.0 WHERE SKEYWORD = '" + s2 + "'");
行,它就会再次转到keyword set.next()
并抛出异常。
这是代码:
Connection connection = connectToDatabase();
Statement mstmt = connection.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE);
String s1,s2;
ResultSet mailset = mstmt.executeQuery("SELECT * from MAIL");
System.out.println("hello in getImportance beg");
//mailset.beforeFirst();
while(mailset.next())
{
System.out.println("hello in first while");
Statement kstmt = connection.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE);
ResultSet keywordset = kstmt.executeQuery("SELECT * FROM KEYWORD");
while(keywordset.next())
{
s1=mailset.getString("SUBJECT");
System.out.println("Subject: "+s1);
s2=keywordset.getString("SKEYWORD");
System.out.println("Keyword: "+s2);
if(s1.contains(s2))
{
System.out.println("hello in if");
kstmt.executeUpdate("UPDATE KEYWORD SET IMPORTANCE = IMPORTANCE + 1.0 WHERE SKEYWORD = '" + s2 + "'");
}
}
keywordset.close();
}
mailset.close();
connection.close();
谢谢!
如果你看一下这行似乎有问题:
kstmt.executeUpdate("UPDATE KEYWORD SET IMPORTANCE ='" + (keywordset.getFloat("IMPORTANCE") + 1.0) + "'");
你应该仔细检查通话
keywordset.getFloat("IMPORTANCE")
这很可能是由于记录的Importance列为空,或者该列可能不存在。只是为了调试你尝试
keywordset.getFloat(0);
或者您知道某个列的哪个id存在,以查看调用是否有效。
您是否尝试删除COMMIT语句?因为通常情况下,除非您另行指定,否则数据将自动提交,因此无需在语句后调用提交。如果您已将auto_commit指定为false,则要提交,您应该执行con.commit()
,而不是通过更新语句
请参阅此处的文档http://download.oracle.com/javase/tutorial/jdbc/basics/retrieving.html
之前没必要打电话。首先,正如你所期望的那样,下一个会表现得很好。
public static void viewTable(Connection con) throws SQLException {
Statement stmt = null;
String query = "select COF_NAME, SUP_ID, PRICE, SALES, TOTAL from " + dbName + ".COFFEES";
try {
stmt = con.createStatement();
ResultSet rs = stmt.executeQuery(query);
while (rs.next()) {
String coffeeName = rs.getString("COF_NAME");
int supplierID = rs.getInt("SUP_ID");
float price = rs.getFloat("PRICE");
int sales = rs.getInt("SALES");
int total = rs.getInt("TOTAL");
System.out.println(coffeeName + "t" + supplierID + "t" + price + "t" + sales + "t" + total);
}
} catch (SQLException e ) {
JDBCTutorialUtilities.printSQLException(e);
} finally {
if (stmt != null) { stmt.close(); }
}
}
您是否尝试过在没有关键字wordset.foreFirst((和mailset.foreFirst((的情况下运行它?这不是必需的,它可能会在ResultSet中产生问题。
kstmt.executeUpdate("UPDATE KEYWORD SET IMPORTANCE = IMPORTANCE + 1.0 WHERE SKEYWORD = '" + s2 + "'");
当这一行在代码中执行时,您的关键字集ResultSet
对象将发生更改,因为所有执行方法都会对ResultSet
产生影响。Statement接口中的所有执行方法都会隐式关闭语句的当前ResultSet对象(如果存在打开的对象(。您要做的是创建一个单独的语句对象来运行此查询,这样关键字集ResultSet
就不会受到影响……:(