1You have to grant priviliges to root@localhost
2e.g
3GRANT ALL PRIVILIGES ON *.* TO 'root'@'localhost' IDENTIFIED BY password
4
5(*.*: means everything, it can be modified to some specific DB or opertations)
6
1As you are creating a database from scratch, you could use:
2
3Connection connection = DriverManager.getConnection("jdbc:mysql://localhost/?user=root&password=rootpassword");
4PreparedStatement ps = connection.prepareStatement("CREATE DATABASE databasename");
5int result = ps.executeUpdate();
6
1java.sql.SQLException: Access denied for user 'root'@'localhost' (using password: YES)
2 at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1074)
3 at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4074)
4 at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4006)
5 at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:919)
6 at com.mysql.jdbc.MysqlIO.proceedHandshakeWithPluggableAuthentication(MysqlIO.java:1694)
7 at com.mysql.jdbc.MysqlIO.doHandshake(MysqlIO.java:1244)
8 at com.mysql.jdbc.ConnectionImpl.coreConnect(ConnectionImpl.java:2397)
9 at com.mysql.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:2430)
10 at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2215)
11 at com.mysql.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:813)
12 at com.mysql.jdbc.JDBC4Connection.<init>(JDBC4Connection.java:47)
13 at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
14 at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
15 at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
16 at java.lang.reflect.Constructor.newInstance(Unknown Source)
17 at com.mysql.jdbc.Util.handleNewInstance(Util.java:411)
18 at com.mysql.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:399)
19 at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:334)
20 at java.sql.DriverManager.getConnection(Unknown Source)
21 at java.sql.DriverManager.getConnection(Unknown Source)
22 at db.Database.<init>(Database.java:91)
23 at db.Main.main(Main.java:10)