tags:

views:

65

answers:

4
while (tokens.hasMoreTokens()) 
{
    keyword = tokens.nextToken();
    System.out.println("File= "+fileid+" Keyword=" + keyword);
    stmt.executeUpdate(
        "INSERT into TEXTVALUEINVERTEDINDEX " + "(FILEID, KEYWORD) values ('"
        + fileid + "', '" + keyword + "')"
    );      
}

This is the loop in which I'm updating the rows. The problem I'm facing is that when i run this only 1 value gets updated and when I comment the stmt.executeUpdate() line it displays all the possible entries in the database.

A: 

Your code should work. Make sure the sentence is not throwing any Exceptions when running by surrounding it with a try/catch block:

try {
    stmt.executeUpdate("INSERT into TEXTVALUEINVERTEDINDEX " + 
         "(FILEID, KEYWORD) "+"values ('"+fileid+"', '"+keyword+"')"); 
} catch (SQLException e) {
    e.printStackTrace();
}

You should also consider using a PreparedStament instead since its use is very appropriate for your described scenario:

Something like this:

String sql = "insert into textvalueinvertedindex (fileid, keyword) values (?,?)";
PreparedStatement pstmt = conn.prepareStatement(sql);
while (tokes.hasMoreTokens()) {
    keywords = tokens.nextToken();
    pstmt.setString(1, fileid);
    pstmt.setString(2, keyword);
    pstmt.executeUpdate();
}
pstmt.close();
Pablo Santa Cruz
Actually i pasted only the loop code..the try catch block is present there is no exception being thrown
gaurav
+1  A: 

If you want all updates to be applied at once you can use batch execution, here is an example

jutky
page not loading.
gaurav
strange, it work for me. anyway just google for "java jdbc addBatch" and pick any page you like.
jutky
+2  A: 

You need to use preparedStatements...

PreparedStatement pStmt = connection.prepareStatement("INSERT into TEXTVALUEINVERTEDINDEX (FILEID, KEYWORD) values(?,?)");
while (tokens.hasMoreTokens()) 
    {
        keyword = tokens.nextToken();
        System.out.println("File= "+fileid+"    Keyword="+keyword);

        pStmt.setString(1, fileid); //This might be pStmt.SetInt(0, fileid) depending on teh type of fileid)
        pStmt.setString(2, keyword);

        pStmt.executeUpdate();
    }

then using this you can extend to us batch update...

PreparedStatement pStmt = connection.prepareStatement("INSERT into TEXTVALUEINVERTEDINDEX (FILEID, KEYWORD) values(?,?)");
    while (tokens.hasMoreTokens()) 
        {
            keyword = tokens.nextToken();
            System.out.println("File= "+fileid+"    Keyword="+keyword);

            pStmt.setString(1, fileid); //This might be pStmt.SetInt(0, fileid) depending on teh type of fileid)
            pStmt.setString(2, keyword);

            pStmt.addBatch();
        }
pStmt.executeBatch();

Not sure why your code isn't working though - but this will probably help in the long run...

Matt Fellows
pStmt.setString(0, fileid);here its giving an exception column not found.the fileid ive declared as String fileid
gaurav
We have to give pStmt.setString(1, fileid);pStmt.setString(2, keyword);
gaurav
Edited the original - this is the problem with working with multplie languages, I was getting mixed up with .Net which is 0 indexed...
Matt Fellows
A: 

Your date range and filter selection contained no results.

mory