Win a copy of Mastering Corda: Blockchain for Java Developers this week in the Cloud/Virtualization forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Bear Bibeault
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Jj Roberts
  • Carey Brown
Bartenders:
  • salvin francis
  • Frits Walraven
  • Piet Souris

Database connection

 
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello!
I would like to have some help.
You see I have this server that I need to connect to for my project, but for some reason i just cannot?
Here is the thing:
I need to connect to the database to retrieve a simple word from now.
I know that I need this mysql connector which I have downloaded and inserted in my libraries, and I did the code that I could find on the internet being the most common thing I've found.
Unless I have done something wrong, this should theoretically work just fine right?

Well, apparently I just cannot! I receive an error message displaying the code displayed on my console followed with the network of my university (which I will not display).
I have on the other hand attached two screenshots, one with the code and the other one with the issue I was talking about.

The idea would be later to use the logic to implement it on to an android app. I initially have tried this on my app, but did as well on eclipse just to make sure that it wasn't related to the platform, but indeed the code or package, or whatever might be causing he issue.

Could anyone please help me go through it til i manage to get at least one word out of this database please?
Screenshot-2020-12-01-at-15.35.19.png
[Thumbnail for Screenshot-2020-12-01-at-15.35.19.png]
 
Darius Boteand
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi again, I forgot to add the screenshot with the output error, so here it is:
Screenshot-2020-12-01-at-15.50.04.png
[Thumbnail for Screenshot-2020-12-01-at-15.50.04.png]
 
Darius Boteand
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Also I felt interesting to add the logs from android studio to show you that i am having the exact same issue, so here is a screenshot.
Either ways I need this solved on eclipse first. then I'll adapt it to my project.
Screenshot-2020-12-01-at-15.56.34.png
[Thumbnail for Screenshot-2020-12-01-at-15.56.34.png]
 
Saloon Keeper
Posts: 23006
156
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
Please don't post screenshots. Copy and paste the text itself. Screenshots tend to be hard to read, waste a lot of space on our servers and sometimes don't display at all. In fact, I can't see them right now on the "answer" editor page.

If you have formatted text, wrap it in Code tags (use the Code button on our message editor).

It looks like your machine isn't authorized to connect to the MySQL server and you need a database administrator to correct that. But that's just a quick guess, since everything's a blur or invisible.

Also, lose the Class.forName(). That hasn't been necessary since probably 2005. It's time people stopped repeating it!
 
Rancher
Posts: 4784
50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That would be my guess as well.
MySQL (if I remember it correctly) defaults to not allowing anyone to connect to it outside of the machine it is deployed on, so you have to set it up to allow network connections.
 
Tim Holloway
Saloon Keeper
Posts: 23006
156
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

Dave Tolls wrote:That would be my guess as well.
MySQL (if I remember it correctly) defaults to not allowing anyone to connect to it outside of the machine it is deployed on, so you have to set it up to allow network connections.



MySQL is extremely paranoid.

Aside from the fact that you have to set a configuration option for it to accept external connections, there are fine-grained access rights on MySQL objects themselves. To the point where I can deternine not only which databases and tables a user can access and how, but even what host(s) they can access them from.

The text of the error message isn't definitive, but I believe that that MySQL server is configured to accept external connections, just not from the user's machine.
 
Darius Boteand
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi! thanks for your explanations it starts to be a little more clear for me! so what exactly would I need to modify within that mysql server?
There aren't many tables, here is what I have:
| SandboxEnjoy       |
| information_schema |
| mysql              |
| performance_schema |
| phpmyadmin         |
| sys                |

The only database I have altered is the mysql one for a new user that I have created, to give him all the privileges. But it did not solved it.
Now I understand why.
Though where do i need to edit to allow this user to access from the outside world?

Many thanks for your involvement !!!
 
Tim Holloway
Saloon Keeper
Posts: 23006
156
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
You don't edit tables, you use the "GRANT" verb to authorize and the best advice I can give you on that is to read the MySQL manual because it's as complex as a SELECT or UPDATE.

Those "tables", you listed, incidentally are databases. A MySQL server instance can contain many databases, each database can contain many tables. The "mysql" and "information_schema" databases are built into MySQL to allow better management of the database, and technically, a GRANT operation does modify stuff in the "mysql" database, but it's better to let GRANT do it than to try and handle it manually.
 
Darius Boteand
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi!
Now this is listed hence I logged into it as a root. The regular user does not have all these listed, but the SandboxEnjoy one.

Alright, I will try to read that manual. I did on the other hand use the GRANT for the user, but can't manage to make it work... Perhaps there is a little bit more than just that, so I will spend some time on it and come back here if I still have any difficulties.

Thanks for the advices and explanation!
 
Dave Tolls
Rancher
Posts: 4784
50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When you granted the user orivs did you grant them to '<USERNAME>' or did you grant them to '<USERNAME>'@'<IP>'?
 
Tim Holloway
Saloon Keeper
Posts: 23006
156
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

Dave Tolls wrote:When you granted the user orivs did you grant them to '<USERNAME>' or did you grant them to '<USERNAME>'@'<IP>'?



Should not be a problem. According to the docs:

MySQL Documentation wrote:
The simple form 'user_name' is a synonym for 'user_name'@'%'.



However it is more secure to include a hostname or hostname pattern (other than "%").

Also note those single quotes around 'username' and 'host'! If you forget them, things may not work!
 
Tim Holloway
Saloon Keeper
Posts: 23006
156
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
Incidentally, I think you can grant full access to a database with something like this:


And for that matter, make sure you defined the user via a CREATE USER command.
 
Darius Boteand
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi!
Ok so the server does have now a 'phpmyadmin' and various users with all the grants.
The error persists to appear!
Here is the java code that I have used (note that i removed the ip):
-----------------------------------------

-----------------------------------------
The error message that I get is (please note that 'pma'@ is followed by my ip.internetProviderNetwork): Error message - Access denied for user 'pma'@'____' (using password: YES)

What could this be caused by other than what has been said above?
 
Tim Holloway
Saloon Keeper
Posts: 23006
156
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
That is the message you'll see if GRANT rights weren't given to 'pma'@'hostname', where 'hostname' is either the hostname of your client (PHP) machine or is a wildcard such as '%' (not recommended!) or, I think, a subnet such as '192.168.1.%'
 
Darius Boteand
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi!
Thank you for the advice!
Alright, so I did run these two sql commands:
create user 'final'@'%' identified by 'finalresort666'; grant all privileges on Sandbox.* to 'final'@'%' with grant option;

Though now I am receiving this error message:
Error message - Access denied for user 'final'@'%' to database 'SandboxEnjoy/'

Do you think that this could be caused by the java code itself?
 
Darius Boteand
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Though I do not really understand what you mean by the subnet '192.168.1.%'. Could you tell me a little more about it please?
 
Darius Boteand
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Actually, please disregard my last two messages.
I made it work, thank you very much for your help!!! it helped a lot!!!
 
I am Arthur, King of the Britons. And this is a tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic