There is an error , it is cannot find Symbol

import java.sql.*;
public class Main {
    public static void main(String[] args) {
        try {
            Class.forName("com.mysql.jdbc.Driver");
            String connectionUrl = "jdbc:mysql://localhost/mytestdb?" +
                                   "user=root&password=PHP";
            Connection con = DriverManager.getConnection(connectionUrl);
        } catch (SQLException e) {
            System.out.println("SQL Exception: "+ e.toString());
        } catch (ClassNotFoundException cE) {
            System.out.println("Class Not Found Exception: "+ cE.toString());
        }
    
     Statement stmt = null;
            ResultSet rs = null;
            //SQL query command
            String SQL = "SELECT * FROM user";
            stmt = con.createStatement();
            rs = stmt.executeQuery(SQL);
            while (rs.next()) {
                System.out.println(rs.getString("username") + " : " + rs.getString("password"));
            }
    
    }

}

Recommended Answers

All 8 Replies

On which line?
Or shall I guess? ... I see a line number with two digits ... the first looks like a one, the second is .... wait for it ... a nine?

On which line?

symbol : variable con
location: class mytestapps.Main
stmt = con.createStatement();

That was a good guess, 19 is my lucky number.
Anyway - you declared con in a try{} block - so its scope is limited to that block. Line 19 is outside the block, so con is out of scope. It's all a question of where you declare it...

That was a good guess, 19 is my lucky number.
Anyway - you declared con in a try{} block - so its scope is limited to that block. Line 19 is outside the block, so con is out of scope. It's all a question of where you declare it...

public class Main {

    /**
     * @param args the command line arguments
     */
   static Connection con;
  // Connection con;
    public static void main(String[] args) {
       // Connection con=null;
        try {
            Class.forName("com.mysql.jdbc.Driver");
            String connectionUrl = "jdbc:mysql://localhost/mytestdb?" +
                                   "user=root&password=PHP";
             con = DriverManager.getConnection(connectionUrl);
        } catch (SQLException e) {

i also though that but then it gives another error ,it says
unreported exception java.sql.SQLException; must be caught or declared to be thrown
stmt = con.createStatement();

can u plz tell me how to declared that, may be i declared in a wrong way plz correct my error

... or declare it within the main method without the static.

Your new error says it all - how can I make "java.sql.SQLException; must be caught or declared to be thrown" any clearer?
You have to catch SQLException OR declare the enclosing method as throwing this exception (wrong choice!).

... or declare it within the main method without the static.

Your new error says it all - how can I make "java.sql.SQLException; must be caught or declared to be thrown" any clearer?
You have to catch SQLException OR declare the enclosing method as throwing this exception (wrong choice!).

it is working Thnaks a lot JamesCherrill,i used catch SQLException .it is working, this is my first time i made a database connection with java,Thanks a lot, i m relly happy, Thanks a lot again

OK that's great. Mark this thread "solved"?

OK that's great. Mark this thread "solved"?

Ok, Thanks

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.