i am trying to connect to mysql database using java. I have done connecting to microsoft access before. and i modified my code to connect to mysql server locally.

i think the problem is I don't really understand where my mysql database is. anybody may point out the problem would be great. thanks very much.

DataHolder(Boolean getAll)
	{
		String url = "jdbc:mysql://http://localhost/phpmyadmin/index.php?db=employees"; 
		String username = "root";
		String password = "root";
		
		try
		{
			Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
			connection = DriverManager.getConnection(url, username, password );
		}

You need to add port information in the URL. MySQL's default port is 3306 so unless you have made any changes to it that's whats missing. Try adding the port number after the localhost.
Also sometimes localhost doesn't work so try 127.0.0.1. But this is certainly the problem that I have faced so in your case it might not be.

he's also using the wrong jdbc driver

And, if he really wanted help, he would have provided the complete exception with stack trace, of course.

Yes, I missed that one. For MySQL you should be using this driver.

hey thanks verruckt24 for all the info I needed. that's really helpful. Jwenting pointed out I used the wrong jdbc driver which also gave me a good hint too. I just downloaded "connector/J" and it looks like documents there will help a lot. i'll take some time to try to figure this out first. bad thing is I was using jdbc that i think i will have to change a lot of my code to transfer everything to mysql db.

Simple man download the connector/j driver and add it to the environment variables.(named classpath)
then use

Class.forName("com.mysql.jdbc.Driver");
String url = "jdbc:mysql://localhost:3306/databasename"

Now you able to connect,if using phpmyadmin then add the classpath to that mysql available in wamp or xamp server you are using

commented: Just repeating what was laready said -2

Simple man download the connector/j driver and add it to the environment variables.(named classpath)
then use

Class.forName("com.mysql.jdbc.Driver");
String url = "jdbc:mysql://localhost:3306/databasename"

Now you able to connect,if using phpmyadmin then add the classpath to that mysql available in wamp or xamp server you are using

took your suggestion and get

Failed to load JDBC/ODBC driver.
java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
	at java.net.URLClassLoader$1.run(Unknown Source)
	at java.security.AccessController.doPrivileged(Native Method)
	at java.net.URLClassLoader.findClass(Unknown Source)
	at java.lang.ClassLoader.loadClass(Unknown Source)
	at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
	at java.lang.ClassLoader.loadClass(Unknown Source)
	at java.lang.Class.forName0(Native Method)
	at java.lang.Class.forName(Unknown Source)
	at DataHolder.<init>(DataHolder.java:26)
	at AddRecord.<init>(AddRecord.java:13)
	at MenuBar.<init>(MenuBar.java:46)
	at TheFrame.<init>(TheFrame.java:21)
	at TheFrame.main(TheFrame.java:78)

does it mean my driver is not in the right place or envir variable not set right? or if it is my code .. what made it fail to load? thanks

Make sure the jar file is in path. You can also specify the location of the jar file through the "cp" flag.

Make sure the jar file is in path. You can also specify the location of the jar file through the "cp" flag.

Ok, let me make sure if my first step was right. I am using eclipse. i opened up "Windows" --> "preference" --> "java" --> "buildPath" --> "classPath Variables"

and then i added:

edit variable entry:
Name: JDBC
Path: C:/mysql-connector-java-5.0.8

is this how it should be done?

thanks everybody i finally got it figured out. it was something in eclipse that i had to add the jar to.

thanks very much.

Hey you haven't mentioned you are using eclipse that's why i gave only connection codings ok In eclipse means right click the project->buildpath->add jar file->locate your mysqlconnector.jar
Now it should work.

Hey you haven't mentioned you are using eclipse that's why i gave only connection codings ok In eclipse means right click the project->buildpath->add jar file->locate your mysqlconnector.jar
Now it should work.

mohamedsafiq,
that's right. sorry i didn't realize the compiler matters until i figured out. your posts were very help for me though. Thanks

just curious how it should be done outside eclipse... please correct me and help ..
1. right click on "my computer"
2. properties
3. Advance tab
4. Environment Variables
5. I see "PATH in User variables panel" and "Path in System variables panel" , which one should I add the "mysql-connector driver" to????

6. my system variables are as follow:
C:\Program Files\Common Files\ArcSoft\Bin;%SystemRoot%\system32;%SystemRoot%;%SystemRoot%\System32\Wbem;C:\Program Files\Intel\WiFi\bin\;C:\Program Files\ThinkPad\ConnectUtilities;C:\Program Files\Common Files\Lenovo;C:\WINDOWS\system32\WindowsPowerShell\v1.0;C:\Program Files\Java\jdk1.6.0_18\bin;C:\mysql-connector-java-5.0.8\mysql-connector-java-5.0.8-bin.jar

please notice the last variable for mysql-connector-java, does it look right? i still have that "failed to load mysql driver" message when running the program.

thanks very much

add the proper jars to the classpath of the application you're trying to run, the system classpath should hardly ever be used at all.

Not that you're using it now, you're adding the jar to the system PATH, not the CLASSPATH.

In sytem variable create a new variable named CLASSPATH and add value like below C:\Program Files\Apache Software Foundation\Tomcat 5.5\webapps\fuestation\WEB-INF\lib\mysql-connector-java-5.1.8-bin.jar or any path where mysql connector exists this might helps

I should downrep you for that advice. The system classpath is very nearly useless and suggesting to "newbies" to play around with it (even more to the point of adding it if it doesn't exist) is counter-productive to their development. This "advice" is just so wrong on so many levels, but it is not, in its technical form, that far off-base so I can't, personally, justify downreping it.

i got it work by adding the jar file to my eclipse library to run on eclipse. However, i am trying to run my program without eclipse but jdk only. for now if i do this

c:\mydir>java myprogram

i will have "failed to load mysql connector driver" message.

i appreciate all the helps so far, but can anyone please specifically direct me where i should add the mysql-connector jar file to? i tried both the user variable path and system variable path.. still not fixed.

so much wish to know this because i guess the way to setup this in unix environment would be similar.. thanks

You may want to use

java -classpath PATH_TO_CONNECTOR_INCLUDING_NAME YOUR_PROGRAM_NAME

for example JAR file on same level as your application

java -classpath mysql-connector-java-3.1.12-bin.jar MyDatabaseProgram
java -classpath .;mysql-connector-java-3.1.12-bin.jar MyDatabaseProgramjava -classpath mysql-connector-java-3.1.12-bin.jar MyDatabaseProgram

would actually be required :)

well spotted jwenting, it does show how often I use command line :$

java -classpath .;mysql-connector-java-3.1.12-bin.jar MyDatabaseProgramjava -classpath mysql-connector-java-3.1.12-bin.jar MyDatabaseProgram

would actually be required :)

