-3

I am a new guy for JAVA.

This is my mysql customer table structure.

   create table Customer(
    CID  VARCHAR (10) primary key,
    Name VARCHAR (30) not null,
    LicenseID VARCHAR (10) not null,
    NIC VARCHAR (10) not null,
    DOB date not null,
    Address VARCHAR (25) not null,
    Tp int (10) ,
    Email VARCHAR (30) not null 
   );

This is my customerController function.

private CustomerController custController;

private void addCustomerBtnActionPerformed(java.awt.event.ActionEvent evt) {                                               
    CustomerDTO customer = new CustomerDTO(
            idTxt.getText(),
            nameText.getText(),
            licenseText.getText(),
            NICText.getText(),
            DOBText.getText(),
            addressText.getText(),
            Integer.parseInt(tpText.getText()),
            emailText.getText());
    try { 
        boolean result = custController.add(customer);
        if (result){
            JOptionPane.showMessageDialog(this, "Customer has been successfully added...");
        }else{
            JOptionPane.showMessageDialog(this, "Customer has not been added...");
        }
    } catch (Exception ex) {
        Logger.getLogger(addCustomer.class.getName()).log(Level.SEVERE, null, ex);
    }
}

This is my add customer controller.

@Override
public boolean add(CustomerDTO dto) throws Exception {
    String SQL="Insert into Customer values(?,?,?,?,?,?,?,?)";
    PreparedStatement stm=connection.prepareStatement(SQL);
    stm.setObject(1,dto.getCID());
    stm.setObject(2,dto.getName());
    stm.setObject(3,dto.getLicenseID());
    stm.setObject(4,dto.getNic());
    stm.setObject(5,dto.getDob());
    stm.setObject(6,dto.getAddress());
    stm.setObject(7,dto.getMobile());
    stm.setObject(8,dto.getEmail());
    int res=stm.executeUpdate();
    return (res > 0);
}

When I add the new customer, it is added to database and also it show this message : "Customer has been successfully added...". But when i try to add an existing CID or Tp as a string, I can't see the "Customer has not been added..." message. Please can anyone help me solve this problem?

litelite
  • 2,857
  • 4
  • 23
  • 33
  • Is an exception logged instead? – KevinO May 23 '17 at 19:41
  • There should either be the message `"Customer has been successfully added..."` or the message `"Customer has not been added..."`, provided the method gets called at all and no `Exception` is thrown. How is the return value of `custController.add(...)` defined? When does it return `true`, when does it return `false`? – Turing85 May 23 '17 at 19:42
  • sorry Sir..i didn't get it – Nuwan Pradeep May 23 '17 at 19:43
  • @NuwanPradeep, you haven't provided any definition for custController. Assuming the method is, as noted, called, it must return true/false, or throw an Exception (checked or not). – KevinO May 23 '17 at 19:45
  • Add `JOptionPane.showMessageDialog()` to your catch. It looks like your custController.add() throws exception when you try to add existing record instead of returning `false` – Bor Laze May 23 '17 at 19:46
  • ya..i already created..i can get return true..but i can't get false result..when true i can see the message Customer has been successfully added .. but when i try to use same id it is not return false and also no show the "Customer has not been added..." message.. – Nuwan Pradeep May 23 '17 at 19:48
  • @KevinO : i edited my question..please check it and help me – Nuwan Pradeep May 23 '17 at 19:55
  • 1
    I *think* trying to insert again throws an SQLException. If so, then you should be seeing the logged exception. Did you do what @BorLaze suggested? If it throws an exception, you will not return false, and therefore it will not show the "customer has not been added". Aside: always define a method's `throws` to the narrowest possible, which would be `SQLException` instead of Exception. Edit: [this page](https://stackoverflow.com/questions/26761436/catch-duplicate-key-insert-exception) seems to confirm that an SQLException is thrown. – KevinO May 23 '17 at 20:02

1 Answers1

1

I think the problem is with the method where your query is called. Have you handled exception inside your add() method? Only if you have handled it there and returned a value in catch, it can return value here. If you have not handled there, it might throw error in console and end your application.

Anusha
  • 36
  • 3