Results 1 to 6 of 6
Thread: Cannot find symbol
- 01-22-2012, 04:57 PM #1
Member
- Join Date
- May 2011
- Posts
- 84
- Rep Power
- 0
Cannot find symbol
Hello,
When I try to compile this code:
I get compile error: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); } } }
But connectToDatabase() returns variable connection, so why does it say it does not exists?Java Code:cannot find symbol symbol : variable connection location: class Database Statement statement = connection.createStatement();
-
Re: Cannot find symbol
The compiler doesn't lie:
connection != Connection.
- 01-22-2012, 05:29 PM #3
Member
- Join Date
- May 2011
- Posts
- 84
- Rep Power
- 0
Re: Cannot find symbol
Ok, but here:
connection is lowercase and connectToDatabase() also returns lowercase:Java Code:Statement statement = connection.createStatement();
I don't really get it.Java Code:Connection connection = DriverManager.getConnection("jdbc:mysql://localhost/mud", "x", "x"); return connection;
-
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.
- 01-22-2012, 08:35 PM #5
Member
- Join Date
- May 2011
- Posts
- 84
- Rep Power
- 0
Re: Cannot find symbol
I get it now. Thanks for explanation :).
-
Similar Threads
-
Cannot Find Symbol
By Promisha in forum New To JavaReplies: 10Last Post: 03-30-2011, 02:11 AM -
can't find symbol
By globo in forum New To JavaReplies: 21Last Post: 01-17-2011, 04:06 AM -
Cannot find symbol
By Johanis in forum New To JavaReplies: 18Last Post: 11-09-2010, 08:34 PM -
cannot find symbol symbol :constructor Error. Please help! =(
By KalEl in forum New To JavaReplies: 9Last Post: 10-18-2008, 08:26 PM -
cannot find symbol symbol : class Item location: package platypos.services.order
By officialhopsof in forum New To JavaReplies: 3Last Post: 05-01-2008, 08:30 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks