-3

My java database is not updating but the query executed successfully. I'm trying to call my update method from a class named "ProductClass" and put the method into my jframe source code "UpdateProductForm". But I'm not sure if this issue is due to an error in creating my update method or an error in calling my update method. Note that there is no Error message given by the compiler.I need help badly. Thanks in Advance.

My ProductClass

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;

public class ProductClass extends MaterialClass implements Addable, Updatable, Deletable {  

public int PRODUCT_ID;
public String PRODUCT_NAME;
public String PRODUCT_BRAND;
public String PRODUCT_MODEL;
public String PRODUCT_TYPE;
public int PRODUCT_PRICE;
public int MATERIAL_ID;

public ProductClass(int PRODUCT_ID, String PRODUCT_NAME, String PRODUCT_BRAND, String PRODUCT_MODEL, String PRODUCT_TYPE, int PRODUCT_PRICE, int MATERIAL_ID)
{

}
public ProductClass(int PRODUCT_ID)
{ 
}

@Override
public boolean Update() 
{
     try 
    {
        Connection conn= DriverManager.getConnection("jdbc:derby://localhost:1527/KDatabase","koushal","456");
        String query= "UPDATE PRODUCT SET PRODUCT_NAME= '"+PRODUCT_NAME+"' ,PRODUCT_BRAND= '"+PRODUCT_BRAND+"' ,PRODUCT_MODEL= '"+PRODUCT_MODEL+"' ,PRODUCT_TYPE='"+PRODUCT_TYPE+"',PRODUCT_PRICE= "+PRODUCT_PRICE+" ,MATERIAL_ID= "+MATERIAL_ID+" where PRODUCT_ID= "+PRODUCT_ID+"  ";
        PreparedStatement statement =conn.prepareStatement(query);
        statement.executeUpdate();
        return true;
    } 
    catch (SQLException ex) 
    {
        Logger.getLogger(SystemAdministrator.class.getName()).log(Level.SEVERE, null, ex);
    }
    return false;
}
}

My UpdateProductForm where I'm trying to call the method update. Update Successful here but my database not updated

    private void UpdateButtonActionPerformed(java.awt.event.ActionEvent evt) {                                             
    int value1=Integer.parseInt(Ptd_IDtxtField.getText());
    String value2=Ptd_NametxtField.getText();
    String value3=PtdBrand_txtField.getText();
    String value4=PtdModel_txtField.getText();
    String value5=PtdType_txtField.getText();
    int value6=Integer.parseInt(PtdPrice_txtField.getText());
    int value7=Integer.parseInt(MaterialID_txtField.getText());
    ProductClass object = new ProductClass(value1,value2,value3,value4,value5,value6,value7);
    if(object.Update()==true)
    {
        JOptionPane.showMessageDialog(null,"Update Successful");
    }
}  

this is my Updatable Public Interface

public interface Updatable {
/**
 *
 * @return
 */
public boolean Update();

}

MarcoReus
  • 1
  • 5
  • Are you seeing any error message? Best to separate your GUI and your database code, and try to debug them separately. – DontKnowMuchBut Getting Better Nov 01 '18 at 20:22
  • There is no error message – MarcoReus Nov 01 '18 at 20:23
  • 1
    You are using the PreparedStatement incorrectly. The point of using the PreparedStatement is that you specify the SQL with "?" to represent the values and then you use the PreparedStatement to replace those values so you don't need to be concerned with delimiters. It makes the SQL easier to write and understand and is less error prone. Simple example: https://stackoverflow.com/questions/6838970/when-i-am-trying-to-save-the-data-to-mysql-an-exception-is-being-thrown-java-s/6839015#6839015 – camickr Nov 01 '18 at 20:26
  • You may have an answer to your problem, but I re-iterate that you really ***really*** want to have a stronger separation of concerns. Looking again at your code, the database access shouldn't be within Product either but should be in a class of its own. Otherwise you've got a debugging nightmare ahead of you. – DontKnowMuchBut Getting Better Nov 01 '18 at 20:26
  • but why the database is not being updated? Anyone can fix the issue. I really need a help – MarcoReus Nov 01 '18 at 20:35
  • If you just want a quick fix then change your declaration of the variable `statement` to this: `Statement statement = conn.createStatement(query);`. However, that is not really the best approach, and @camickr has already given you the proper answer. Also include the `commit()` in the answer below. – skomisa Nov 01 '18 at 23:34
  • `I really need a help` - 1) you haven't posted your code showing the proper usage of the PreparedStatement. 2) you haven't showed how you invoke the commit() method. 3) do basic debugging of your code to display the value of your variables when they are used in the PreparedStatement. How do you even know the "where" clause is correct? Did you try doing a simple query first to make sure you have a valid product id? – camickr Nov 02 '18 at 00:55

1 Answers1

0

Your update missing commit statement at the end

 conn.commit();

And also close your resources, preferqbly using try with resources since java 7, see more in docs

Ori Marko
  • 56,308
  • 23
  • 131
  • 233