?
Solved

MySQL with Java... (installation) + PATH

Posted on 2005-04-24
29
Medium Priority
?
199 Views
Last Modified: 2013-11-23
Alright I've seen many examples on how to install Connectjor/J but it still doesn't seem to want to work.  What is the best source for just wanting to connect and manipulate some of the data I have in my MySQL database.  Just basically the easiest to install and run, step by step.  And if my java install is in C:\Program Files\java\jdk1.5.0_02, how and what would my CLASSPATH be?

Thanks in advance!  I'll try to help but I'm new to JAVA. :)
0
Comment
Question by:lamerhooDJV
  • 12
  • 9
  • 8
29 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 13855405
Just put the jar containing the driver into *all* your jre\lib\ext directories
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 13855406
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 13855411
Make sure you use the right class name for the driver. It could be

 Class.forName("com.mysql.jdbc.Driver").newInstance();

but check this
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 1

Author Comment

by:lamerhooDJV
ID: 13855528
C:\Program Files\Java\jdk1.5.0_02\bin>javac MySQLattempt.java
MySQLattempt.java:1: 'class' or 'interface' expected
Connection connection = null;
^
MySQLattempt.java:2: 'class' or 'interface' expected
    try {
    ^
2 errors
0
 
LVL 1

Author Comment

by:lamerhooDJV
ID: 13855533
How do I figure out my drivername?
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 13855545
>> How do I figure out my drivername?

It will tell you in the drover docs
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 13855549
drover=driver

;-)
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 13855554
0
 
LVL 1

Author Comment

by:lamerhooDJV
ID: 13855995
C:\Program Files\Java\jdk1.5.0_02\bin>javac JdbcExample1.java

C:\Program Files\Java\jdk1.5.0_02\bin>java JdbcExample1
Exception in thread "main" java.lang.NoClassDefFoundError: JdbcExample1 (wrong
ame: com/stardeveloper/example/JdbcExample1)
        at java.lang.ClassLoader.defineClass1(Native Method)
        at java.lang.ClassLoader.defineClass(ClassLoader.java:620)
        at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:1
4)
        at java.net.URLClassLoader.defineClass(URLClassLoader.java:260)
        at java.net.URLClassLoader.access$100(URLClassLoader.java:56)
        at java.net.URLClassLoader$1.run(URLClassLoader.java:195)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.net.URLClassLoader.findClass(URLClassLoader.java:188)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
        at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:268)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:251)
        at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:319)

C:\Program Files\Java\jdk1.5.0_02\bin>

I get a different error now... think you can help me figure this one out ? :)
0
 
LVL 15

Expert Comment

by:aozarov
ID: 13856375
JdbcExample1 has a package declaration (com.stardeveloper.example)
compile it like this:
C:\Program Files\Java\jdk1.5.0_02\bin>javac -d . JdbcExample1.java
and run it like this:
C:\Program Files\Java\jdk1.5.0_02\bin>java -classpath %classpath%;<driver_path>;. com.stardeveloper.example.JdbcExample1
where replace <driver_path> to the path of your connectorj driver jar file.
0
 
LVL 1

Author Comment

by:lamerhooDJV
ID: 13856446
Not sure if i'm doing something incorrect, but this is what I put and get :(

C:\Program Files\Java\jdk1.5.0_02\bin>java -classpath %classpath%;.C:\Program Fi
les\java\jdk1.5.0_02\jre\lib\ext\mysql-connector-java-3.0.16-ga-bin.jar;. com.st
ardeveloper.example.JbdcExample1
Exception in thread "main" java.lang.NoClassDefFoundError: Files\java\jdk1/5/0_0
2\jre\lib\ext\mysql-connector-java-3/0/16-ga-bin/jar;/C:\Program

C:\Program Files\Java\jdk1.5.0_02\bin>
0
 
LVL 1

Author Comment

by:lamerhooDJV
ID: 13856450
C:\Program Files\Java\jdk1.5.0_02\bin>echo %classpath%
.;C:\Program Files\java\jdk1.5.0_02\jre\lib\ext\mysql-connector-java-3.0.16-ga-b
in.jar

just so you know, that's my %classpath% is set to :)
0
 
LVL 15

Expert Comment

by:aozarov
ID: 13856497
if your classpath is set correctly to mysql driver then just do:

java -classpath %classpath%;. com.stardeveloper.example.JbdcExample1

BTW, the problem is with space (this should fix it, though the above should work as well)
java -classpath %classpath%;"C:\Program Files\java\jdk1.5.0_02\jre\lib\ext\mysql-connector-java-3.0.16-ga-bin.jar";. com.stardeveloper.example.JbdcExample1
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 13856873
It will be easier for you simply to delete the package declaration in this case
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 13856875
And if you've placed the driver jar as i mentioned earlier, you don't need to include it in a classpath
0
 
LVL 15

Expert Comment

