I am writing this program in which I am using preparedStatements
to make changes to an SQL Database. However, the UPDATE
query is not working.
Here is the code:
package financials; import java.net.URL; import java.util.ResourceBundle; import java.sql.*; public void initialize(URL url, ResourceBundle rb) { try{ Class.forName("com.mysql.cj.jdbc.Driver"); con=DriverManager.getConnection("jdbc:mysql://localhost/finances","root","P@ssword"); con.setAutoCommit(true);// TODO } catch(Exception ae) { System.out.println("Error in connection !"); } @FXML private void SaveOrAdd(ActionEvent event) { //This is button which on click executes the following code String Action=save.getText(); if(Action.equals("Add Account")) { String SBNumber=LinkedSB.getText(); String newAccountType=AccountTypeF.getText(); String newFHolder=FHolderF.getText(); String newSHolder=SHolderF.getText(); String newTHolder=THolderF.getText(); String Bankcode=BankCodeF.getText(); if(newAccountType.equals("")||newFHolder.equals("")||newSHolder.equals("")||newTHolder.equals("")) { update.setText("Please fill in all the fields !"); } else { try { PreparedStatement pst=con.prepareStatement("INSERT INTO banklines (Bank_Code,Linked_SB_Account,Sb_Account_Type,First_Holder,Second_Holder,Third_Holder) VALUES (?,?,?,?,?,?)"); pst.setString(1,Bankcode); pst.setString(2,SBNumber); pst.setString(3,newAccountType); pst.setString(4,newFHolder); pst.setString(5,newSHolder); pst.setString(6,newTHolder); int a=pst.executeUpdate(); System.out.println(a); //This returns a 1 } catch(Exception ae) { update.setText("Update Failed !"); } }} else { String SBNumber=LinkedSB.getText(); String newAccountType=AccountTypeF.getText(); String newFHolder=FHolderF.getText(); String newSHolder=SHolderF.getText(); String newTHolder=THolderF.getText(); String Bankcode=BankCodeF.getText(); if(newAccountType.equals("")||newFHolder.equals("")||newSHolder.equals("")||newTHolder.equals("")) { update.setText("Please fill in all the fields !"); } else //This is the block in concern { try { //Here is where the issue starts ! PreparedStatement pst2=con.prepareStatement("UPDATE banklines SET Sb_Account_Type=?,First_Holder=?,Second_Holder=?,Third_Holder=? WHERE Linked_SB_Account=? AND Bank_Code=?"); pst2.setString(1,newAccountType); pst2.setString(2,newFHolder); pst2.setString(3,newSHolder); pst2.setString(4,newTHolder); pst2.setString(5,SBNumber); pst2.setString(6,Bankcode); pst2.executeUpdate(); int a=pst2.executeUpdate(); System.out.println(a); //This returns a 0 update.setText("Successfully Updated !"); } catch(Exception ae) { update.setText("Update Failed !"); } } } }
The problem is that no error is being thrown, that is, the output is always Successfully Updated
. However, the changes are not being reflected on the database. I have tried executing the query UPDATE banklines SET Sb_Account_Type=?,First_Holder=?,Second_Holder=?,Third_Holder=? WHERE Linked_SB_Account=? AND Bank_Code=?
separately as a query in mySQL workbench, and it returns no error. I have also ensured that no variable is left blank. In-spite of all this, the update is not working. What confused me even more is that the previous query in the if-else
block, that is the INSERT
query works perfectly, and the results are updated in the database as well.
I am using NetBeans 8.2 with jdk 1.8 and mysql-connector-java-8.0.21.
P.S. I have stuck to java naming conventions to the best of my knowledge, ensuring that I follow CamelCase
notation wherever I could. Please edit my code or suggest changes if you feel that anything is wrong.
Advertisement
Answer
The column names of your insert statement don’t match the order of the bind variables which means that your inserted record has the wrong account id values.
For example you set SBNumber as index 6 when it should be:
pst.setString(2,SBNumber);
It is also good practice to check the number of rows changed by updates so that you can make further asserts / checks on your actions:
int rows = pst.executeUpdate(); if (rows != 1) throw new RuntimeException("Failed to update account: "+ SBNumber);
In your case rows is set to 0 as the row to update is never found – because Linked_SB_Account is not matched.