Home > Mobile >  When I work with JAVA dao architect I had a problem when I had a foreign key
When I work with JAVA dao architect I had a problem when I had a foreign key

Time:04-24

I had a java project about bank account managing with java ana DAO Design. I work in operation class witch is depend to client class and account(compte in french ) class.


operation variable class is like that:

private String DateDebut ;
private String DateFin ;
private String NumeroOperation ;
private String TypeOperation ;
private Compte Compte ;
private Client Client ;
public Operation(String numeroOperation, String typeOperation, String dateDebut, String dateFin, Compte compte,Client client) {
    DateDebut = dateDebut;
    DateFin = dateFin;
    NumeroOperation = numeroOperation;
    TypeOperation = typeOperation;
    Compte =compte ;
    Client = client ;
}

and I write in OperationDAO Class that to find operation By their ID:

public Operation findById (String code) {
    Connection  cnx = Sconnection.getInstance() ;
    Operation op=null;
    try {       
    PreparedStatement req = cnx.prepareStatement("select * from operation where numerOperation = ? ");
    req.setString(1, code);
    
    ResultSet res=  req.executeQuery();
        while (res.next())
        {
            op = new Operation(code, res.getString(2),res.getString(3),res.getString(4),res.getString(5),res.getString(6)) ;
            System.out.println(res.getString(2)  " "  res.getString(3) " "  res.getString(4));
        }
    req.close();
    }catch (SQLException e)
    {System.out.println("Erreur de chargement de client.. verifier !!"  e.getMessage());}
    
    return op;
    }

but I had a problem in this line :

op = new Operation(code,res.getString(2),res.getString(3),res.getString(4),res.getString(5),res.getString(6)) ;

but in dataBase operation Table is like this

PHP MyAdmin

but I don't know how I should save or update or find variable of foreign keys.

CodePudding user response:

What I understood from your explanation is that you want to save Compte and Client in Operation class.

Problem arises where you are initializing Operation class.

Since both NumeroCompt and NumeroClient is foreign key you need to make 2 more DB calls to get details of Compt from NumeroCompt and Client from NumeroClient

While update you have to do the same. Make 2 more update calls to save details in other table.

Moving to use Hibernate/JPA will make your life a lot easier.

  • Related