Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

My head hurts: JDBC + Java

 
Carsten Rasmussen
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Rene Larsen
Ranch Hand
Posts: 1179
Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ??
 
Carsten Rasmussen
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1179
Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 65
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It does not work and i dont get the login part no such command, sorry
 
Rene Larsen
Ranch Hand
Posts: 1179
Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1179
Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What errors do you get when compiling ?? ClassNotFoundException ??
 
Carsten Rasmussen
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1179
Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1179
Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 65
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Strange


And its allready binded in that file.
 
Rene Larsen
Ranch Hand
Posts: 1179
Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 65
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes the line is commented out allready.
Is it this you want ?
 
Rene Larsen
Ranch Hand
Posts: 1179
Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, we want the error you get when you run your code....
 
Carsten Rasmussen
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Rene Larsen
Ranch Hand
Posts: 1179
Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 65
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Changed my script to:


The output was as i guessed:
 
Rene Larsen
Ranch Hand
Posts: 1179
Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1337
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 65
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 65
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 65
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1179
Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 65
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1179
Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1179
Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The driver(s) to use when connecting to MySql are these: http://dev.mysql.com/downloads/connector/
 
Carsten Rasmussen
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1179
Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 65
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Isnt there a way to use my applet under servlet rules.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic