Can connect to MySQL via localhost, but not remotely!

Hi all!

I got a problem that I think you guys could help me with :)..

I have installed JDBC to make it possible to link my Java programs with MySQL databases. The installation worked just fine.

When I'm running my program with line:

Connection connection =null;

connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/chat","*****","*****");

...it works without any problems. I can handle information from the tables, add posts, delete posts and so on.

But when I'm trying to connect from an another computer, from school for an example, it doesn't connect. JBDC says it can't fint the mysql jbdc driver.

When I'm using this way I uses the line:

Connection connection =null;

connection = DriverManager.getConnection("jdbc:mysql://85.235.154.98:3306/chat","*****","*****");

Like I said, this doesn't work at all.

How do I fix this problem?

Worth to mention maybe, is that I'm usingWindows XP Home, and I have placed themysql-connector-java-5.0.5-bin.jar in "C:\Program\Java\jre1.6.0_01\lib\ext"

I'm usingJava version 1.6.0!

Please Help :)

[1409 byte] By [XaZea] at [2007-11-27 4:35:24]
# 1
> JBDC says it can't fint the mysql jbdc driver.This is not the actual error message. Please copypaste.
BalusCa at 2007-7-12 9:45:26 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 2
it could be that you school is blocking outgoing access to port 3306... make sure your school is not blocking this.... also, make sure your computer is not blocking this port as well.. (p.s. windows firewall may block this port by default)...
JWKC-5ivea at 2007-7-12 9:45:26 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 3

Thx for the reply!

Hmm, port 3006 is MySQL's default port number, so why would this be blocked? Second, my Windows Firewall is inactive, have never used it, or any other third party firewalls at any kind.

Maybe my school is blocking port 3306 for outgoing traffic, maybe should try anywhere else.

Well, we'll see if it works :).. Soon back with an answear! :)

XaZea at 2007-7-12 9:45:26 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 4

> But when I'm trying to connect from an another

> computer, from school for an example, it doesn't

> connect.

So the database is on your home computer? And you are attempting to connect from the school?

> JBDC says it can't fint the mysql jbdc

> driver.

Then it has nothing to do with connecting. That means your class path is wrong.

If it actually is a connection problem then you can do the following in a console window....

telnet 85.235.154.98 3306

There will be one of two results from the above after several minutes.

1. It will just sit there.

2. It will tell you it can't connect.

For 2 you have a problem that has nothing to do with java. Looking at your code will not fix it.

If it is 1 then something is wrong with your code and you should provide the exact exception.

jschella at 2007-7-12 9:45:26 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 5

Yes, my MySQL server is placed in one of my servers at home.

I understand what you just said.

In my last post I said I was going to test from an another computer, so I tried from one here at home.

I got the same result as i get from school. But I solved my problem.

I putted a copy of the file "mysql-connector-java-5.0.5-bin.jar" in my client computer at directory "C:\Program\Java\jre1.6.0_01\lib\ext" and added my CLASSPATH to the directory!

But I got a question. I now see that i need this file at the client computer allso, but "should" it be like this? Do I actually need this file here?

Never, that my schools network admin will put this file there for me! Fells sad ;D..

Thx for reply ;D

XaZea at 2007-7-12 9:45:26 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 6

> I putted a copy of the file

> "mysql-connector-java-5.0.5-bin.jar" in my client

> computer at directory

> "C:\Program\Java\jre1.6.0_01\lib\ext" and added my

> CLASSPATH to the directory!

>

> But I got a question. I now see that i need this file

> at the client computer allso, but "should" it be like

> this? Do I actually need this file here?

> Never, that my schools network admin will put this

> file there for me! Fells sad ;D..

The file has to be in the class path of the VM that is using it.

The location where you are putting it is considered a special location in terms of the class path.

It however it not the only way it can end up in the class path. And a number of people will tell you that that location is never correct for this type of jar.

jschella at 2007-7-12 9:45:26 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 7

