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

Hola,

lo que me parecia un problema sencillo, se me esta complicando.

En mi aplicacion, en una ventana, quiero seleccionar una fila de un jtable y con esos datos me creo un objeto.

Ahora, desde otra ventana, con otro jtable, elijo una fila tambien(es otro jtable) y entonces procedo a escribir en una tabla de una base de datos... pero quiero escribir un dato en concreto de la segunda fila y otro dato de la primera fila seleccionada en el primer jtable... Pero como me paso el objeto a la segunda clase?

Os pongo codigo para que lo entendais mejor:

ESTA ES LA PRIMERA CLASE, DONDE TENGO EN MI VENTANA UN JTABLE Y SELECCIONO UNA FILA Y AL DARLE A ACEPTAR SE ME CREA EL OBJETO EMPRESA.
Código:
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {                                         
        seleccionado();
        dispose();
}

public Empresa seleccionado(){
        //Connection conexion = 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);

        Empresa em = new Empresa(idC, id, nombre, fecha, ciudad, edad_minima, edad_maxima, sexo, ca, null);
        
        return em;

        }catch(Exception e){
            return null;
        }
}

Y ESTA ES LA SEGUNDA CLASE. Donde tengo otro Jtable y selecciono tambien una fila...una vez la tengo, quiero guardar una dato de este jtable y otro dato del otro jtable.
Hago primero una consulta porque el dato que quiero guardar es un autoincrementable en la bb.dd...y por eso lo leo. Pero el tema esta en lo que esta en ROJO que al llamar a seleccionado() de la otra clase me da NULL...que es lógico. Entonces como puedo pasarle el objeto EM de la otra clase que es un objeto con los datos de los seleccionado?

Código:
private void jButton8ActionPerformed(java.awt.event.ActionEvent evt) {                                         
        // TODO add your handling code here:
        String p = "";
        Connection conexion = null;
        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();
        Empresa em = new Empresa(0,null,null, null, null, 0, 0, null, null, null);
        SeleccionarEm cogerEm = new SeleccionarEm(null, true);
        em = cogerEm.seleccionado();
        System.out.println(em); //NNNNUUULLLL
        int idJ = j.getId(); // ESTE IDJ LO QUIERO GUARDAR EN LA BB.DD A LA VEZ QUE EL idE pero como el campo nombre es null....
        String nom = em.getNombre();// Y ESTO ES LO QUE FALLA PORQUE nom es null, puesto que no me pasa bien el objeto EM de la otra clase
        int i=0;
         try{
           Class.forName("com.mysql.jdbc.Driver");
           conexion = DriverManager.getConnection("jdbc:mysql://localhost/gestion", "root", "xxx");
           PreparedStatement consulta1 = conexion.prepareStatement("select idE 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", "xxx");
           //PreparedStatement consulta1 = conexion.prepareStatement();
           Statement st = conexion.createStatement();
           st.executeUpdate("INSERT INTO participa (idJ, idC, nombre, resultado, idCategoria) VALUES('"+idJ+"', '"+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());
            }

        }
    }


Agradeceria mucho vuestra ayuda!
1 saludo