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 );
		}

Dani AI

Generated

Quick checklist and the root causes you missed: phpMyAdmin is a browser-based administration UI — it is not the database endpoint, so you must not use its HTTP URL inside a JDBC URL. The JDBC-ODBC bridge (the sun.jdbc.odbc driver) is not the right layer for MySQL and has been removed from modern JDKs; use the official MySQL JDBC driver (Connector/J) instead. (phpmyadmin.net)

Driver and URL details (what and were getting at): the JDBC URL must point at the MySQL server (host[:port]/database); the server defaults to port 3306. Connector/J’s driver class changed in newer releases (older 5.x used com.mysql.jdbc.Driver, newer 8.x+ uses com.mysql.cj.jdbc.Driver), and modern JDBC drivers auto-register so an explicit Class.forName() is usually unnecessary if the driver JAR is on the runtime classpath. (docs.oracle.com)

How to run outside Eclipse (practical): put the Connector/J JAR into a lib/ folder inside your app directory and launch with a short classpath — do not rely on the system PATH. Example patterns (adjust for your package name and OS):

# Linux/macOS
java -cp "classes:lib/*" com.example.Main

# Windows (cmd.exe)
java -cp "classes;lib/*" com.example.Main

Or build an executable JAR and list the libraries in the JAR manifest Class-Path or produce a single "fat" JAR with your build tool so end users do not need long commands. (docs.oracle.com)

Fast troubleshooting steps (what to look for): a ClassNotFoundException for the MySQL driver means the connector JAR is not on the runtime classpath; a "No suitable driver" or connection failures usually mean the URL is wrong (do not include http:// or phpMyAdmin paths), the server is not listening on the expected host/port, or the MySQL user is not allowed from that host (remember user@'localhost' vs user@'127.0.0.1'). Test with the MySQL client (mysql -h 127.0.0.1 -P 3306 -u <user> -p) and then fix classpath/packaging so the connector JAR is visible to the JVM. (docs.oracle.com)

Acknowledgement: the thread already contains the right pointers from , , , and — the above ties those tips together and fills the gap between "it runs in Eclipse" and "how to run it from the command line or package it for distribution."

Recommended Answers

All 24 Replies

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.