Click to See Complete Forum and Search --> : mysql/j issue
mwmwnmw
07-06-2006, 02:19 AM
Just wading into Java and am having a JDBC issue. I'm pretty sure it's a classpath issue, but not sure how to resolve it.
The Basics...
I am running Mysql 5x with an apache / tomcat config. The db is up and running and accessible through phpmyadmin. I have downloaded and (I think) installed mysql/j. I guess this is where my problem lies.....
In the various online tutorials, including the one at mysql.com, they say to simple unzip mysql/j and put it in jdk\jre\lib\ext. Ok... when I did that it actually created a directory named mysql-connector-3.1. Inside that directory were three other directories, one of which is src. Within the src directory is the com directory that actually contains the connector. So, technically, the path for the connector is something like mysql-connector-3.1\src\com\mysql\Driver.
Anyway, I am trying to test the connection with the following...
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class Dbtry {
public static void main(String args[]) {
Connection con = null;
try {
Class.forName("com.mysql.jdbc.Driver").newInstance();
con = DriverManager.getConnection("jdbc:mysql://[dbname]",
"username", "password");
if(!con.isClosed())
System.out.println("Successfully connected to " +
"MySQL server using TCP/IP...");
} catch(Exception e) {
System.err.println("Exception: " + e.getMessage());
} finally {
try {
if(con != null)
con.close();
} catch(SQLException e) {}
}
}
}
Where the dbname, username, and password are all valid on this mysql server. This code compiles but when it runs I get the following error....
Exception: com.mysql.jdbc.Driver
It is my understanding that this is a classpath error. I have since moved the com directory from the src directory straight to the jre\lib\ext directory. So now the true classpath would be com.mysql.Driver. This had no effect. I still get the same exception. I'm a little fuzzy on .jar files so I'm wondering if there is something else that needs to be done. Everything within the com directory is unpacked, so I'm not sure that it matters.
Ok, that's the problem. Anyone have an idea?
Khalid Ali
07-06-2006, 09:57 AM
try the suggestions in the sticky at the top of this forum Basic java trouble shooting... (http://www.webdeveloper.com/forum/showthread.php?t=44232)... I think you may find your answer
mwmwnmw
07-06-2006, 11:00 PM
Did you even read my post?
For what it's worth, I did read the sticky at the top of this forum prior to posting the question here. I have read it twice since then. It is, essentially, the same as the 12 or fifteen other tutorials that I've read. Whatever it is that's wrong I'm clearly missing it. I thought that was understood by the fact that I posted the question in the first place. I'm not sure why some people insist on trying to make others look or feel stupid for asking questions. I thought that was the whole point of a forum such as this. If it's such a bother then why respond at all? I've answered plenty of questions on PHP on several forums and never done that to someone. I mean, really.... if you read someone's request for help and you know the answer, just tell them the answer. Sure, when people ask you to write code for them that's a little much, but that isn't usually the case. I know I'm on a rant here but I'm fairly frustrated and cryptic answers or being sent off to re-read something for the fourth time doesn't benefit anyone or anything.
Khalid Ali
07-07-2006, 12:35 AM
I understand the frustration( we all have been there one time or another), however, with this attitude of yours! good luck....
dbaber
07-08-2006, 11:49 AM
Classpath issues are the most annoying thing about Java. If you are having a classpath problem (I can't tell 100% from what you have posted), you need to get the JDBC .jar file on the classpath. You can do this either by adding the path to the .jar file to your classpath, or putting the .jar file somewhere that Java can find it. The latter is the approach that you are apparently trying to do. The other stuff in the download is not important for executing your program. You are only concerned with the .jar file. It should be named something like mysql-connector-java-3.1.12-bin.jar. If you copy this file to the location that you referred to in your posting, that should work.
I say "should", because I don't usually do it that way. My preference is to keep the jar file with the application and add its location to the class path. That gives you the flexiblity to use different versions of drivers with different applications, but either approach will work.
Waylander
07-09-2006, 07:23 PM
Yes dbaber probably has it right.
It is generally much easier to manipulate the environment your working with to include the jars you want, sometimes incompatibilitys between versions of jars will force using specific/old versions and you can run into trouble having all your jars in the classpath all the time.
I would download eclipse... put all your dependancies in a lib folder for your application, then make a user library and include them all.
Waylander.
mwmwnmw
07-11-2006, 01:31 AM
waylander & dbader,
Thank you for your responses. I actually figured it out with help on another forum about an hour after my last post here. The problem was more with my understanding of the driver jar itself, and that led to a classpath issue.
One thing I would like to throw out there that made all the difference in the world to me... and this should really be included in any tutorial.... is that when you catch the Exception and call
System.err.println("Exception: " + e.getMessage());
You should follow that with
e.printStackTrace();
The stack trace goes a long way to really pinpointing the problem. If nothing else comes of this thread hopefully someone else w/ the same problem adds the stack trace and is able to take it from there.
Thanks again for giving an actual response!
Khalid Ali
07-12-2006, 03:18 AM
waylander & dbader,
..........
Thanks again for giving an actual response!
This is unbelieveable and extremely ignorant I let it slide your unwarranted attack the first time. Have you had taken few mins to read thru the article I suggested above you would have known that towards the end of it there is a mention if how to set up mysql db. The error you have posted, it almost all the time means that your path (pointing to the driver file) is incorrect. But you seem to be one of those who like to be spoon fed every inch of the way.
We are volunteers here taking time to help I don't think I have time to take "such sarcasm" from some ungratefull people like you. I am hoping that you will be decent enough to end it here.
Thanks
Mr. Ram
07-12-2006, 04:40 AM
I think, u have missed to specify port details.
Try with this :-
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/DBName?user=root&password=root");
(Or)
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/DBName", "root", "root");
mwmwnmw
07-12-2006, 12:26 PM
Khalid,
I have no idea why you feel so wounded here. Once again I feel as if you haven't actually read what I've posted at all. Please set your ego aside and just read what I'm saying this time as I mean you no offense at all.
I thanked Waylander and dbader because I felt that they had both actually read my post and tried to offer helpful advice. As I said before, had you actually read my post, I had read through the sticky you mentioned before posting my question. I read it three more times afterward. I had also read through no less than six other similar tutorials. The last thing that I wanted was to be spoonfed anything. My goal is to actually learn java... to understand it... and not just to make some silly example from a book work. My problem, as I said in my third post, was not with the classpath per se, but with my understanding of how the .jar was accessed. If all I wanted was make one program work then I really wouldn't care about understanding .jars.
As I said before, I did receive actual help on another forum within an hour of my little rant here. Was I spoonfed an answer? No. But someone did take the time to actually read what I was asking and rather than suggest another tutorial they suggested that I print the stack trace for the exception. This changed the whole game for me. Once I printed the stack trace I was able to see and understand how / why the .jar was being missed. I spent another hour or so editing, recompiling, and researching stack trace errors before I was able to find my problem. Do you really consider this being spoonfed?
You seem to want to paint a picture of me based upon some perceived offense, but that picture doesn't fit the facts here at all. It's almost as if you are offended by the idea that your sticky post could somehow not be enough for everyone. I'm sure for most it is. What I was looking for was not something that gave me a b c steps where I gained little, if any, knowledge in the process. I was looking for someone to help me actually understand an error. In all honesty, one could pick any category on this site... java, php, perl etc... and answer literally every post with "go read a manual", but how helpful is that? As I said in my second post, which again I believe you did not actually read, I participate in many of these forums on the php side and I would never dream of doing that to someone.
http://www.webdeveloper.com/forum/showthread.php?t=113068
http://www.webdeveloper.com/forum/showthread.php?t=113136
http://www.webdeveloper.com/forum/showthread.php?t=113070
Those are just from the last day or so. I know what it is to volunteer my time and expertise. I also know the difference, as you can see from those posts, between taking the time to actually read someone's problem and make a real effort to give them a better understanding of what they are doing, as opposed to simply firing off a pithy remark or assuming that they are too lazy or intellectually weak to want more than a wet nurse. But I guess that is where we differ, my friend.
As for my ingratitude... if you really do feel slighted that I wasn't gushing over the fact that you spent all of four seconds to type out one sentence referring me to a tutorial I had already read, or another 8 seconds to tell me I have a bad attitude, well then I do apologize. I suspect, however, that the real point of offense is about nothing other than the fact that I did not find your sticky helpful. You should not take that as an insult. If I were indeed just looking for a b c instructions... to be spoonfed if you will, then I am sure it would have been great. As it turned out there was something in your tutorial, and all of the others, that simply did not register with me after reading many of them many times each. Maybe it will make you feel better to believe that I'm just too dim to understand? If that helps then it's ok with me.
Khalid Ali
07-12-2006, 12:47 PM
.........
Where the dbname, username, and password are all valid on this mysql server. This code compiles but when it runs I get the following error....
Exception: com.mysql.jdbc.Driver
It is my understanding that this is a classpath error. I have since moved the com directory from the src directory straight to the jre\lib\ext directory. So now the true classpath would be com.mysql.Driver. This had no effect. I still get the same exception. I'm a little fuzzy on .jar files so I'm wondering if there is something else that needs to be done. Everything within the com directory is unpacked, so I'm not sure that it matters.
Ok, that's the problem. Anyone have an idea?
I rarely write what I have in the post above(I have been helping people for several years online).
Now the qoute above is from your first post. It 100% points me to a classpath error as you have mentioned yourself. Hence my suggestion to you to checkout the classpath settings for mysql...
Anyways....I am glad you solved your issue, at the end of the day, that is the main goal, whoever helped you or wherever you got the help or pointers from, doesn't really matter that much..cheers