Results 1 to 4 of 4
  1. #1
    iNko is offline Member
    Join Date
    Dec 2012
    Posts
    55
    Rep Power
    0

    Default Getting a value from one method that is in a 'while' , to another method

    Hey, i need to get a value from my database and define it as a variable so i can use it later.

    Heres an example of what im trying to do:

    Got this method where im taking a specific 'Surname' from a database table, where 'Login' matches. It writes the Surname in a label.
    Java Code:
    private void whoslogedinLabel(){       
        try {
           String sql = "SELECT Surname FROM Users WHERE login='" + whosloggedin + "'"; 
           pst = conn.prepareStatement(sql);    
           rs = pst.executeQuery(sql);
           while (rs.next())
           {
               logininfoLabel.setText(rs.getString("Surname"));   
           } 
    }
        catch (Exception e){
            JOptionPane.showMessageDialog(null, e);
        } 
        }
    After this, i want to take that Surname data (logininfoLabel) and define it as a variable so i could use it later on to get data from other database tables.

    Heres an example on how i want to use it (it doesnt work because 'logininfoLabel' isnt defined i think):
    Java Code:
    private void Update_Table(){
        try{
         String sql = "SELECT * FROM Group where Surname = '" + logininfoLabel + "'";
         pst = conn.prepareStatement(sql);
         rs = pst.executeQuery(sql);
         Table1.setModel(DbUtils.resultSetToTableModel(rs));
        } 
        catch (Exception e){
            JOptionPane.showMessageDialog(null, e);
        }
    }
    In this code i cant take the 'logininfoLabel' value, how do i take it? or do i need to make a new String sql somehow? please help.

  2. #2
    Zyril is offline Senior Member
    Join Date
    Oct 2011
    Location
    Sweden
    Posts
    124
    Rep Power
    0

    Default Re: Getting a value from one method that is in a 'while' , to another method

    Can you perhaps pass it as a parameter to your Update_Table method?

    Where do you define String logininfoLabel?

  3. #3
    iNko is offline Member
    Join Date
    Dec 2012
    Posts
    55
    Rep Power
    0

    Default Re: Getting a value from one method that is in a 'while' , to another method

    Quote Originally Posted by Zyril View Post
    Can you perhaps pass it as a parameter to your Update_Table method?

    Where do you define String logininfoLabel?
    whosloggedin - value from the login textfield.
    logininfoLabel - label that i write in the Surname value from Users table, where login = value from login textfield (whosloggedin).
    This logininfoLabel is only used in whoslogedinLabel() method, and only defined here - "private javax.swing.JLabel logininfoLabel;" (no more code with 'logininfoLabel ').

    I want to take the Surname value that i just wrote into the 'logininfoLabel' and use it in other methods to find specific data from other database tables. I dont know how to do this..

    As for passing it as a parameter, im not sure how to do this..

    Having a hard time forming my questions in english :(

  4. #4
    iNko is offline Member
    Join Date
    Dec 2012
    Posts
    55
    Rep Power
    0

    Default Re: Getting a value from one method that is in a 'while' , to another method

    I think i got it working..

    Made a new public static String Surname
    Java Code:
    public static String Surname;
    Created a new method to find Surname
    Java Code:
    private void findSurname(){ 
        try {
           String sql = "SELECT Surname FROM Users WHERE login='" + whosloggedin + "'";
           pst = conn.prepareStatement(sql);    
           rs = pst.executeQuery(sql);
           while (rs.next()){
           Surname= rs.getString("Surname");    
           }    
        } catch (Exception e) {
            JOptionPane.showMessageDialog(null, e);
        }  
    }

Similar Threads

  1. Replies: 2
    Last Post: 03-23-2012, 04:53 AM
  2. Replies: 1
    Last Post: 10-17-2011, 01:00 AM
  3. Replies: 18
    Last Post: 04-18-2011, 05:39 AM
  4. ArrayLists compareTo method, equals method
    By random0munky in forum New To Java
    Replies: 2
    Last Post: 10-26-2009, 07:20 PM
  5. cannot call private method from static method
    By jon80 in forum New To Java
    Replies: 3
    Last Post: 05-07-2008, 08:37 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
  •