by:aozarov
ID: 13858544
>> It will be easier for you simply to delete the package declaration in this case
Having classes without package declaration is always a bad habit.
If you compile and run it as I showed before, it should work fine even when using the prefered way of having your class as part of a package.
javac -d . JbdcExample1.java
java -classpath %classpath%;. com.stardeveloper.example.JbdcExample1

>> And if you've placed the driver jar as i mentioned earlier, you don't need to include it in a classpath
I mentioned that: java -classpath %classpath%;. com.stardeveloper.example.JbdcExample1
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 13858579
>>Having classes without package declaration is always a bad habit.

It's better to get it working first and the best way to do that is to diminish complexity as far as possible. Packages can always be put in later. You can then run it thus (having followed my earlier suggestions about where to put the jar):

java JbdcExample1
0
 
LVL 15

Expert Comment

by:aozarov
ID: 13858738
>> It's better to get it working first and the best way to do that is to diminish complexity as far as possible.
But I already gave him a solution that will work fine for him with having its package declaration as is.
0
 
LVL 1

Author Comment

by:lamerhooDJV
ID: 13858864
I've tried each of yours guy's examples and no luck whatsoever.  I'm not saying your examples are wrong, or that they don't work, but in my particular case (maybe Java is misconfigured?  etc...) I am unable to run said commands :(  

Is there anything you could do to basically test to make my installation is proper or not.  What version are you testing, or developing on?

Thanks!
0
 
LVL 15

Expert Comment

by:aozarov
ID: 13858897
did you try this:
javac -d . JbdcExample1.java
java -classpath %classpath%;. com.stardeveloper.example.JbdcExample1

If so what was your exception?
0
 
LVL 1

Author Comment

by:lamerhooDJV
ID: 13859011
C:\Program Files\Java\jdk1.5.0_02\bin>javac -d . JdbcExample1.java

C:\Program Files\Java\jdk1.5.0_02\bin>java -classpath %classpath%;. com.stardeve
loper.example.JdbcExample1
Exception in thread "main" java.lang.NoClassDefFoundError: Files\java\jdk1/5/0_0
2\jre\lib\ext\mysql-connector-java-3/0/16-ga-bin/jar;/

C:\Program Files\Java\jdk1.5.0_02\bin>
0
 
LVL 86

Accepted Solution

by:
CEHJ earned 2000 total points
ID: 13859037
Step-by-step:

1. Delete the package declaration in the source (it can be put back in another time)
2. Put the driver jar in *all* jre\lib\ext directories
3. Compile:

javac JdbcExample1.java
4. Run
JdbcExample1
0
 
LVL 1

Author Comment

by:lamerhooDJV
ID: 13859108
Very nice CEHJ, worked beautifully.  I must have missed the part about deleting the package declaration and I apologize for that.  How do you think I should do the points, split or something?  Or ... (input suggested, please) :)
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 13859162
No problem. Points are really up to you to decide
0
 
LVL 15

Expert Comment

by:aozarov
ID: 13859164
CEHJ, I am amazed (two very bad advices).
I didn't expect that "jre\lib\ext directories" from you... (but from someone else).

The real problem is:
C:\Program Files\java\jdk1.5.0_02\jre\lib\ext\mysql-connector-java-3.0.16-ga-bin.jar
change to ->
"C:\Program Files\java\jdk1.5.0_02\jre\lib\ext\mysql-connector-java-3.0.16-ga-bin.jar""

As I said before.

>> How do you think I should do the points, split or something?
I don't need any of the points, thanks.
0
 
LVL 1

Author Comment

by:lamerhooDJV
ID: 13859240
aozarov,  I apologize if I may have offended you.  

If you don't need the points, I'll give them to CEHJ... but I would personally like to thank both of you :)
0
 
LVL 15

Expert Comment

by:aozarov
ID: 13859290
lamerhooDJV, you didn't offend me at all :-)
I was offended by the response you got, which is in my mind, just to make your stuff working without considering any implications.
Having class without package declaration is a bad thing to do.
Putting stuff in your JDK/JRE ext folder is a bad thing to do (unless you really intended/ needed to) because it has a global effect on all of your java applications and many other side effects. See: http://www.neward.net/ted/Papers/ClassForName/index.html
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 13859369
>>Having class without package declaration is a bad thing to do.

It's better to have packages, that's true, but as i mentioned above that can be put in later

>>Putting stuff in your JDK/JRE ext folder is a bad thing to do ...

Well you'd better have a word with Sun and tell them where they're going wrong. That paper's about 4 years old and they haven't pulled out of the extensions mechanism have they?
0
 
LVL 15

Expert Comment

by:aozarov
ID: 13859496
>> That paper's about 4 years old and they haven't pulled out of the extensions mechanism have they?
The extensions mechanism has its purpose though it is probably not putting your jdbc driver there just because you didn't set the classpath correctly and was missing quotes.
Later on when one is going to upgrade its driver and forgot that he had it already in its extension folder will have many unpleasant surprises.
BTW, the content and the problems that are mentioned in that paper are still valid.
0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Suggested Courses
Course of the Month13 days, 14 hours left to enroll

807 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question