Mysql

Hi,

I made a small game which uses mysql database. The game just retrieves data from the mysql database and update data aswell. It just stores the float data of the spatial’s position and updates it. The database works properly in my laptops localhost, but when I made a free host database online and tried to connect there it gives an error, below is the error:



“com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure

The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.”



this is the class for connecting to the database:

[java]

package ClientSide.login;



import java.sql.*;



public class loginInfo {

public String JDBC_DRIVER = “com.mysql.jdbc.Driver”;

public String DATABASE_URL = “jdbc:mysql://localhost:1433/link-to-my-database”;



public Connection conn = null;

public Statement state = null;



public boolean isExit(String user, String pass)

{

boolean exist = false;



try{

Class.forName(JDBC_DRIVER);

conn = DriverManager.getConnection(DATABASE_URL,“root”,“password”);

state = conn.createStatement();

ResultSet resultSet = state.executeQuery(“Select * FROM login WHERE user = '”+user+"’ AND pass = ‘"+pass+"’");

int count = 0;

while(resultSet.next())

{

count++;

}



if(count > 0)

{

exist = true;

}



}catch(Exception e){

System.out.println(e);

}finally{



try{

state.close();

conn.close();

}catch(Exception e){

}



}



return exist;

}

}



[/java]



It will be very helpful if any one can help me with this. Thanks in advance.

K Out!

To answer your question, is this the code you’re using to connect to the online database? Its definitely not located on localhost. Also ensure that the port you’re supplying is correct. MySQL usually works over 3306.



Now, for the tough love (don’t be offended if you already know this). Never do this. Distributing an application [to the public] that connects directly to a database, at all, is a huge security risk, let alone that you’re giving the root credentials out as well.



To do this with some protection, build a simple application residing on the server that acts as the in-between for the game application and the data that you’re storing in the database. This can be in Java, PHP, Ruby, whatever suits you. The way it would work is your game sends a command to the server, the server figures out what to do, and the server puts the information in the database. This way, your MySQL credentials are stored in the server software where they can’t be accessed by the game.



I hope you take this into some consideration :slight_smile:

@sbook



No no, i am not offended i welcome any good advices plus I am doing this to get the connection right with the online database. Just am determined to make it work.



Yes, this is the code for connecting to the online database.



I changed the port to 3306 and now have a new error message, below:

“java.sql.SQLException: Access denied for user ‘root’@‘localhost’ (using password: YES)”



I also tried with the port 3307 but gives the previous error.

MySQL might be denying access because it’s set to refuse remote root connection.

kamran said:
Yes, this is the code for connecting to the online database.

I changed the port to 3306 and now have a new error message, below:
"java.sql.SQLException: Access denied for user 'root'@'localhost' (using password: YES)"


The online database is not located on localhost (which is your laptop), its located somewhere online, like "freedatabases.com" (I really hope that's not a real website)

Of course, I can't be sure without some info on which online host this is, but you'll probably want something like:
[java]public String DATABASE_URL = "jdbc:mysql://freedatabases.com:3306/name-of-your-database";[/java]

madjack said:
MySQL might be denying access because it's set to refuse remote root connection.


This could also have something to do with it. I'm 99.99999% sure that an online hosting company is not giving you the username "root".

k, all this while i had my local server on, so i closed it and used the port 3306. This time is gives this error which is like the first error:



“com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure

The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.”

is it looking for the database in my local server for some reason?

What is the DATABASE_URL value you’re currently using? As long as that says “localhost”, it will continue to look for the database in your local server.

yes, It was “localhost” all this time but now i changed it to the online database one which is

“jdbc:mysql://game.something.com:3306/game_something_mmo”. But still the above error is giving.

What about your username and password, are you using something other than root, like a set of credentials they gave you?

yes I am using the root and username that they gave me.

“root” is a username… they didn’t supply you with a username and a password?

root is for example something like this “12345_root”

you probably need to allow remote access to your MySQL server for your IP or all IP’s

Can you post a link to the online service you’re using? Its hard to help you without knowing how their service is supposed to be configured!

fixed it. I called them and wezrule was right. I had to enable remote access. I had to enable “Direct Database Access”.

Thank you all for helping I really appreciate it.



K Out!

1 Like

Cool, glad you got this fixed :slight_smile:

[java]

import java.sql.;



public class Connect

{

public static void main (String[] args)

{

Connection conn = null;



try

{

String userName = “testuser”;

String password = “testpass”;

String url = “jdbc:mysql://localhost/test”;

Class.forName (“com.mysql.jdbc.Driver”).newInstance ();

conn = DriverManager.getConnection (url, userName, password);

System.out.println (“Database connection established”);

}

catch (Exception e)

{

System.err.println (“Cannot connect to database server”);

}

finally

{

if (conn != null)

{

try

{

conn.close ();

System.out.println (“Database connection terminated”);

}

catch (Exception e) { /
ignore close errors */ }

}

}

}

}

[/java]



I want to use this code in offline database… But this doesn’t connects. Is there a problem is this connection??

Can you give me a solution or some other connection to use? Thanks in advance!!!

is missing the port.

www.vogella.de/articles/MySQLJava/article.html