Results 1 to 5 of 5
  1. #1
    Symbiot is offline Member
    Join Date
    May 2010
    Posts
    15
    Rep Power
    0

    Default returning data outside scope..

    Hi

    I am trying to read some data from an sql table, but I can't get it to return the data stored in my 'usr' ... The problem is obvious since usr is limited to the scope above the return but how can I structure the below code to circumvent the problem?


    Java Code:
    public static User getUserbyUid(int id)
             
        {      
                try
            {
                Connection con = getConnection();
                Statement s = con.createStatement();
                ResultSet RS = s.executeQuery("SELECT * FROM users");
    
                RS.next();
    
                int ID = RS.getInt("id");
                String userName = RS.getString("username");
                String Psw = RS.getString("psw");
                int startDate = RS.getInt("startdate");
          
                User usr = new User(ID, userName, Psw, startDate);
    
            }
    
    
            catch (SQLException sqlgetUserbyUid)
            {
                System.out.print(sqlgetUserbyUid);
            }
    
              return usr; 
        }

  2. #2
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,365
    Blog Entries
    7
    Rep Power
    20

    Default

    Don't define your User usr object inside your try { ... } block.

    kind regards,

    Jos

  3. #3
    Symbiot is offline Member
    Join Date
    May 2010
    Posts
    15
    Rep Power
    0

    Default

    Yes, but if I define it outside, then the variables aren't initialized (no data).

  4. #4
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,365
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by Symbiot View Post
    Yes, but if I define it outside, then the variables aren't initialized (no data).
    Be a bit creative:

    Java Code:
    User yourMethod( ... ) {
       User usr= null;
       try {
           // assign a value to usr here
       }
       catch ( ... ) { ... }
       return usr;
    }
    kind regards,

    Jos

  5. #5
    Symbiot is offline Member
    Join Date
    May 2010
    Posts
    15
    Rep Power
    0

Similar Threads

  1. Looking for some info.
    By Ragster in forum JDBC
    Replies: 2
    Last Post: 01-17-2008, 05:19 PM
  2. release info
    By venkatashiva in forum Java Software
    Replies: 3
    Last Post: 01-01-2008, 05:25 AM
  3. Getting Class info
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 12-06-2007, 02:46 PM
  4. Getting Header Info
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 11-26-2007, 01:30 PM
  5. JVM Info
    By kishi in forum Advanced Java
    Replies: 0
    Last Post: 11-16-2007, 01:44 PM

Posting Permissions

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