0

Attempting to connect to MySQL via Java. Here is code, error trace copied below. I attempted to have the program accept a password from the user, but it's not even getting that far. I've tried hard coding a password and double checking that I'm on the right port. Additionally, the error begins with a timezone dump error. Again, I'm clueless there. Any help would be appreciated. I'm very new to JDBC so if there are any concepts I might be overlooking or unaware of please let me know.

import java.sql.*;
import java.util.Scanner;

public class DataConnect
{
    private Connection con;
    private Scanner sc;
    
    public DataConnect()
    {
        String p;
        sc = new Scanner(System.in);
        try
        {
            System.out.println("enter password");
            p = sc.next();
            Class.forName("com.mysql.cj.jdbc.Driver");
            con = DriverManager.getConnection("jdbc:mysql://localhost:3306/testdb","root", p);
            System.out.println("connection established"); 
        }
        catch (ClassNotFoundException e)
        {
            System.out.println("CNF exception");
            e.printStackTrace();
        }
        catch (SQLException e)
        {
            System.out.println("SQL exception");
            e.printStackTrace();
        }
    }
    
    public static void main(String[] args)
    {
        DataConnect d = new DataConnect();
    }
}

Error stack trace:

Exception in thread "main" java.sql.SQLException: Access denied for user ''@'localhost' (using password: NO)
    at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:129)
    at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:97)
    at com.mysql.cj.jdbc.exceptions.SQLExceptionsMapping.translateException(SQLExceptionsMapping.java:122)
    at com.mysql.cj.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:836)
    at com.mysql.cj.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:456)
    at com.mysql.cj.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:246)
    at com.mysql.cj.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:197)
    at java.sql/java.sql.DriverManager.getConnection(DriverManager.java:677)
    at java.sql/java.sql.DriverManager.getConnection(DriverManager.java:251)
    at com.mysql.cj.jdbc.admin.TimezoneDump.main(TimezoneDump.java:70)
5
  • ' '@'localhost ' is an anonymous user. Create a new user in the database and use it. It looks like failed access is reverting to the anonymous user. Commented Jul 5, 2020 at 23:18
  • 1
    What is TimezoneDump? Because your posted code is DataConnect. And that isn't what your exception is coming from. Commented Jul 5, 2020 at 23:58
  • @ElliottFrisch you're right. Not sure what that is about. Will do some digging and attempt to get back with an answer. Commented Jul 6, 2020 at 0:40
  • You seem to be running the wrong program: the TimezoneDump application of the MySQL Connector/J library instead of your own application. Commented Jul 6, 2020 at 15:20
  • @MarkRotteveel Yep, I wasn't even running my application. Just had to specify it was my application I wished to run (via right click on .java in Eclipse). facepalm Commented Jul 8, 2020 at 22:45

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.