Thanks! it works! ... however, is it possible to permanently add the driver to java's classpath so I won't have to type this long command every time?

i could script it... but i guess if there is a way to add the path .. would be nice. // i couldn't find any java -flag to add jars permanently

script it.
Way better approach. Other programs will need other libraries. Some probably need incompatible versions from others.
And if you distribute your program, you can distribute the libraries (if you're licensed to do so) and script so others can run it without having to mess with their system configuration (which they may not know how to do, not be allowed to do, or like many professionals may not want to do).

script it.
Way better approach. Other programs will need other libraries. Some probably need incompatible versions from others.
And if you distribute your program, you can distribute the libraries (if you're licensed to do so) and script so others can run it without having to mess with their system configuration (which they may not know how to do, not be allowed to do, or like many professionals may not want to do).

if this is the convention i would love to follow =)
thanks very much!

if this is the convention i would love to follow =)
thanks very much!

Also, if you distribute your app as a jarfile, and place the mysql jarfile alongside it, you can include a reference to that jar in your jars manifest and the command is just "java -jar <yourjarfile>" (assuming the mainclass attribute is set correctly). See http://java.sun.com/docs/books/tutorial/deployment/jar/index.html for further info.

Edit: And that command, not coincidentally, is usually what is recorded in the file associations for jarfiles.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.