0

In connection with another programming project, I am prototyping a JDBC project (with Netbeans, Java, and a Derby database). My program needs to iteratively update all the rows in a database table as follows:

  1. There are three columns in the table: famousName, famousQuote, hashKey.

  2. Originally, the famousQuote column contains a verbatim quote. I want to go down that column using a while loop, get the checksum of the ascii letters, bitwise "AND" with the hashKey value, and then replace the verbatim quote with an "encrypted" value.

As of right now, I try to extract the verbatim famousQuote using a ResultSet object, perform necessary encryption, and then an SQL statement that updates the value. All of this takes place in a while(rs.next()) loop as follows:

(pseudo-code):  //all necessary database connections and variable declared here.
String sqlStatement = "Select * FROM mainTable ORDER BY famousName";
ResultSet rs = stmt.executeQuery(sqlStatement);

while(rs.next()){
     tempString1 = rs.getString("famousQuote");
     tempString2 = rs.getString("hashKey");
     tempString3 = EncryptionAlgorithm.EncryptStatement(tempString1, tempString2);

     sqlStatement = "UPDATE maintable SET famousQuote=tempString3 WHERE hashKey=tempString2"; 
     (note, there is a bit of pseudo-code regarding the WHERE part, 
     but I'm sure it's immaterial to the error message I'm getting.)

     stmt.executeUpdate(sqlStatement);
}

This seemed like a good idea until the program started throwing errors such as:

ResultSet not open.  Operation 'next' not permitted.  Verify that autoCommit is off

I later read in the documentation (concerning ResultSet):

A ResultSet object is automatically closed when the Statement object that generated it 
is closed, re-executed, or used to retrieve the next result from a sequence of multiple results.

Lastly, I guess I could try moving the ResultSet rs declaration inside the while loop, so that it would instantiate a new ResultSet object, but I'm pretty sure this would lose my place in the database (re-updating the first row perpetually).

I'm now at a standstill on how to iterate my way down the table rows, executing my EncryptionAlgorithm on each quote, and then updating the column values in place.

0

1 Answer 1

0

I apologize in advance if my JDBC is a little rusty, but something like this might do the trick:

// 'conn' is your JDBC connection
Statement stmt = conn.createStatement();
PreparedStatement update = conn.prepareStatement(
    "UPDATE maintable SET famousQuote=tempString3 WHERE hashKey=tempString2";    
String sqlStatement = "Select * FROM mainTable ORDER BY famousName";
ResultSet rs = stmt.executeQuery(sqlStatement);
while(rs.next()){
     tempString1 = rs.getString("famousQuote");
     tempString2 = rs.getString("hashKey");
     tempString3 = EncryptionAlgorithm.EncryptStatement(tempString1, tempString2);
     update.setObject(1, tempString3);
     update.setObject(2, tempString2);
     update.executeUpdate(sqlStatement);
}
2
  • This code doesn't work exactly as is, but is certainly close enough to what I need that I can finally make some progress now! Thank you so much for taking the time to answer my question about this very advanced and niche topic!
    – the_photon
    Commented Jun 19, 2016 at 3:56
  • @the_photon This is not really an advanced subject, it is basic JDBC, and it is explained (including a solution) in the javadoc. See also the duplicate question. Commented Jun 19, 2016 at 8:06

Not the answer you're looking for? Browse other questions tagged or ask your own question.