File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes JDBC and the fly likes My head hurts: JDBC + Java Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » JDBC
Bookmark "My head hurts: JDBC + Java" Watch "My head hurts: JDBC + Java" New topic
Author

My head hurts: JDBC + Java

Carsten Rasmussen
Ranch Hand

Joined: Sep 22, 2009
Posts: 65
I'm new at jave and I cant compile and run java apps with JDBC on Ubuntu.
I have installed libmysql-java
And the files is located at /usr/share/java/
I have tried a million times to setup export CLASSPATH and when i do i cant run java before reboot.
Please help.
Your regards Carsten Rasmussen

I have tried:
javac -classpath "/usr/share/java/*" Connect.java
and
javac -classpath /usr/share/java/mysql-connector-java-5.1.10.jar Connect.java
None working.

Here is the code.
Stephan Mueller
Ranch Hand

Joined: May 05, 2010
Posts: 50
Please help.


I'd like to throw this request back to you.
Please help us to understand your problem.
What is it actually that you are trying to do? What have you coded/what are you invoking? What is the error message?
What have you done so far to narrow down the problem? What were your findings?
...
You see where this is going, so please, help.


1. Make it run - 2. Make it run correctly - 3. Make it pretty OR fast/small - 4. ??? - 5. Profit
Rene Larsen
Ranch Hand

Joined: Oct 12, 2001
Posts: 1179

Do you have the jar mysql-connector-java-5.1.10.jar in the same folder as your java source ??

How do you set-up your CLASSPATH (export) - and where (which server file) ??

What are the errors you get when trying to compile Connect.java ??


Regards, Rene Larsen
Dropbox Invite
Carsten Rasmussen
Ranch Hand

Joined: Sep 22, 2009
Posts: 65
I dont have connector same where as source.
I tried this:
export set CLASSPATH="/usr/share/java/mysql-connector-java-5.1.10.jar;$CLASSPATH"
Error from source:
Cannot connect to database server
Rene Larsen
Ranch Hand

Joined: Oct 12, 2001
Posts: 1179

Carsten Rasmussen wrote:I dont have connector same where as source.
I tried this:
export set CLASSPATH="/usr/share/java/mysql-connector-java-5.1.10.jar;$CLASSPATH"

Your export is a mix between windows and unix/linux

