-2

I have an JApplet that connects to the database via this code:

    String url = "jdbc:mysql://localhost:3306/moodlele_survey";
    String user = "moodlele_garanti";
    String password = "garanti";



    try {
        panel.add( new JLabel("before driver"));
        Class.forName("com.mysql.jdbc.Driver");
        panel.add( new JLabel("failed"));
        Connection con = DriverManager.getConnection(url, user, password);

The applet is signed, so it has the right to connect databases. The database user is granted all previlleges and the username, password and database name is correct because I can connect to the database via PHP with the very same information. How can I fix it?

The printed stack error is: Access denied for user 'moodlele_garanti'@'localhost' (using password: YES)

  • Are you sure you're using the right text for your database, user and password? – Luiggi Mendoza Jul 16 '13 at 19:55
  • @RohitJain IMO no need for upvote nor downvote. – Luiggi Mendoza Jul 16 '13 at 19:56
  • I dunno, there's about a metric ton of dups, trying to find a good one. – Brian Roach Jul 16 '13 at 19:57
  • People down vote on a whim sometimes. Ignoring that, I have a question. Does this code works when it is not in an applet? That is, if you drop the connecting code to a class with a main, and if you invoke it from the command line, does it connect to the database with those credentials? – luis.espinal Jul 16 '13 at 19:57
  • Oh, actually ... the OP's *own dup* is a good choice – Brian Roach Jul 16 '13 at 19:58
  • Also, you should at least print the name of the exception, with a meaningful stack trace preferably. Was it a SQLException, a SecurityException? What exception did you get? Maybe you got downvoted because you omitted such a crucial piece of information (I wouldn't have downvoted but other people do, silly, I know.) – luis.espinal Jul 16 '13 at 19:59
  • This is working in a client server- my website's server. This code with my local database perfectly works. The suggestion I get was to change url to www.mywebsite.com:3306 however that brings the error: null, message from server: "Host '85.110.122.149' is not allowed to connect to this MySQL server" – user1570194 Jul 16 '13 at 19:59
  • The driver works because the applet shows "failed" label – user1570194 Jul 16 '13 at 20:01
  • Does the user `moodlele_garanti` have permissions to access the `moodlele_survey` database from `localhost`? Check your users in mysql. – Sotirios Delimanolis Jul 16 '13 at 20:01
  • 1
    *The suggestion I get was to change url to www.mywebsite.com:3306 however that brings the error* the problem is that the Applet is being downloaded to client and he can't access to the database in localhost (probably the user doesn't have a mysql database installed). How to solve this? Well... – Luiggi Mendoza Jul 16 '13 at 20:03
  • Yes, I have checked it and also the PHP code I wrote can connect to the database with the same user and password – user1570194 Jul 16 '13 at 20:03
  • You have a mysql server running on the same machine as your browser. That mysql server is telling you that user doesn't have access rights. You're now talking about some *other* mysql server running somewhere else, which this code has nothing to do with. – Brian Roach Jul 16 '13 at 20:04
  • No, mysql server is running on a remote host which is my website's host. I actually uploaded the project to my website via ftp and ran it. Although the applet is signed, it could not connect to the database. – user1570194 Jul 16 '13 at 20:06
  • 2
    See that word "localhost" in your code? That's the **local** host; Your (*local*) machine. Applets run in the browser. – Brian Roach Jul 16 '13 at 20:12
  • You seem to be unclear on where the connection is originating and where the target is. If this code is running on a remote client then it will not be able to connect to a database on your server unless you grant global access on the server database. – Jim Garrison Jul 16 '13 at 20:13
  • Sorry, I could not understand that? My website's client machine is also a localhost for the codes that run inside it uploaded via ftp like the logic in PHP: you write localhost and upload to the server, then it connects – user1570194 Jul 16 '13 at 20:14
  • The database belongs to the remote client, the code is uploaded in the client via ftp. Lets think it like that. I have written the code in my computer, then uploaded it to the host via ftp. Then I logged in to my servers mysql configuration page, created the user, authenticated it and I ran the code which is in a url like:www.mywebsite.com/applet.html then it could not connect – user1570194 Jul 16 '13 at 20:15
  • 2
    @user1570194 the problem is that PHP executed the server PHP code on, uhm, **the server where the application is deployed**. Your users access to your PHP site by `http://someUrl/appName/restOfUrl.php` which means that the PHP code is executed at `someUrl` server (or wherever this `someUrl` name points to), and looks like this server has installed the MySQL database in it thus not getting problems to access to the database. In the other hand, an Applet is downloaded to the client pc i.e. it will be executed as a Desktop application and your client doesn't have a mysql database in its pc. – Luiggi Mendoza Jul 16 '13 at 20:17
  • @user1570194 if this application is meant to run in a LAN inside the company, you **must** change `localhost` from the JDBC URL connection to `serverNameThatHasTheMySQLDatabase`. If it should be used outside of your LAN, then you have a huge design error that can't be easily solved. – Luiggi Mendoza Jul 16 '13 at 20:18
  • Then, would you recommend me to tell my host owner about that? Because as far as I understand from your comments, the applet connects to the database differently from PHP. So, the host owner might tell me the correct host and port to do that or the user? – user1570194 Jul 16 '13 at 20:29
  • @user1570194 that's not a problem of the host but your application design. – Luiggi Mendoza Jul 16 '13 at 20:35

1 Answers1

2

You don't really seem to grasp the technologies you're using, or how they work.

In the comments you state:

My website's client machine is also a localhost for the codes that run inside it uploaded via ftp like the logic in PHP: you write localhost and upload to the server, then it connects

That PHP is running on the webserver. That also happens to be the same host as your mysql server. So, yes localhost is correct in PHP. Your web browser on your computer at home connects to the webserver via TCP, the PHP is executed on the webserver, connects to the mysql server on the same host and all is well.

Java applets run in your browser on your machine. The applet is downloaded from the website to your machine at home, and it runs on that machine. The way your code is written in your question, it then attempts to connect to a mysql server on your machine at home. localhost is your computer.

In order for that applet to connect to the mysql server at your hosting company, it would need to attempt to connect to that host. In addition, the ports would need to be open to/on that server allowing a remote connection to the mysql server, mysql needs to be listening for connection on an external (public) IP address, and you would need to grant privileges for that user to be able to connect from a remote host.

It is probably worth mentioning that in general this is a very bad idea. Rarely is exposing your database to the world a good idea; one small mistake in granting access creates a very large problem. There are numerous other security issues. Do people do it? Sure. Still doesn't make it a good idea.

Typically an applet that is designed for public consumption is going to use an intermediary service (such as a web API or even RMI) to give you a fighting chance to lock things down.

Brian Roach
  • 76,169
  • 12
  • 136
  • 161