• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Cannot connect to the SQL Database through Java Eclipse

 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
FULL CODE IS IN THESE LINKS:

http://www.newthinktank.com/2012/04/java-video-tutorial-34/

http://www.newthinktank.com/2012/04/java-video-tutorial-36/

Hey guys, i am following this Java Tutorial 36 from Derek Banas on Youtube.

The problem:

When i try to acces the MySQL database with code from Java Tutorial 34 i can query the lahman2016 database without any problem and the data i pulled gets shown in the eclipse log.

When i try to acces the MySQL database with code from Java Tutorial 36 i get the exception code below

I am using a windows 10 operating system + MySQL 8.0

The only thing different in Java Tutorial 36 is that he wants to put the pulled data from MySQL into Java Tables, but that shouldn't cause this error.

EDIT: I have reinstalled MySQL and still have the same problem.

Also applied this https://stackoverflow.com/questions/50177216/how-to-grant-all-privileges-to-root-user-in-mysql-8-0/50197630 didnt work either.







 
Sheriff
Posts: 7125
184
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Is the user actually '@'localhost?  I would think it would just be localhost.

If that doesn't help, we'll need to see the code that produces that error.  Or better yet, write an SSCCE (that's a link) that demonstrates the problem.
 
Marshal
Posts: 28177
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Wensley, welcome to the Ranch!

You posted some code:



which gets a connection, using user ID "root" and an associated password. But on the other hand your error message:

MySQL wrote:Access denied for user ''@'localhost' (using password: NO)



which says that somebody tried to get a connection using no user ID and no password.

So it seems that the code you posted isn't the code which is causing the exception.

And no, I didn't watch the tutorial. Video is such a horribly linear format, whereas if it was a text-based tutorial I could just search for the relevant section.

EDIT: I see you edited your post to link to a text-based tutorial as well as the video tutorial. That's a useful change but it would also be useful in future if you could insert a comment (like this one) when you edit one of your posts.
 
Wensley Brands
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Paul Clapham wrote:Hi Wensley, welcome to the Ranch!

You posted some code:



which gets a connection, using user ID "root" and an associated password. But on the other hand your error message:

MySQL wrote:Access denied for user ''@'localhost' (using password: NO)



which says that somebody tried to get a connection using no user ID and no password.

So it seems that the code you posted isn't the code which is causing the exception.

And no, I didn't watch the tutorial. Video is such a horribly linear format, whereas if it was a text-based tutorial I could just search for the relevant section.

EDIT: I see you edited your post to link to a text-based tutorial as well as the video tutorial. That's a useful change but it would also be useful in future if you could insert a comment (like this one) when you edit one of your posts.



Wow very weird, there was no reply button underneath my post until you 2 guys replied my bad sorry! Anyway could it be that MySQL settings are not correct?

When i try logging in with my sqladmin account instead of my root account both tutorial 34 and 36 wont work i get the vendor error:

SQL Exception: Access denied for user 'sqladmin'@'localhost' (using password: YES)
Vendor Error: 1045

 
Paul Clapham
Marshal
Posts: 28177
95
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Wensley Brands wrote:Anyway could it be that MySQL settings are not correct?



It certainly could (for some definition of "not correct"). It's clear that your MySQL installation is set up with user IDs and passwords. I would expect that you did that at some point, or was somebody else involved in the setup? Anyway to use your MySQL you're going to have to provide a user ID and password which matches a user ID which it knows about.
 
Wensley Brands
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Paul Clapham wrote:

Wensley Brands wrote:Anyway could it be that MySQL settings are not correct?



It certainly could (for some definition of "not correct"). It's clear that your MySQL installation is set up with user IDs and passwords. I would expect that you did that at some point, or was somebody else involved in the setup? Anyway to use your MySQL you're going to have to provide a user ID and password which matches a user ID which it knows about.



No i installed it myself on my desktop pc. I will reinstall it to see if it fixes this issue. Yes i did use an ID and Password which is shown in the code to access the MySQL DB user: root pass: r00t1
 
Wensley Brands
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Would it help to screenshot a certain part of MySQL settings ?
 
Saloon Keeper
Posts: 27752
196
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Wensley Brands wrote:Would it help to screenshot a certain part of MySQL settings ?



It would help a a lot more if you'd stop using media-intensive examples. Just copy-and-paste the text. No videos, no screenshots. No singing fairies and no dancing flowers. Not unless we ask specifically.

It's just a lot easier for us to read plain text (with code tags) than have to deal with a circus.

Also, Class.forName is long obsolete. You can completely delete that line.

For specifics on how to form MySQL JDBC URLS: https://dev.mysql.com/doc/connector-j/8.0/en/connector-j-reference-jdbc-url-format.html

It's a pretty "busy" page itself, but it's quite detailed.
 
Wensley Brands
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Tim Holloway wrote:

Wensley Brands wrote:Would it help to screenshot a certain part of MySQL settings ?



It would help a a lot more if you'd stop using media-intensive examples. Just copy-and-paste the text. No videos, no screenshots. No singing fairies and no dancing flowers. Not unless we ask specifically.

It's just a lot easier for us to read plain text (with code tags) than have to deal with a circus.

Also, Class.forName is long obsolete. You can completely delete that line.

For specifics on how to form MySQL JDBC URLS: https://dev.mysql.com/doc/connector-j/8.0/en/connector-j-reference-jdbc-url-format.html

It's a pretty "busy" page itself, but it's quite detailed.



That's exactly what i wanted to do when i first started the post but i am a new user i dont have any Pie's that allows me to post a lot of code lines, sorry for all the links!
 
Tim Holloway
Saloon Keeper
Posts: 27752
196
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Wensley Brands wrote:
That's exactly what i wanted to do when i first started the post but i am a new user i dont have any Pie's that allows me to post a lot of code lines, sorry for all the links!



???

Did we start limiting how much text can be posted? Nobody told me! We routinely run through long stack traces here. All we've ever really asked when posting code samples is to keep them pared down to the suspicious parts, but that's because once it doesn't all fit on the screen, it's hard to read.

Anyway, I hope the official link helped some.
 
Paul Clapham
Marshal
Posts: 28177
95
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Wensley Brands wrote:No i installed it myself on my desktop pc. I will reinstall it to see if it fixes this issue. Yes i did use an ID and Password which is shown in the code to access the MySQL DB user: root pass: r00t1



It may be that the tutorial told you to use that ID and password in the code. However if you didn't actually go into MySQL as admin and set that ID up with that password, then it isn't going to work.

Reinstalling MySQL is not going to help. It's a relatively simple matter to manage users and their passwords in MySQL so that's the direction you should go in.
 
Knute Snortum
Sheriff
Posts: 7125
184
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Tim Holloway wrote:Did we start limiting how much text can be posted? Nobody told me!


I believe we did.  This is one of the reasons I suggested writing an SSCCE (that's a link).  With a short example that demonstrates the problem (at least on the OP's side) we might be able to help.  It will also help the OP diagnose the problem themselves.
 
Knute Snortum
Sheriff
Posts: 7125
184
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, SSCCE is not a link because it was linked the first time only.  So here is the URL: http://sscce.org/
 
Wensley Brands
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
EDIT: Quoted the wrong person!
 
Wensley Brands
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Paul Clapham wrote:

Wensley Brands wrote:No i installed it myself on my desktop pc. I will reinstall it to see if it fixes this issue. Yes i did use an ID and Password which is shown in the code to access the MySQL DB user: root pass: r00t1



It may be that the tutorial told you to use that ID and password in the code. However if you didn't actually go into MySQL as admin and set that ID up with that password, then it isn't going to work.

Reinstalling MySQL is not going to help. It's a relatively simple matter to manage users and their passwords in MySQL so that's the direction you should go in.



The complete code is now included in this post, hope it fits your sscce standard.

I have created another account called sqladmin with all the privéleges and instead of using:

conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/lahman2016","root", "r00t");

I now use:

conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/lahman2016","sqladmin", "SQLPASSWORD");

and still the same Exception.

Also when i test the connection on MySQL it says succes! This just makes 0 sense duo to the fact it connects smoothly in tutorial 34 :(

 
Paul Clapham
Marshal
Posts: 28177
95
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Wensley Brands wrote:Also when i test the connection on MySQL it says succes! This just makes 0 sense duo to the fact it connects smoothly in tutorial 34 :(



If "in tutorial 34" refers to some other person running code on some other computer, then that means nothing. However if it refers to some code running on your computer, then it's worth looking into. Do you have two different code examples which use the same user ID and password but, which when run on your computer, have different results with respect to being allowed to connect?

It's possible you didn't notice that the MySQL permissions system allows you to specify which hosts a user may connect from, and thus haven't set up your user ID to be allowed to connect from localhost.
 
Knute Snortum
Sheriff
Posts: 7125
184
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Wensley Brands wrote:The complete code is now included in this post, hope it fits your sscce standard.


Oops, it look like you didn't include the code.  Just post it as text into a reply and UseCodeTags (that's a link).
 
Wensley Brands
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In Java lesson 34 i connect to the MySQL database and i connect without any problems, the data shows up in my eclipse log.
The sqladmin account i use to log in with is made within MySQL workbench and i gave it all the privéleges possible just like someone told me to, instead of using the root account.



In Java lesson 36 i use the same connection code from JavaLesson34 but this time the data gets pulled and transfered into a Java tabel. Some how it will not let me connect and gives me the SQL exception below.



 
Wensley Brands
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Paul Clapham wrote:

Wensley Brands wrote:Also when i test the connection on MySQL it says succes! This just makes 0 sense duo to the fact it connects smoothly in tutorial 34 :(



If "in tutorial 34" refers to some other person running code on some other computer, then that means nothing. However if it refers to some code running on your computer, then it's worth looking into. Do you have two different code examples which use the same user ID and password but, which when run on your computer, have different results with respect to being allowed to connect?

It's possible you didn't notice that the MySQL permissions system allows you to specify which hosts a user may connect from, and thus haven't set up your user ID to be allowed to connect from localhost.



I have tried typing GRANT ALL ON *.* TO 'sqladmin'@'localhost'; on my Command Line Client and it says query OK. But still does not grant me access.
 
Paul Clapham
Marshal
Posts: 28177
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It still remains to be answered why the stack trace you posted documents a connection attempt with no user ID and password:

Exception in thread "main" java.sql.SQLException: Access denied for user ''@'localhost' (using password: NO)



It also remains to be answered why the stack trace doesn't mention any of your code in it. In other words, what code tried to make that connection and why? I don't see any connection between that code (which is using a TimezoneDump class from the JDBC driver) and your JavaLesson36 class.
 
Wensley Brands
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Paul Clapham wrote:It still remains to be answered why the stack trace you posted documents a connection attempt with no user ID and password:

Exception in thread "main" java.sql.SQLException: Access denied for user ''@'localhost' (using password: NO)



It also remains to be answered why the stack trace doesn't mention any of your code in it. In other words, what code tried to make that connection and why? I don't see any connection between that code (which is using a TimezoneDump class from the JDBC driver) and your JavaLesson36 class.



Ok, this answer was stuck in my head, so i did the following things:

I reinstalled Eclipse and went from JDK 8.0 to JDK 11.3

In advanced system settings -> enviromental variables... i created a new user variable to the jdk 11.3

And because you said something about TimeZoneDump class i synced my time zone in MySQL commandline with:  SET GLOBAL time_zone = 'SYSTEM';

It then gave me an error saying it can't recognize west europe time zone it represents mulitply country's, so i did SET GLOBAL time_zone = '+2:00'; <- which is weird i did this before i created this post and it did not work.

I did all of the above and now it's working 100 %.

Thanks for all the input guys i appreciate it, now i can finnaly sleep again and continue this tutorial :D
 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello , any way to print a text file content to a JTeaxtArea , please help
 
Knute Snortum
Sheriff
Posts: 7125
184
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ilham blanchete: please create a new thread (topic) for your question in the Swing forum and TellTheDetails (that's a link).
 
Paul Clapham
Marshal
Posts: 28177
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Wensley Brands wrote:In advanced system settings -> enviromental variables... i created a new user variable to the jdk 11.3

And because you said something about TimeZoneDump class i synced my time zone in MySQL commandline with:  SET GLOBAL time_zone = 'SYSTEM';

It then gave me an error saying it can't recognize west europe time zone it represents mulitply country's, so i did SET GLOBAL time_zone = '+2:00'; <- which is weird i did this before i created this post and it did not work.

I did all of the above and now it's working 100 %.



Well good, I'm glad you got that sorted out. Good work. But weird, yeah. I didn't do any messing about with time zones when I installed MySQL, I just let it (and Java, and Eclipse) figure out the time zone from the settings in the operating system. Anyway... yeah, weird.
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello, I'm a longtime user of this site, but was only really lurking on here with no identity. I decided to make an account to show explain how to fix this issue (at least for me).

How I found the problem?
I was struggling with this issue for quite a while too. When looking at the stack-trace, I noticed that even if I had no code running in my main method (except a System.out.print message) , my message still wouldn't print. I did a bit of digging into it and realised that the referenced library also contained a "main" method. You can find this in here "com.mysql.cj.jdbc.admin.TimezoneDump". Upon finding this, it was obvious that my main method wasn't being run at all.

The Solution(s)
After realising that this was the issue, the most logical solution I could come up with was run-time configurations. Looking at this I noticed that for some reason, Eclipse had made the TimezoneDump class main run instead of my code.
One of the solutions to fix this is to delete this specific run-time configuration.
The other solution (And easiest) is to simply run your code directly. Personally, I choose the latter because I didn't want to have to click twice every time I ran my code.

I hope this helps you guys, it was a big face-palm for me when I noticed that this was the issue. Thank you for all your contributions towards helping me debug code in the past.

Cheers!
~ Sam
 
This guy is skipping without a rope. At least, that's what this tiny ad said:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic