I am trying to update a column in already exists MySQL table. The values must be read from a text file, and then they must be inserted into the specified column.
I have tried the following:
int counter=0;
while((fileLine=in.readLine())!=null)
{
System.out.println("Line read is: "+fileLine);
//execute db insertion
try {
//insert in the database
Query= "update db.table set col4=?"; //database
preparedStmt3 = DBConnection.con.prepareStatement(Query);
preparedStmt3.setString (1, fileLine);
preparedStmt3.executeUpdate();
System.out.println("Complete update statement for row: "+counter);
counter++;
} catch (Exception e) {
System.out.println("DB_Error:_"+ e.toString());
}
} //end while loop
I have tried to output fileLine
value and it seems correct and change correctly with every loop round. But, after I run the program and check the database, I find that the value inserted is the last line only (and this repeated in all records, it is not what I am supposed to do, which is insert every line in records consequently). what is the cause for this problem ?
EDIT: The text file contains the following lines: aaa bbb ccc ddd eee fff
The run output after I added some printline
statements to see debug is as the following:
while the DB contains the last line inserted only