A question for Java+MySQL Developers

T

tobleron

Dear All,

I have a question for Developers who use Java and MySQL.
When you finished your code, and successfully tested on your
localhost, you'll started to distribute your Java application,
wright ? My question is : what is the technique that you use to make
your distributed Java application connect to the MySQL server ?

I have a problem when I start to distribute my Java application.
Previously I used "jdbc:mysql://localhost:3306/dbname" or
"jdbc:mysql://127.0.0.1:3306/dbname" in my local PC, and it runs well.
But when I change to "jdbc:mysql://ip_address:3306/dbname" it can not
work. The error message was
"com.mysql.jdbc.exceptions.jdbc4.CommunicationsException:
Communications link failure".

I tried to follow guidance about connecting mysql from remote hosts,
search for mysql bugs about connector/j, manipulate my.cnf file,
search for another forums, etc, but get no solution. Almost all
samples come with "jdbc:mysql://localhost:port/dbname".

Please help.

Note :
I used NetBeans 6.1
Connector/J 5.1.7
MySQL 5.0.51b-community-nt (runs on Win XP SP2)
Please dont give me links about mysql documentation, etc. I've tried
to follow them, thank you. Just share your techniques or practical
solutions.
 
D

Donkey Hottie

Dear All,

I have a question for Developers who use Java and MySQL.
When you finished your code, and successfully tested on your
localhost, you'll started to distribute your Java application,
wright ? My question is : what is the technique that you use to make
your distributed Java application connect to the MySQL server ?

I have a problem when I start to distribute my Java application.
Previously I used "jdbc:mysql://localhost:3306/dbname" or
"jdbc:mysql://127.0.0.1:3306/dbname" in my local PC, and it runs well.
But when I change to "jdbc:mysql://ip_address:3306/dbname" it can not
work. The error message was
"com.mysql.jdbc.exceptions.jdbc4.CommunicationsException:
Communications link failure".

I tried to follow guidance about connecting mysql from remote hosts,
search for mysql bugs about connector/j, manipulate my.cnf file,
search for another forums, etc, but get no solution. Almost all
samples come with "jdbc:mysql://localhost:port/dbname".

Please help.

Note :
I used NetBeans 6.1
Connector/J 5.1.7
MySQL 5.0.51b-community-nt (runs on Win XP SP2)
Please dont give me links about mysql documentation, etc. I've tried
to follow them, thank you. Just share your techniques or practical
solutions.

