0

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"));
            }
    
    }

}
2
Contributors
8
Replies
9
Views
6 Years
Discussion Span
Last Post by MrBlack
0

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?

Edited by JamesCherrill: n/a

0

On which line?

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

0

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...

0

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

Edited by MrBlack: n/a

0

... 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!).

0

... 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

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.