0

package com.exmaven2;

import java.sql.*;


public class MainApp 
{
    public static void main(String[] args) 
    {
        String url = "jdbc:mysql://localhost:3306/baseune"; 
        String userName = "utilisateur1";
        String password = "password";
        
        try { 
            java.sql.Connection con = DriverManager.getConnection(url, userName, password); 
        } catch (SQLException e) {
            e.printStackTrace();
        }   
    }
}

Hello, I'm actually trying to connect to MySQL with a Maven project build in Eclipse IDE. I wrote this code that should let me connect to my server but I'm getting the error described in the title. I just began learning about databases and MySQL so I don't really know what to do. I already checked the privileges for 'utilisateur1' (somebody else posted the same question but this answer didn't work for me) and i have them all so I don't understand why it doesn't work.

Andres Sacco
  • 650
  • 3
  • 13
  • I don't know if it's connected with your problem but you need to add the driver - Class.forName("com.mysql.jdbc.Driver"); – Andres Sacco Jan 06 '21 at 21:09
  • I just have to write this in the main() ? – Georges Dupont Jan 06 '21 at 23:04
  • Add the block of code inside your try/catch – Andres Sacco Jan 06 '21 at 23:06
  • I got the following error : Loading class `com.mysql.jdbc.Driver'. This is deprecated. The new driver class is `com.mysql.cj.jdbc.Driver'. The driver is automatically registered via the SPI and manual loading of the driver class is generally unnecessary. - Actually idk if it is an error or not, it is written in red – Georges Dupont Jan 06 '21 at 23:22

1 Answers1

0

could you please try this,

package com.exmaven2;

import java.sql.*;
public class MainApp 
{
    public static void main(String[] args) 
    {
        String url = "jdbc:mysql://localhost:3306/baseune?useSSL=false"; 
        String userName = "utilisateur1";
        String password = "password";
        
        try { 
            Connection con = DriverManager.getConnection(url, userName, password); 
        } catch (SQLException e) {
            e.printStackTrace();
        }   
    }
}
Gomathy M
  • 338
  • 2
  • 10
  • First of all, thanks for answering. I tried it and I got the following error : java.sql.SQLNonTransientConnectionException: Public Key Retrieval is not allowed – Georges Dupont Jan 06 '21 at 22:59
  • your welcome, ok could you please try this String url = "jdbc:mysql://localhost:3306/baseune?allowPublicKeyRetrieval=true&useSSL=false"; – Gomathy M Jan 06 '21 at 23:53
  • The previous error disappeared but the "SQLException : Access denied for user (...)" is still here... – Georges Dupont Jan 07 '21 at 00:29
  • in mysql could you please try by granting privileges e.g GRANT ALL PRIVILEGES ON baseune.* TO 'utilisateur1'@'localhost'; – Gomathy M Jan 07 '21 at 00:38
  • I've got : "Error code: 1410. You are not allowed to create a user with GRANT" I selected current_user() which is 'root' so I should be allowed idk why it says not allowed – Georges Dupont Jan 07 '21 at 09:35
  • [link]https://stackoverflow.com/questions/50177216/how-to-grant-all-privileges-to-root-user-in-mysql-8-0 could you please refer this – Gomathy M Jan 07 '21 at 13:04
  • I created a new user and the connection worked. Thanks for your help – Georges Dupont Jan 07 '21 at 13:41