0
 public Connection conn() throws ClassNotFoundException, InstantiationException,                   IllegalAccessException, SQLException
{
    Connection all_connection = null;
     Class.forName("com.mysql.jdbc.Driver").newInstance();

      all_connection = DriverManager
        .getConnection("jdbc:mysql://localhost:3306/dbname","username","password");   
    return all_connection;

}

Above code is my connection code. Its working fine. If i replace localhost by my ip address, its not working. Actually i want to replace localhost by ip address. Replacing localhost by 127.0.0.1 works fine. Why replacing ip address is not working????

3
  • 1
    are you confused, firstly you said not working with ip then after you told working fine with ip, 127.0.0.1 , what is your issue . its seems very confusing. Commented May 2, 2014 at 5:01
  • Do you have access control set up properly as described in Access Control, Stage 1: Connection Verification? Commented May 2, 2014 at 5:03
  • which database you are using ? Commented May 2, 2014 at 5:17

4 Answers 4

1

This is probably an issue with your database user. (I can't tell for sure because you didn't post the actual error)

You'll need to verify that the user you're logging in as is allowed to access is allowed to access the database from an external IP. It seems likely that you're using the root user, which is allowed access from localhost, 127.0.0.1, and ::1, but not from external IPs.

If you are, make sure you set up a new DB user with only the required access, and allow access from the specific IP, or % (any host)

Sign up to request clarification or add additional context in comments.

3 Comments

thanx for the reply. and yeah i am using root user.
Per my answer: Create a new database user and make sure you allow access from your specific IP, or a range of IPs, or from all (%)
I have created a new user. I cant find any interface for loging in. I am using xampp. Usually when i open it, it directly takes me to root user. where can i find dbuser login interface?
0

I have faced same issue while I am new to this.

IP address means other DB server's IP ?

If yes then check firewall that is it allow Incoming connection for 3306 port ?

If it is not then ad new rule in that server machine with 3306 connection post allow.

and which OS in server machine ?

I have done like following in postgresql :

jdbc:postgresql://192.168.1.9:5432/dbName

In your case it would be :

jdbc:mysql://192.168.1.9:3306/dbName

I suggest you to first of all check that machine's firewall port rules then go for next. And be sure that you have written true user name and password.

Comments

0

First check whether your machine is configured for the IP address you are trying.

Try to ping it from other machine.

Try to nslookup for the same IP address.

If both the results are positive, means that your machine is configured for the IP address,

If any one fails, try to configure your ip, and then try again.

Connection object = DriverManager.getConnection("jdbc:mysql://127.xx.xx.x:3306/dbname","username","password");

use above code after configuration.

Comments

0

By default in some database server remote access is disabled. You have to enable remote access. If you are using mySQL here is the solution.

1.Go to my sql bin folder or add it to PATH
2.Login to root by mysql -uroot -proot (or whatever the root password is.)
3.On success you will get mysql>
4.Provide grant access all for that user.

query to enable access.

GRANT ALL PRIVILEGES ON *.* TO 'username'@'IP' IDENTIFIED BY 'password';

Here IP is IP address for which you want to allow remote access, if we put % any IP address can access remotely.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.