0

This is my code. This is my code.

And this is the error i am getting. and this is my error.

Even though i added mysql connector to the database, which was the common solution for this problem, but i am still having it. Any advices?

Thanks in advance!

Greddy
  • 27
  • 3
  • use this `Class.forName("com.mysql.jdbc.Driver");` And rather than uploading pictures post your code. – Silverfang Mar 05 '20 at 13:10
  • Oh, sorry. I will! This is what i got after i changed it with your suggestion: java.sql.SQLException: Access denied for user ''@'localhost' (using password: NO) . – Greddy Mar 05 '20 at 13:31

1 Answers1

1

Since Java 1.6, JDBC 4.0 API provides a new feature to discover java.sql.Driver automatically and therefore Class.forName is no longer required.

Assuming login is the name of your database, use the following code in getConnection:

return DriverManager.getConnection("jdbc:mysql://localhost/login?user=dbuser&password=dbpassword");
Arvind Kumar Avinash
  • 62,771
  • 5
  • 54
  • 92
  • Now i got this one: java.sql.SQLException: Access denied for user 'dbuser'@'localhost' (using password: YES). Different than before, now it has using password: yes.. – Greddy Mar 05 '20 at 16:46
  • Great! Do you have a database user as, `dbuser` with password as `dbpassword` with the required privileges? If not, first create them and test using a SQL client whether you can login to your database with these credentials. Check https://stackoverflow.com/questions/1720244/create-new-user-in-mysql-and-give-it-full-access-to-one-database for help. – Arvind Kumar Avinash Mar 05 '20 at 16:52
  • @Greddy - I hope the solution worked for you. Do not forget to accept the answer so that future visitors can also use the solution confidently. Check https://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work to learn how to do it. Feel free to comment in case of any doubt/issue. – Arvind Kumar Avinash May 22 '20 at 11:06