Ver Mensaje Individual
  #8 (permalink)  
Antiguo 13/09/2010, 02:43
peter_21
 
Fecha de Ingreso: mayo-2010
Mensajes: 71
Antigüedad: 14 años
Puntos: 1
Respuesta: llamar a objeto desde otra clase

Gracias ioel, pero no entiendo muy bien lo que quieres decir.
He intentado hacer lo que me has dicho, pero como no lo entiendo muy bien, pues no lo consigo hacer. Es que ese es el problema, que me quiero guardar la primera fila seleccionada, pero no lo consigo, porque siempre que necesito llamar al objeto creado a partir de esa fila, me da null.

Te pongo lo que he intentado hacer ahora:
AQUÍ SELECCIONO LA PRIMERA FILA DE LA PRIMERA VENTANA
Código:
 private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {                                         
        seleccionado();
        dispose();
}
public void seleccionado(){
        InscribirParticipante ip = new InscribirParticipante(null, true);
        Empresa em = new Empresa(0, null, null, null, null, 0, 0, null, null, null);
        try{
        int idC = (Integer) jTable1.getValueAt(jTable1.getSelectedRow(), 0);
        String id = (String) jTable1.getValueAt(jTable1.getSelectedRow(), 1);
        String nombre = (String) jTable1.getValueAt(jTable1.getSelectedRow(), 2);
        String fecha = (String) jTable1.getValueAt(jTable1.getSelectedRow(), 3);
        String ciudad = (String) jTable1.getValueAt(jTable1.getSelectedRow(), 4);
        String ca = (String) jTable1.getValueAt(jTable1.getSelectedRow(), 5);
        int edad_minima = (Integer) jTable1.getValueAt(jTable1.getSelectedRow(), 6);
        int edad_maxima = (Integer) jTable1.getValueAt(jTable1.getSelectedRow(), 7);
        String sexo = (String) jTable1.getValueAt(jTable1.getSelectedRow(), 8);

         em = new Empresa(idC, id, nombre, fecha, ciudad, edad_minima, edad_maxima, sexo, ca, null);
        
        }catch(Exception e){
            System.out.println(e);
        }
         finally{
            ip.recibeEm(em); //Aquí paso a un metodo de la otra clase, la de la ventana 2, el objeto empresa, que es la fila que he seleccionado en la primera ventana. Este es el problema, que no se como pasarla a la otra clase..
        }
}
Y AQUÍ SELECCIONO LA SEGUNDA FILA DE LA SEGUNDA VENTANA Y LUEGO LLAMO A UN METODO PARA HACER LA INSERCCION EN LA TABLE DE LA BB.DD.

Código:
//ESTE PRIMER METODO ES EL ENCARGADO DE SELECCIONAR LA SEGUNDA FILA DE LA SEGUNDA VENTANA DE MI APLICACION.
private void jButton8ActionPerformed(java.awt.event.ActionEvent evt) {                                         
        String p = "";
        int id = (Integer) jTable1.getValueAt(jTable1.getSelectedRow(), 0);
        String nombre = (String) jTable1.getValueAt(jTable1.getSelectedRow(), 1);
        String apellido1 = (String) jTable1.getValueAt(jTable1.getSelectedRow(), 2);
        String apellido2 = (String) jTable1.getValueAt(jTable1.getSelectedRow(), 3);
        int fecha = (Integer) jTable1.getValueAt(jTable1.getSelectedRow(), 4);
        String sexo = (String) jTable1.getValueAt(jTable1.getSelectedRow(), 5);
        String pais= (String) jTable1.getValueAt(jTable1.getSelectedRow(), 6);
        String comunidad = (String) jTable1.getValueAt(jTable1.getSelectedRow(), 7);
        String em = (String) jTable1.getValueAt(jTable1.getSelectedRow(), 8);
        String dni = (String) jTable1.getValueAt(jTable1.getSelectedRow(), 9);
        int edad = (Integer) jTable1.getValueAt(jTable1.getSelectedRow(), 10);
        int peso = (Integer) jTable1.getValueAt(jTable1.getSelectedRow(), 11);
        int ra = (Integer) jTable1.getValueAt(jTable1.getSelectedRow(), 12);

        Empleado j = new Empleado(id, nombre, apellido1, apellido2, fecha, sexo, pais, comunidad, em, dni, edad, peso, ra);
        Pesos pe = new Pesos(null, true);
        pe.setVisible(true);
        p = pe.devuelvePeso();
       
        insertarParticipa(j);  //Y aquí llamo al metodo que se encargara de insertar en la bb.dd....pero este metodo tambien necesita el un parametro de la primera fila...
}

//Este metodo es el que recibe la fila/empresa seleccionada en la otra tabla, en la primera ventana... me inicializo fuera Empresa porque si lo hago dentro, luego, desde insertarParticipa no puedo acceder a la variable...es que nose como hacerlo.
Empresa em = new Empresa(0, null, null, null, null, 0, 0, null, null, null);
    public Empresa recibeEm(Empresa e){
       em = e;
       return em;
    }

//Y este metodo es el que deberia insertar en la bb.dd...pero el problema es que no tengo manera de conseguir el getNombre de la empresa, porque siempre me da null....no se como pasar el objeto de la otra clase a esta.
public void insertarParticipa(Empleados e){
        Connection conexion = null;
        int i=0;
        int idE = e.getId();
        String nom = cam.getNombre();
        System.out.println(nom); // ME SIGUE DANDO NULL
        try{
           Class.forName("com.mysql.jdbc.Driver");
           conexion = DriverManager.getConnection("jdbc:mysql://localhost/gestion", "root", "xxxx");
           PreparedStatement consulta1 = conexion.prepareStatement("select idC from empresas where nombre='"+nom+"'");
           ResultSet result1 = consulta1.executeQuery();

           while(result1.next()){
               i = result1.getInt(1);
           }

         }catch(Exception e){
            System.out.println("Ocurrio la siguiente excepcion : " + e.toString());
            System.out.close();
        }

        try{
           Class.forName("com.mysql.jdbc.Driver");
           conexion = DriverManager.getConnection("jdbc:mysql://localhost/gestion", "root", "xxxx");
           Statement st = conexion.createStatement();
           st.executeUpdate("INSERT INTO participa (idJ, idC, nombre, resultado, idCategoria) VALUES('"+idE+"', '"+i+"', '"+nom+"', "+null+", "+null+")");

        }catch(Exception e){
            System.out.println("Ocurrio la siguiente excepcion : " + e.toString());
            System.out.close();
        }finally{
            try{if (null != conexion)
                conexion.close();
            }catch(Exception e){
                System.out.println("Ocurrio la siguiente excepcion(2) : " + e.toString());
            }

        }
    }

Ioel10, cuando te refieres a:

Cita:
Guardas esa fila de la primera ventana al seleccionarla o la fila que tengas seleccionada al abrir la segunda ventana...
En principio eso lo hago...me guardo la fila en el objeto "em".
Cita:
1. ... y después llamas al método get de Empresa.
Eso lo hago ya en la segunda clase, pero dice que es null...porque no consigo pasar bien el objeto.

Cita:
2. ... y cuando realices la llamada a la segunda le pasas Empresa como parámetro y ya manejas esta variable desde tu ventana sin tener que llamar a ningun método de la primera ventana.
Ahora si que le paso a la segunda clase el objeto Empresa...pero como accedo al metodo que recibe dicho parametro?


MIL GRACIAS POR ADELANTADO