An export has the syntax like below (notice the use of : between the elements, and not the use of 'set' between export and CLASSPATH - and I've added a dot in the beginning):


If you have set the export in some system file (e.g. .login) - then you can reload the file by typing the below command in your terminal:
Carsten Rasmussen
Ranch Hand

Joined: Sep 22, 2009
Posts: 65
It does not work and i dont get the login part no such command, sorry
Rene Larsen
Ranch Hand

Joined: Oct 12, 2001
Posts: 1179

Where do you write the export - direct in the terminal, or in some file ??

I gave only the .login as a sample.
Rene Larsen
Ranch Hand

Joined: Oct 12, 2001
Posts: 1179

What errors do you get when compiling ?? ClassNotFoundException ??
Carsten Rasmussen
Ranch Hand

Joined: Sep 22, 2009
Posts: 65
I get no error(s) it compiles but JDBC mysql can't connect.
Does this mean its the code or my MySQL setup its wrong with ???
Rene Larsen
Ranch Hand

Joined: Oct 12, 2001
Posts: 1179

It is your MySQL setup...

Can you telnet port 3306

e.g.

This should give you an output like this (after a few seconds the command will end, and terminate automatically)

And here is something you can read referring to "mysql linux can not connect":
http://dev.mysql.com/doc/refman/5.1/en/can-not-connect-to-server.html
http://dev.mysql.com/doc/refman/5.0/en/connector-j-usagenotes-troubleshooting.html
Rene Larsen
Ranch Hand

Joined: Oct 12, 2001
Posts: 1179

Maybe you need to enable remote connect
Mysql 5.0: Networking is disabled by default on the default Ubuntu installation. To enable remote database access, comment out (or remove) the following line with a "#" in the file: /etc/mysql/my.cnf


http://www.yolinux.com/TUTORIALS/LinuxTutorialMySQL.html
Carsten Rasmussen
Ranch Hand

Joined: Sep 22, 2009
Posts: 65
Strange


And its allready binded in that file.
Rene Larsen
Ranch Hand

Joined: Oct 12, 2001
Posts: 1179

Carsten Rasmussen wrote:
And its allready binded in that file.

Is the bind-address line comment out ?? - it should be that.
Stephan Mueller
Ranch Hand

Joined: May 05, 2010
Posts: 50
First, the telnet test looks like your db is up and running on the expected port, this could also have been achieved by using the
mysql command-line utility.

Next:
I get no error(s) it compiles but JDBC mysql can't connect.

You should print the stacktrace in your catch clause, don't ignore the errors or you'll be as blind as you are now.

That's an oxymoron. How do you know you can't connect? Probably there's an error message telling you, isn't?
If so, please tell us.


Carsten Rasmussen
Ranch Hand

Joined: Sep 22, 2009
Posts: 65
Yes the line is commented out allready.
Is it this you want ?
Rene Larsen
Ranch Hand

Joined: Oct 12, 2001
Posts: 1179

No, we want the error you get when you run your code....
Carsten Rasmussen
Ranch Hand

Joined: Sep 22, 2009
Posts: 65
Rene Larsen
Ranch Hand

Joined: Oct 12, 2001
Posts: 1179

Carsten Rasmussen wrote:

You need to show us the stacktrace of the exception thrown. You only print your own String. Try to include the variable e from the exception in your println.
Carsten Rasmussen
Ranch Hand

Joined: Sep 22, 2009
Posts: 65
Changed my script to:


The output was as i guessed:
Rene Larsen
Ranch Hand

Joined: Oct 12, 2001
Posts: 1179

How did (or do) you set the CLASSPATH ??

What entries does your CLASSPATH contain of ??

Try to type the code below in a terminal, and post the output:
Lester Burnham
Rancher

Joined: Oct 14, 2008
Posts: 1337
I recommend to stay clear of the CLASSPATH environment variable; it's generally more trouble than it's worth. Try specifying the required libraries on the command line:

java -cp .:/usr/share/java/mysql-connector-java-5.1.13-bin.jar Connect

Note the ".:" in front - it ensures that the current directory (which contains the Connect class) is also part of the classpath.
Carsten Rasmussen
Ranch Hand

Joined: Sep 22, 2009
Posts: 65
I did this >>
[code]
export CLASSPATH=/usr/share/java/mysql-connector-java.jar:$CLASSPATH
[code]

And now when i run Connect.class
[code]
cagge@cagge-desktop:~/cpp/java$ java Connect
Database connection established
Database connection terminated
[code]

Wierd it didnt work before
Carsten Rasmussen
Ranch Hand

Joined: Sep 22, 2009
Posts: 65
I would like to thank all of the people who has tried to help me,
thank you all very much.

I Love Coderanch
Carsten Rasmussen
Ranch Hand

Joined: Sep 22, 2009
Posts: 65
One more question, if i want users to see my applets sql witch connector should i use:
And don't you say jmiJDBC unless you have a full understandable example on you
thank in advanced.
Rene Larsen
Ranch Hand

Joined: Oct 12, 2001
Posts: 1179

I don't think I understand why you want another JDBC connector - if you just want to show the SQL used - then just print (or give access to) the PreparedStatement like this: http://www.avajava.com/tutorials/lessons/how-do-i-display-a-prepared-statement-with-bind-variables-using-mysql.html

Or what exactly do you mean ??
Carsten Rasmussen
Ranch Hand

Joined: Sep 22, 2009
Posts: 65
In php you access you database like this:

And the users outside network can access my database with my script.
That's what i meant.
Rene Larsen
Ranch Hand

Joined: Oct 12, 2001
Posts: 1179

To access your MySql DB from outside your network, you need to open up in your firewall on port 3306 - and the outside will then connect like you do with a MySql JDBC driver (just with your WAN IP-adr. or domain name - instead of localhost)
Rene Larsen
Ranch Hand

Joined: Oct 12, 2001
Posts: 1179

The driver(s) to use when connecting to MySql are these: http://dev.mysql.com/downloads/connector/
Carsten Rasmussen
Ranch Hand

Joined: Sep 22, 2009
Posts: 65
That exactly what i wont be doing.
I would like client and server to run on localhost.
Is it possible ?

I have tomcat 6 installed if i can use something of that because servlets work that way
Rene Larsen
Ranch Hand

Joined: Oct 12, 2001
Posts: 1179

What do you mean by "because servlets work that way" ??

If you call your servlets or database from the same machine where they are served from - you can use 'localhost' (127.0.0.1), but if they are called from another machine - on the same network, or outside your network - you need to call the hosting machine (server) with its LAN IP-adr. or host name (on same network) / WAN IP-adr. or domain name (outside your network).

BUT - you can however do a dirty hack (not recommended) on the calling clients by setting localhost to the server's WAN or LAN IP-adr. in the clients host file - then each time localhost is used, it will call the server instead.
Carsten Rasmussen
Ranch Hand

Joined: Sep 22, 2009
Posts: 65
Isnt there a way to use my applet under servlet rules.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: My head hurts: JDBC + Java