You have to grant priviliges to root@localhost
e.g
GRANT ALL PRIVILIGES ON *.* TO 'root'@'localhost' IDENTIFIED BY password
(*.*: means everything, it can be modified to some specific DB or opertations)
You can flush the root user's priviliges in mysql cli.
> ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'password';
> FLUSH PRIVILEGES;
As you are creating a database from scratch, you could use:
Connection connection = DriverManager.getConnection("jdbc:mysql://localhost/?user=root&password=rootpassword");
PreparedStatement ps = connection.prepareStatement("CREATE DATABASE databasename");
int result = ps.executeUpdate();