my.cnf or my.ini (depending on MySQL platform has following as default

#
# Instead of skip-networking the default is now to listen only on
# localhost which is more compatible and is not less secure.
bind-address = 127.0.0.1

I have it as

# bind-address = 127.0.0.1

and remote connection works.

You have to restart mysql server when you change that.
 
A

Arne Vajhøj

tobleron said:
I have a question for Developers who use Java and MySQL.
When you finished your code, and successfully tested on your
localhost, you'll started to distribute your Java application,
wright ? My question is : what is the technique that you use to make
your distributed Java application connect to the MySQL server ?

I have a problem when I start to distribute my Java application.
Previously I used "jdbc:mysql://localhost:3306/dbname" or
"jdbc:mysql://127.0.0.1:3306/dbname" in my local PC, and it runs well.
But when I change to "jdbc:mysql://ip_address:3306/dbname" it can not
work. The error message was
"com.mysql.jdbc.exceptions.jdbc4.CommunicationsException:
Communications link failure".

I tried to follow guidance about connecting mysql from remote hosts,
search for mysql bugs about connector/j, manipulate my.cnf file,
search for another forums, etc, but get no solution. Almost all
samples come with "jdbc:mysql://localhost:port/dbname".
Note :
I used NetBeans 6.1
Connector/J 5.1.7
MySQL 5.0.51b-community-nt (runs on Win XP SP2)
Please dont give me links about mysql documentation, etc. I've tried
to follow them, thank you. Just share your techniques or practical
solutions.

There are no "brilliant practical solution".

Just make sure that:
- MySQL listen to the IP address you connect to
- no firewall is blocking TCP port 3306 between Java box
and MySQL box
- you are granted access in MySQL from the Java box

Then it will work.

Arne
 
M

Mark Space

tobleron said:
Dear All,

I have a question for Developers who use Java and MySQL.
When you finished your code, and successfully tested on your
localhost, you'll started to distribute your Java application,
wright ? My question is : what is the technique that you use to make
your distributed Java application connect to the MySQL server ?

Web app: put the db parameters in config.xml so they can be configured
for the target machine. Frameworks may dictate a slightly different
config file for the db parameters.

Desktop app: make a window where the user types in their local driver,
host and port. Remember those values in a properties file for next run.

Desktop apps which "phone home" to my machines, I use a .properties
file, rather than hard code the values, so it's easy to change if I
reconfigure the database.
 
L

Lew

Mark said:
Web app: put the db parameters in config.xml so they can be configured
for the target machine. Frameworks may dictate a slightly different
config file for the db parameters.

For web apps, another option is web.xml (I'm not familiar with config.xml),
and you can also use properties or bundles.

To read properties/bundles, you use one of the 'getResource(String)' or
'getResourceAsStream(String)' methods, such as
<http://java.sun.com/javaee/5/docs/a...xt.html#getResourceAsStream(java.lang.String)>

The method from 'ServletContext' searches the application context root
directory, 'WEB-INF/' and 'WEB-INF/classes/' as the classpath. I find
'WEB-INF/' most natural for this, perhaps under a subdirectory such as
'WEB-INF/resources'.

Properties props = new Properties();
props.load( context.getResourceAsStream(
"resources/dbconnection.properties" ));

If the properties file is in XML format you'd use 'loadFromXML(InputStream)'.
 
M

Mark Space

Lew said:
For web apps, another option is web.xml (I'm not familiar with
config.xml), and you can also use properties or bundles.

Oops, yeah, can you tell it's been a while since I've actually worked on
a JEE app? Lew is correct.
 
A

Arne Vajhøj

Mark said:
Oops, yeah, can you tell it's been a while since I've actually worked on
a JEE app? Lew is correct.

It is still not a good pick for the purpose.

web.xml is part of the software.

This type of configuration should be done elsewhere.

In fact most app servers have a special place to define
connection pools where driver name, connection URL etc.
can be specified.

Arne
 
T

tobleron

@Donkey
I have follow your way. But still can not work.

@Arne
- Done
- Done (I shutdown the firewalls, anti virus, and I can connect
through windows command prompt)
- Done (I also grant all connection by using %)
But still can not work.

The error message still
"com.mysql.jdbc.exceptions.jdbc4.CommunicationsException:
Communications link failure".

Should I change the version of mysql and connector/j ? I already tried
connector/j 5.0.8, 5.1.5, and 5.1.7. I tried with mysql 5.0.51b-
community-nt runs on Win XP SP 2.

Any other possible solution ?
 
S

Silvio Bierman

tobleron said:
@Donkey
I have follow your way. But still can not work.

@Arne
- Done
- Done (I shutdown the firewalls, anti virus, and I can connect
through windows command prompt)
- Done (I also grant all connection by using %)
But still can not work.

The error message still
"com.mysql.jdbc.exceptions.jdbc4.CommunicationsException:
Communications link failure".

Should I change the version of mysql and connector/j ? I already tried
connector/j 5.0.8, 5.1.5, and 5.1.7. I tried with mysql 5.0.51b-
community-nt runs on Win XP SP 2.

Any other possible solution ?

In such situations always start to look at the network level.

Have you looked at what DonkieHottie wrote? At the MySQL host do a
netstat and look on what addresses MySQL is listening. If it is 127.0.0.
1 it is impossible to connect to it from the network. Make sure it
listens to its external address if the is only one or to 0.0.0.0 if you
want to make sure the server is accessible.

If that is OK and you are able to 'telnet <host> <port>' then you should
start looking at MySQL application level issues. Try to connect with a
true MySQL client app (not the PHP based admin) and see if that will
connect.

When even that works then you should start looking at the JDBC part.

Good luck.

Silvio Bierman
 
T

tobleron

In such situations always start to look at the network level.

Have you looked at what DonkieHottie wrote? At the MySQL host do a
netstat and look on what addresses MySQL is listening. If it is 127.0.0.
1 it is impossible to connect to it from the network. Make sure it
listens to its external address if the is only one or to 0.0.0.0 if you
want to make sure the server is accessible.

If that is OK and you are able to 'telnet <host> <port>' then you should
start looking at MySQL application level issues. Try to connect with a
true MySQL client app (not the PHP based admin) and see if that will
connect.

When even that works then you should start looking at the JDBC part.

Good luck.

Silvio Bierman

@Silvio
I already followed what DonkieHottie wrote.
I can use telnet or Windows command box to access and manipulate MySQL
in my local PC or another MySQL server in my LAN.
I also tried to use 3 version of Connector/J (which is native JDBC
driver of MySQL) version 5.0.8, 5.1.5, and 5.1.7. Still can not work.
I don't know why, and I don't know what else should be checked. Thank
you for your suggestion anyway.
 
S

Silvio Bierman

@Silvio
I already followed what DonkieHottie wrote.
I can use telnet or Windows command box to access and manipulate MySQL
in my local PC or another MySQL server in my LAN.
I also tried to use 3 version of Connector/J (which is native JDBC
driver of MySQL) version 5.0.8, 5.1.5, and 5.1.7. Still can not work.
I don't know why, and I don't know what else should be checked. Thank
you for your suggestion anyway.

So you can connect to the MySQL server this way from the machine that
tries to make the JDBC connection? Does that also work with the MySQL
command line client and the username/password you use from JDBC? If so
then there is almost nothing that the JDBC app does differently.

Please verify that you are testing this between exactly the two machines
where your JDBC connection fails

Silvio
 
T

tobleron

So you can connect to the MySQL server this way from the machine that
tries to make the JDBC connection? Does that also work with the MySQL
command line client and the username/password you use from JDBC? If so
then there is almost nothing that the JDBC app does differently.

Please verify that you are testing this between exactly the two machines
where your JDBC connection fails

Silvio

@Silvio
YES. I can connect to the MySQL server from the machine that tries to
make JDBC conenction. YES. It also work with the MySQL command line
client and the username/password I use from JDBC. I did it
successfully from 3 PCs in my LAN. But it doesn't work by using JDBC.
I just can use JDBC with "localhost" and "127.0.0.1" only, while in
MySQL command line I can use IP or hostname.

I don't know why.
 
S

Silvio Bierman

tobleron said:
@Silvio
YES. I can connect to the MySQL server from the machine that tries to
make JDBC conenction. YES. It also work with the MySQL command line
client and the username/password I use from JDBC. I did it
successfully from 3 PCs in my LAN. But it doesn't work by using JDBC.
I just can use JDBC with "localhost" and "127.0.0.1" only, while in
MySQL command line I can use IP or hostname.

I don't know why.

That is very strange. The MySQL JDBC driver does nothing more than plain
socket communications just as the MySQL command line tool does. The fact
that you get a communication link failure indicates a network level
problem and not a MySQL level issue. Using hard IP addresses, DNS names
etc. should work from Java just as it does at the native level.

You might try one of the typical network sniffer tools and see if you
can find out what the JDBC driver is trying to do.

Silvio
 
T

tobleron

Yeah. That's confusing me also. I'll look for another possibility
about this issue.

Thx.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top