Ok, I thought the .jar-file should only be in my servers lib/ext folder.

What's this file for actually? Does it transform my MySQL commands into Java commands or some like that?..

So how do I solve this problem without hitting on my network administrators at school ? ;)..

Can i use a method getDriver() or something to load the driver from my server before I connect to it?

Have a nice day :)

XaZea at 2007-7-12 9:45:26 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 8
It just make the JDBC connection with MySQL available. You still have to write the SQL statements yourself, the JDBC is not handling this for you. It just sends the statements and returns the results accordingly.
BalusCa at 2007-7-12 9:45:26 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 9

> Ok, I thought the .jar-file should only be in my

> servers lib/ext folder.

>

> What's this file for actually? Does it transform my

> MySQL commands into Java commands or some like

> that?..

The jar file provides a java API to the MySQL socket interface.

It doesn't really matter how it works.

>

> So how do I solve this problem without hitting on my

> network administrators at school ? ;)..

By putting it some where else can setting your class path to include that.

> Can i use a method getDriver() or something to load

> the driver from my server before I connect to it?

That has nothing to do with it. The only consideration is that it must be in your class path.

jschella at 2007-7-12 9:45:26 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 10
Ok, so I can for an example put the .jar-file in my project folder, and then add -cp [filepath] in the compile options?.. Cause we can't change the classpath on our accounts at school! =/..How should -cp command look like?
XaZea at 2007-7-12 9:45:26 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 11
Rather add it to the classpath of the JAR to be packaged: define it in the manifest.Here is a nice JAR tutorial: http://java.sun.com/docs/books/tutorial/deployment/jar
BalusCa at 2007-7-12 9:45:26 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 12

Hmm, I dont really get it.

Manifest file? What's that?

Heh, I'm not the most advanced person you've talked to.

I'm using textpad to create my programs. So I compile the program, and then run it directly from the class files. So I dont have any .exe, .jar or manifest (ext?) file!

XaZea at 2007-7-12 9:45:26 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 13
You're the developer here. Click at the link and read it.
BalusCa at 2007-7-12 9:45:26 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 14
Yes, but honestly, I dont understand a sh*t about jar and manifest files. Even less after reading that ;D..What's the manifest-file for? To tell my Chat.class that my mysql-odbc-jar-file is placed in a folder within this Chat.jar file for an example?
XaZea at 2007-7-12 9:45:26 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 15

> Ok, so I can for an example put the .jar-file in my

> project folder, and then add -cp [filepath] in the

> compile options?

Yes.

>

> How should -cp command look like?

That depends on the OS.

So since you are probably using windows it would look like...

java -cp "C:\mystuff\myjar.jar" MyPackage.MyClass

jschella at 2007-7-21 21:09:14 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 16

jschell - Ok, thanx :)..

Should "-cp [filepath]" really be in the "compile" parameters options, and not in "run application" parameters options?

>So since you are probably using windows it would look like...

>java -cp "C:\mystuff\myjar.jar" MyPackage.MyClass

Isn't it when I'm compiling thru cmd, I use "java" before "-cp ...." or should this be in the textpad compile/run parameters options?

Today, I have "$Basename" in the parameters options. Whats this?, should I change this to "$Basename; C:\program\java\jre.1.6.0_01\lib\ext\[The-Java-Obdc-File].jar" ?

XaZea at 2007-7-21 21:09:14 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 17

> jschell - Ok, thanx :)..

>

> Should "-cp [filepath]" really be in the "compile"

> parameters options, and not in "run application"

> parameters options?

If I can guess the context the answer is both.

The sun compiler is written entirely in java.

>

> Isn't it when I'm compiling thru cmd, I use "java"

> before "-cp ...." or should this be in the textpad

> compile/run parameters options?

>

You don't compile using the "java" command. You can however pass in a class path to the compiler as well.

> Today, I have "$Basename" in the parameters options.

> Whats this?, should I change this to "$Basename;

