0

I am working on a mysql database, accessing from a Servlet program(through Eclipse) on Mac. My recent problem is, I couldn't access the mysql DB through my Servlet, which was working fine earlier. I use MySQLWorkBench, where the DB is running on Mac. When Servlet is trying to access it, it throws the below error. Actually, the password is empty. Working on Mac machine.

java.sql.SQLException: Access denied for user 'root'@'localhost' (using password: YES)

Servlet code is below:

// MySQL
    Connection conn = null;
    Properties connectionProps = new Properties();
    connectionProps.put("user", "root");        
    connectionProps.put("password", ""); // No password is set

        try {
            Class.forName("com.mysql.jdbc.Driver");
            conn = DriverManager.getConnection(
                       "jdbc:" + "mysql" + "://" +
                       "localhost" +
                       ":" + "3306" + "/InfoDB",
                       connectionProps);
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    System.out.println("Connected to database");
    // insert record
    try {
        Statement stmt = (Statement) conn.createStatement();

        String sql = "SELECT IP FROM InfoTable";
        ResultSet rs = stmt.executeQuery(sql);
        //STEP 5: Extract data from result set
        String ipaddress = null;
          while(rs.next()){
              //Retrieve by column name
              ipaddress  = rs.getString("IP");
              System.out.println("IP Address retrieved from DB: " + ipaddress);
          }
          PrintWriter out = response.getWriter();
          out.println(ipaddress);

    } catch (SQLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

The same code was working fine earlier. I saw some suggestions here, Solution for this issue , but i don't know where is this information.schema table resides and how to set the permission. And, another suggestion is there in this link, Solution for this issue But, i don't understand how to fix this way?

Could someone please guide me to fix this issue?

Community
  • 1
  • 1
Stella
  • 1,728
  • 5
  • 41
  • 95

3 Answers3

1

Your root account don't have a password. So, make a connection with no password.

connectionProps.put("user", "root");        
// Remove this line --> connectionProps.put("password", ""); // No password is set

Or update the password of root account and connect using that password.

MySQL:

GRANT ALL PRIVILEGES ON *.* TO  'root'@'localhost' IDENTIFIED BY 'your-password';

Then

connectionProps.put("user", "root");        
connectionProps.put("password", "your-password");
ninhjs.dev
  • 7,203
  • 1
  • 49
  • 35
  • Removing the line didn't solved the issue. Where I can apply "GRANT" command? I am running Mysqlworkbench on Mac machine. – Stella Feb 27 '14 at 14:16
  • @Catherine Just open a new query in MySQL Workbench and execute that command. – ninhjs.dev Feb 27 '14 at 15:27
  • Or try this for empty password: `String password = ""; //... connection = DriverManager.getConnection(url, "root", password);` – ninhjs.dev Feb 27 '14 at 15:32
0

I have answered this one in another thread, copying from there.

I had similar problem and tried for hours to find the solution. Finally, I was able to solve the issue by updating the mysql-connector version. Try the latest connector version (5.1.29) and let me know if his works for you. This might help if you have updated your MySQL server version.

techtabu
  • 23,241
  • 3
  • 25
  • 34
0

i`m a student from Midrand Graduate Institute,South Africa this my error i corrected by changing the password phrase in double quotes to blank mysqlbench saves the password automatically.

DriverManager.getConnection("jdbc:mysql://localhost:3306/books", "root", "password"); changed it to: DriverManager.getConnection("jdbc:mysql://localhost:3306/books", "root", "");

And : my output Listing all Books: Book - Id: 1, Book Title: Practical Clojure Book - Id: 2, Book Title: Beginning Groovy, Grails and Griffon Book - Id: 3, Book Title: Definitive Guide to Grails 2 Book - Id: 4, Book Title: Groovy and Grails Recipes Book - Id: 5, Book Title: Modern Java Web Development Book - Id: 6, Book Title: Java 7 Recipes Book - Id: 7, Book Title: Java EE 7 Recipes Book - Id: 8, Book Title: Beginning Java 7 Book - Id: 9, Book Title: Pro Java 7 NIO.2 Book - Id: 10, Book Title: Java 7 for Absolute Beginners Book - Id: 11, Book Title: Oracle Certified Java Enterprise Architect Java EE7 Book - Id: 12, Book Title: Beginning Scala ++++++++++++++++++++++++++++++ Searching Books by Keyword: Java Book - Id: 5, Book Title: Modern Java Web Development Book - Id: 6, Book Title: Java 7 Recipes Book - Id: 7, Book Title: Java EE 7 Recipes Book - Id: 8, Book Title: Beginning Java 7 Book - Id: 9, Book Title: Pro Java 7 NIO.2 Book - Id: 10, Book Title: Java 7 for Absolute Beginners Book - Id: 11, Book Title: Oracle Certified Java Enterprise Architect Java EE7

udeembno
  • 29
  • 1
  • 6