Results 1 to 6 of 6
  1. #1
    Eleeist is offline Member
    Join Date
    May 2011
    Posts
    84
    Rep Power
    0

    Default Cannot find symbol

    Hello,

    When I try to compile this code:
    Java Code:
    import java.sql.*;
    
    public class Database {
        
        public Connection connectToDatabase() throws Exception {
    
            Class.forName("com.mysql.jdbc.Driver");
    
            Connection connection = DriverManager.getConnection("jdbc:mysql://localhost/mud", "x", "x");
            
            return connection;
            
        }
        
        public void getCommands(String category) throws Exception {
            
            this.connectToDatabase();
            
            Statement statement = connection.createStatement();
            ResultSet queryResult = statement.executeQuery("SELECT * FROM commands WHERE category=" + category);
    
            while (queryResult.next()) {
                
                String outputString = queryResult.getString("command");
                System.out.println(outputString);
            
            }
            
        }
        
    }
    I get compile error:

    Java Code:
    cannot find symbol
    symbol  : variable connection
    location: class Database
            Statement statement = connection.createStatement();
    But connectToDatabase() returns variable connection, so why does it say it does not exists?

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

  3. #3
    Eleeist is offline Member
    Join Date
    May 2011
    Posts
    84
    Rep Power
    0

    Default Re: Cannot find symbol

    Ok, but here:

    Java Code:
    Statement statement = connection.createStatement();
    connection is lowercase and connectToDatabase() also returns lowercase:

    Java Code:
    Connection connection = DriverManager.getConnection("jdbc:mysql://localhost/mud", "x", "x");         
            return connection;
    I don't really get it.

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default Re: Cannot find symbol

    The connection variable inside of the connectToDatabase method is "local" to that method. Outside of the method it is invisible (read up on variable scope for more on this).

    You call connectToDatabase inside of your getCommands method, but you discard the object returned. For example you do this:

    Java Code:
        public void getCommands(String category) throws Exception {
            this.connectToDatabase();

    instead of this:
    Java Code:
        public void getCommands(String category) throws Exception {
            Connection connection = connectToDatabase();  // the "this" is not necessary

    To use the Connection object returned by that method, you'll have to assign it to a variable as I show above.

  5. #5
    Eleeist is offline Member
    Join Date
    May 2011
    Posts
    84
    Rep Power
    0

    Default Re: Cannot find symbol

    I get it now. Thanks for explanation :).

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default Re: Cannot find symbol

    Quote Originally Posted by Eleeist View Post
    I get it now. Thanks for explanation :).
    You're welcome. Glad you've got it working.

Similar Threads

  1. Cannot Find Symbol
    By Promisha in forum New To Java
    Replies: 10
    Last Post: 03-30-2011, 02:11 AM
  2. can't find symbol
    By globo in forum New To Java
    Replies: 21
    Last Post: 01-17-2011, 04:06 AM
  3. Cannot find symbol
    By Johanis in forum New To Java
    Replies: 18
    Last Post: 11-09-2010, 08:34 PM
  4. Replies: 9
    Last Post: 10-18-2008, 08:26 PM
  5. Replies: 3
    Last Post: 05-01-2008, 08:30 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
  •