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)
' '@'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.TimezoneDump? Because your posted code isDataConnect. And that isn't what your exception is coming from.TimezoneDumpapplication of the MySQL Connector/J library instead of your own application.