I'm making a java project with SceneBuilder and Eclipse; i need to connect to a local database created using MySQL, but after running my Java code in eclipse i get the following error:
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.DriverManager.getConnection(Unknown Source)
at java.sql.DriverManager.getConnection(Unknown Source)
at com.mysql.cj.jdbc.admin.TimezoneDump.main(TimezoneDump.java:70)
Instead of 'root'@'localhost' (using password: NO)
that is a login credentials error, now i have ''@'localhost' (using password: NO)
.
Using MySQL workbench and command prompt, i can correctly access using "root"/"root", and launch queries on my db. Running the same Eclipseproject on another laptop or my home pc, i can run it without errors. So the problem is not in the code but in some setting of my current laptop.
I already tried to:
- Uninstall and reinstall Eclipse.
- Uninstall and reinstall mySQL products: Workbench, Server, J Connector.
- Switch Eclipse workspace.
- Open the 3306 port from windows firewall.
That's the part of the code where i try to connect to my db.
package model;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
public class UserDAO {
private static String USER = "root";
private static String PASS = "root";
private static String DB_URL = "jdbc:mysql://127.0.0.1:3306/beecological?useUnicode=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC";
private static String DRIVER_CLASS_NAME = "com.mysql.cj.jdbc.Driver";
public static boolean checkUsername(String username) {
Statement stmt = null;
Connection conn = null;
int res = 1;
try {
Class.forName(DRIVER_CLASS_NAME);
conn = DriverManager.getConnection(DB_URL, USER, PASS);
stmt = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,
ResultSet.CONCUR_READ_ONLY);
res = Queries.verifyUsernameAvailable(stmt, username);
stmt.close();
conn.close();
}catch (Exception e) {
e.printStackTrace();
}
if(res == 1) {
return false;
}
return true;
}
public static void saveUser(User instance) {
Statement stmt = null;
Connection conn = null;
try {
Class.forName(DRIVER_CLASS_NAME);
conn = DriverManager.getConnection(DB_URL, USER, PASS);
stmt = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,
ResultSet.CONCUR_READ_ONLY);
Queries.insertUser(stmt, instance);
stmt.close();
conn.close();
}catch (Exception e) {
e.printStackTrace();
}
}
public static boolean verifyLogin(User instance) {
Statement stmt = null;
Connection conn = null;
int res = 0;
try {
//caricamento driver mysql
Class.forName(DRIVER_CLASS_NAME);
conn = DriverManager.getConnection(DB_URL, USER, PASS);
stmt = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,
ResultSet.CONCUR_READ_ONLY);
res = Queries.verifyUserRegistered(stmt, instance);
stmt.close();
conn.close();
}catch (Exception e) {
e.printStackTrace();
}
if (res == 0) {
return false; //utente immesso non esiste
}
return true;
}
}