2

I am beginner to java!! I got cannot find symbol error in java. I google it but not found any satisfactory solution. Here is my connect.java class

public class connect
{
    static String JDBC_DRIVER; 
    static String DB_URL;
    static String USER;
   static String PASS;
 public connect(){
    JDBC_DRIVER = "com.mysql.jdbc.Driver"; 
    DB_URL = "jdbc:mysql://localhost:3306/my_db";
    USER = "admin";
    PASS = "123";
 }
   public Connection openDbConnection(){
        Connection conn = null;
        try{
           Class.forName("com.mysql.jdbc.Driver");
           System.out.println("Connecting to database...");
           conn = DriverManager.getConnection(DB_URL,USER,PASS);
        }
        catch(SQLException se){
           se.printStackTrace();
        }
        catch(Exception e){
           e.printStackTrace();
        }
        System.out.println("successfully connected to db");
        return conn;
   }

And this is query.java class

public class query extends connect {
    Statement stmt;
    Connection connection;
    public query(){
        super();
        stmt = null;
        connection =null;
    }

    public void runquery(){

        connection=super.openDbConnection();
        System.out.println("Creating statement...");
        stmt = connection.createStatement();
        String sql;
        sql = "SELECT * FROM my_table";
        ResultSet rs = stmt.executeQuery(sql);
        while(rs.next()){
           int id  = rs.getInt("id");
           System.out.print("ID: " + id);
        }
    }
    public static void main(String[] args){
        query obj = new query(); 
        obj.runquery();
    }   
}

When i compile query.java through terminal javac query.java, i got error:

public class query extends connect {
                           ^
  symbol: class connect
src/database/query.java:22: error: cannot find symbol
        connection=super.openDbConnection();
                   ^
  symbol:   variable super
  location: class query
2 errors

both classes are in same directory. Can any one help me to fix this error???

Thorn G
  • 12,620
  • 2
  • 44
  • 56
Sony Khan
  • 1,330
  • 3
  • 23
  • 40
  • are you sure your connect class compiles without errors? – ParkerHalo Nov 23 '15 at 13:23
  • Looks like your connect class is not in the build path. – Jens Nov 23 '15 at 13:28
  • 1
    Note: Java has coding conventions. Names of classes, interfaces and enums should start with a capital letter, like `Connect`, `ConnectToDatabase`. Variables and fields which are not constants should start with a lowercase letter, like `jdbcDriver`, `dbUrl` etc. Constants (either enum constants or `static final` variables are the only ones that are written in all-caps and have underscores between words, like `CONST_NAME`. – RealSkeptic Nov 23 '15 at 13:30

2 Answers2

2

Try this

 javac connect.java query.java

it should work.

Or you can try compiling your classes from parent directory of your package i.e. from src directory like below in my case

C:\..\YourProject\src>javac -cp . database\query.java

The link below explains the second approach which I discussed above.

See Also

Community
  • 1
  • 1
RockAndRoll
  • 2,247
  • 2
  • 16
  • 35
0

The class connect miss a } to compile, so the compiler use a older connect.class-file from a version he could compile before, a old version who did not have the openDbConnection-Method. As long as the connect.java can not be compiled, he uses the old compiled version of connect.

Add a } to connect.java and the error in query.java will disappear

Grim
  • 1,938
  • 10
  • 56
  • 123