Results 1 to 4 of 4
- 12-02-2012, 10:13 AM #1
Member
- Join Date
- Dec 2012
- Posts
- 55
- Rep Power
- 0
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.
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.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); } }
Heres an example on how i want to use it (it doesnt work because 'logininfoLabel' isnt defined i think):
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.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); } }
- 12-02-2012, 01:07 PM #2
Senior Member
- Join Date
- Oct 2011
- Location
- Sweden
- Posts
- 123
- Rep Power
- 0
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?
- 12-02-2012, 01:29 PM #3
Member
- Join Date
- Dec 2012
- Posts
- 55
- Rep Power
- 0
Re: Getting a value from one method that is in a 'while' , to another method
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 :(
- 12-02-2012, 02:26 PM #4
Member
- Join Date
- Dec 2012
- Posts
- 55
- Rep Power
- 0
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
Created a new method to find SurnameJava Code:public static String 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
-
Creating recursion method to use Newton's method for square roots
By bdl1127 in forum New To JavaReplies: 2Last Post: 03-23-2012, 04:53 AM -
Whats the different between package.class.method and super.method?
By Pojahn_M in forum New To JavaReplies: 1Last Post: 10-17-2011, 01:00 AM -
Can i Pass a variable as a parameter from one method to other method
By Anagha in forum New To JavaReplies: 18Last Post: 04-18-2011, 05:39 AM -
ArrayLists compareTo method, equals method
By random0munky in forum New To JavaReplies: 2Last Post: 10-26-2009, 07:20 PM -
cannot call private method from static method
By jon80 in forum New To JavaReplies: 3Last Post: 05-07-2008, 08:37 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks