Go through a ResultSet of objects that have another object as an attribute

0

The problem is that I create objects of class Car that have as an attribute an object of class Motor, when going through the resulSet of the query I have printed the data I can not do it well.

In the database there are saved 2 cars and 2 engines that have as fk the id of one of the cars, At the time of printing the result I print the following:

selectSQL= "SELECT * FROM COCHE,MOTOR";

How it can be seen in the image is how the car 1 prints twice, one with its engine and the other with the other, the same happens with car 2.

I attach the code

    private void lecturaBDSQL(){
    conexion.getConexion();
    stm = conexion.getStatement();   
    selectSQL= "SELECT * FROM COCHE,MOTOR";
    //selectMotor="SELECT * FROM MOTOR";
    //selectMotor= "SELECT * FROM MOTOR";
  //Debuggin correcto
    try {
       rs = stm.executeQuery(selectSQL);
      //  rsMotor = stm.executeQuery(selectMotor);
   } catch (SQLException e) {
        System.out.println("Error al lanzar la query para recuperar los datos de la tabla COCHES");
    }
    try{

            while(rs.next()){
                       System.out.println("*****************************************");
                       System.out.println("El id del coche es: "+rs.getInt(1));
                       System.out.println("La marca del coche es: "+rs.getString(2));
                       System.out.println("El modelo del coche es: "+rs.getString(3));
                       System.out.println("El numero de plazas del coche es: "+rs.getInt(4));
                       System.out.println("El precio final del coche es: "+rs.getInt(5)+"\n");

                       System.out.println("El carburante que usa el motor es: "+rs.getString(6));
                       System.out.println("La potencia del motor es de: "+rs.getInt(7)+" CV");
                       System.out.println("La cilindrada del motor es de: "+rs.getInt(8)+" cm3");
                       System.out.println("El numero de cilindros del motor es  "+rs.getInt(9)+"\n");
                       System.out.println("***************************************** \n \n \n");
                   }


    }catch(Exception recorrido){
        System.out.println("Error al recorrer el resultSet que contiene la informacion de la table COCHE");
        try {
            stm.close();
        } catch (SQLException e) {
            System.out.println("Error al intentar cerrar la conexion con la BD SQL. \n");
        }

        try{

        rs=stm.executeQuery(selectMotor);
        } catch (SQLException e) {
             System.out.println("Error al lanzar la query para recuperar los datos de la tabla COCHES");
         }


        conexion.cerrarConexion();
    }
}

Thanks in advance.

    
asked by luis 04.04.2018 в 17:35
source

1 answer

1

A car can have a single engine but the query you are looking for is within the table MOTOR all data at the same time as COCHE . I think you need a JOIN . It would be something like

select * from COCHE join COCHE.id on MOTOR.id

    
answered by 05.04.2018 в 00:10