mysql (21 posts)

  • Profile picture of kamran kamran10p said 8 months, 3 weeks ago:

    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:

    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;
        }
    }
    

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

  • Profile picture of sbook sbook261p said 8 months, 3 weeks ago:

    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 :)

  • Profile picture of kamran kamran10p said 8 months, 3 weeks ago:

    @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.

  • Profile picture of madjack madjack495p said 8 months, 3 weeks ago:

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

  • Profile picture of sbook sbook261p said 8 months, 3 weeks ago:

    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:

    public String DATABASE_URL = "jdbc:mysql://freedatabases.com:3306/name-of-your-database";

    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”.

  • Profile picture of kamran kamran10p said 8 months, 3 weeks ago:

    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.”

  • Profile picture of kamran kamran10p said 8 months, 3 weeks ago:

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

  • Profile picture of sbook sbook261p said 8 months, 3 weeks ago:

    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.

  • Profile picture of kamran kamran10p said 8 months, 3 weeks ago:

    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.

  • Profile picture of sbook sbook261p said 8 months, 3 weeks ago:

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

  • Profile picture of kamran kamran10p said 8 months, 3 weeks ago:

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

  • Profile picture of sbook sbook261p said 8 months, 3 weeks ago:

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

  • Profile picture of kamran kamran10p said 8 months, 3 weeks ago:

    root is for example something like this “12345_root”

  • Profile picture of wezrule wezrule201p said 8 months, 3 weeks ago:

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

  • Profile picture of sbook sbook261p said 8 months, 3 weeks ago:

    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!