> C:\program\java\jre.1.6.0_01\lib\ext\[The-Java-Obdc-Fi

> le].jar" ?

No idea what you are talking about. That is specific to your environment.

jschella at 2007-7-21 21:09:14 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 18
In response with that you can't acces from other computer than the localhost i think is a mysql bad configuration.You'll have to modify the grant tables of mysql to allow connection to the database other computers with the user in question you use.
xcorpyona at 2007-7-21 21:09:14 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 19

Ok, I tried to put -cp [filepath] to my compile and run parameters, but it didnt work, i only get some error message about "java.lang.ClassNotFoundException: com.mysql.jbdc.Driver"

Xcorpyon: My MySQL configuration isn't wrong. I uses it every day to my PHP-projects, and the accessibility from other computers isn't any problem with..

XaZea at 2007-7-21 21:09:14 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 20

> Ok, I tried to put -cp [filepath] to my compile and

> run parameters, but it didnt work, i only get some

> error message about

> "java.lang.ClassNotFoundException:

> com.mysql.jbdc.Driver"

And you are ONLY compiling?

Then you are doing something wrong with the classpath.

And you are using a non-standard usage of jdbc drivers since the only way you can get that error while compiling is if you are explicitly referencing that driver in your code.

jschella at 2007-7-21 21:09:14 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 21
Oh sry, I forgotted to say that.The compile works just fine!It's when I'm running the program and trying to use the connection between my program and mysql i get this error =/..
XaZea at 2007-7-21 21:09:14 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 22
Then the class path is wrong.
jschella at 2007-7-21 21:09:14 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 23
Hmm, I can't see that's the problem, cause I use the same path as I use in my enviroment variable CLASSPATH.. Then it can't be wrong eh?Btw, thx for taking time for me :)
XaZea at 2007-7-21 21:09:14 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 24

When you get a class not found error the basic answer is that the class path is always wrong. There is no other possibility.

Why it is wrong can be caused by any number of possibilities like (but not a full list)

- provided path is wrong

- provided path permissions are wrong

- path includes multiple paths where one does not exist or permissions are wrong

- The loaded class name is wrong

jschella at 2007-7-21 21:09:14 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 25

ok, I see.. If it's incorrect, why does the path work in my enviroment variables?

Any Idea how my path should look like if i want my driver.jar if my directory list looks like this:

ROOT DIR: chat.java, chat.class

Dir: images

- File: images/lol.gif

- File: images/hello.gif

Dir: txt

- File: txt/accounts.txt

Dir: files

- File: files/driver.jar

I want to use command "-cp [FILEPATH TO files/driver.jar]"

Should it look like "./files/driver.jar"?

XaZea at 2007-7-21 21:09:14 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 26

> ok, I see.. If it's incorrect, why does the path work

> in my enviroment variables?

Because it is correct there.

>

> I want to use command "-cp [FILEPATH TO

> files/driver.jar]"

> Should it look like "./files/driver.jar"?

Presuming standard unix stuff it should look something like the following pseudo code.

cd {ROOT_DIR}

java -cp .:./files/driver.jar MyPackage.MyClass

Note in the above that the first line ensures that you are in correct directory. Naturally the above also presumes that

1. The permissions for everything is correct

2. You are not adding other paths to the above (and thus invalidating the example)

You don't actually need your class at all. You can verify the path using the following pseudo code as well....

cd {ROOT_DIR}

java -cp .:./files/driver.jar {class name loaded via forClass}

jschella at 2007-7-21 21:09:14 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 27

I understand.. But I solved the problem earlier today ;D..

I just used the parameter "-cp .;driver.jar;" like I said, but without the directory files.. I just putted the **** in the same folder as rest of it!

Now it works, thanx for the great help :)

My first post, and great help. Great forum :)

XaZea at 2007-7-21 21:09:14 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 28
You might want to experiment with it so you get a better feel for what works and what doesn't work.For example try puting a non-existent directory before a existent one.
jschella at 2007-7-21 21:09:14 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...