Link to home
Start Free TrialLog in
Avatar of lilyyan
lilyyan

asked on

jsp mysql, change db connection form localhost to remote IP address

Hello experts

I'm using mysql and jsp. when I connect to the db :

java.sql.DriverManager.getConnection("jdbc:mysql://localhost/myproject","userName", "userPassword");

the above statement will work.

but , java.sql.DriverManager.getConnection("jdbc:mysql://myIPaddress:3306/myproject","userName", "userPassword");

it won't work.

May you give me suggestion about this?

Thank you for your help.
Avatar of bloodredsun
bloodredsun
Flag of Australia image

Have you tried without the port number
Avatar of lilyyan
lilyyan

ASKER

yeah, i've tried without the port number.

Also try:

java.sql.DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/myproject","userName", "userPassword");
and check that you don't have any firewalls that could affect this connection
What driver are you using, the Connector/J one?
Avatar of lilyyan

ASKER

127.0.0.1:3306 is working
And what is your exception error...
Avatar of lilyyan

ASKER

The connetcor driver is : mysql-connector-java-3.1.7-bin.jar
Avatar of lilyyan

ASKER

The null point exception thrown out from sqlRst.close();
>> The null point exception thrown out from sqlRst.close();
You're getting nothing back from the connection.

>> 127.0.0.1:3306 is working
This means that the code is correct.  127.0.0.1:3306 resolves to localhost for mysql which shows that your code is correct and the error is caused by somehting else. Either the IP is incorrect or you have a network issue such as a proxy or firewall which has closed the access to that port.
PS you should always do a null check on the ResultSet object before you call close. Same goes for statement and connection.

e.g.
if ( sqlRst!=null ){
    sqlRst.close() ;
}
Avatar of lilyyan

ASKER

Thanks for your reply.

From antivirus and windows firewall which are installed in my pc, I did't find port 3306 is blocked.

Coud it be some issue related to the grant Privilege?  But the user name I currrently used is root.
>>Coud it be some issue related to the grant Privilege?  But the user name I currrently used is root.

I doubt it as if it were access, it would not work at all if it were and you can get it to work on "locahost" and "127.0.0.1". How are you getting your IP address, are you using "ipconfig"?

It looks like an annoying network issue. Are you on a home machine or a work one, i.e. are you using a LAN?
Avatar of lilyyan

ASKER

The IP address is correct. I'm using  it in tomcat. Also I checked with ipconfig in dos command  line. It's same IP.

 I'm using a pc within a LAN.
>>I'm using a pc within a LAN.

Then you might have issues with a proxy server and NAT. Can you ping that address?
Avatar of lilyyan

ASKER

Yeah, I just did a test.

ping myipaddress

I got something like : Packets: Sent = 4, Received = 4, Lost = 0 (0% loss)
So the address is a viable one but for some reason the datbase is not responding. Weird. Check your hosts file to see that the IP is not being mapped to somewhere else.

What are you trying to do with this db?
ASKER CERTIFIED SOLUTION
Avatar of kupra1
kupra1

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of lilyyan

ASKER

Thanks all for your replies,

Currently my db only have one user and the user name is root.

>>mysql> grant all privileges on myproject.* TO 'username'@'%' identified by 'userpassword';  //assuming test is your database name.
>>mysql> flush privileges;

I used above command, and added another user.  Now everything works fine. But somehow the program runs much slower than before.

Do you have any suggestion about this ?

Thanks for your help.
Avatar of lilyyan

ASKER

>>either you add one more record in  the table for the same user but with the host as "%"

The user root only has localhost access. If I changed it to %, will this be a good idea for program speed and safety?

Thanks for your reply.
Avatar of lilyyan

ASKER

I am not sure the reason. Now , the speed is just as fast as before.

And it seems that I don't need add port 3306 in the db connection statement.
Performance will not go down due to the fact that you gave more access. It will go down because now your db can be accessed from anywhere and hence, more traffic and slow.
Yea.. You dont need the port.

>> The user root only has localhost access. If I changed it to %, will this be a good idea for program speed and safety?

Well.. of course by giving access from anywhere certainly reduces your safety. If you want to give access only from let say "http://lilyyan.com", use the following:

grant all on myproject.* to 'username'@'lilyyan.com' identified by 'userpassword'.

But the safe way is to creata another user with the above rights to access your database and not use the superuser i.e. root.
Avatar of lilyyan

ASKER

Hi kupra1 ,
Can you help me check the question I posted under mysql topic. Thanks a lot .
I am going there. Hold on.