Results 1 to 4 of 4
  1. #1
    Lagarto is offline Member
    Join Date
    Jul 2007
    Posts
    2
    Rep Power
    0

    Default Passing a value..

    Hello all!

    I want to pass a value of one class to another class, but the classes are in different files.

    I'm going to post both methods and remember they are in different classes :confused:

    Ok from this method i want to pass the value of the variables Nombre, Apellido
    below.
    Java Code:
     public void seleccion(String x){
            Connection con;
            Statement stmt;
            ResultSet rs;
            String [] array = new String [2];
            
            try{
                Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
            }
            catch(Exception e){
                System.out.println("No se pudo lograr la conexion");
            }
            
            try{
                con = DriverManager.getConnection("jdbc:odbc:con_sql","","");
                stmt = con.createStatement();
                int z = Integer.parseInt(x);
                rs = stmt.executeQuery("SELECT Nombre, Apellido FROM dbo.xxx WHERE ID=" + z);
                
                while(rs.next()){  
                     array[0] = rs.getString("Nombre");
                     array[1] = rs.getString("Apellido");
                     Nombre = array[0];
                     Apellido = array[1];
                         
                }
               
            }
            
            catch(Exception e){
                System.out.println("Error al seleccionar Empleado" + " "+ e);
                
            }
    This method is the one that has to receive the values to put them in a text box.

    Java Code:
    private void btnBuscarActionPerformed(java.awt.event.ActionEvent evt) {                                          
            
             String a = txtID.getText();
             int x = Integer.parseInt(a);
             Conexion c = new Conexion();
             txtNombre.setText(c.seleccion(Nombre));  // Does not compile!
             txtApellido.setText(c.seleccion(Apellido));  // Does not compile!
             
             
             
             
        }

  2. #2
    santa's Avatar
    santa is offline Senior Member
    Join Date
    Nov 2009
    Location
    Sweden
    Posts
    208
    Rep Power
    6

    Default

    Is that the code you are going to compile? to get an variable from the other class create a object of the class and set variable to public :
    seleccion s = new seleccion();
    String nombre = s.array[0];
    String Apellido = s.array[1];


    there are alot of things wrong with your code =P

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by santa View Post
    there are alot of things wrong with your code =P
    Since he posted this over 3 years ago, I'm betting he's not losing too much sleep worrying about it. :rolleyes:

  4. #4
    santa's Avatar
    santa is offline Senior Member
    Join Date
    Nov 2009
    Location
    Sweden
    Posts
    208
    Rep Power
    6

    Default

    yes ... or ... he could be sitting every day eyeballing it awaiting an answer for 4 years ;) it is also a scenario ... no but i just wanted to help someone and sorted on replys ;P haha I did not see the 2007 date =D

Similar Threads

  1. Passing data from one JFrame to another
    By abhiN in forum New To Java
    Replies: 2
    Last Post: 03-28-2008, 06:39 AM
  2. JNI - passing and returning parameters by value
    By java_to_c in forum New To Java
    Replies: 0
    Last Post: 01-31-2008, 09:49 PM
  3. passing dynamic data
    By abhiN in forum Web Frameworks
    Replies: 0
    Last Post: 01-17-2008, 02:16 PM
  4. Help passing arraylist to another class
    By adlb1300 in forum New To Java
    Replies: 3
    Last Post: 11-06-2007, 09:02 PM
  5. Passing the Values
    By Lagarto in forum JDBC
    Replies: 1
    Last Post: 07-16-2007, 04:03 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •