3

I am trying to port my code from linux to mac OSX LION. The following method works on linux just fine.

Connection getConnection() throws SQLException{
    String url = "jdbc:mysql://localhost/";
    return DriverManager.getConnection(url, "root", "mypassword");
}

But it's not working on my mac. I am using XAMMP so the path to my database is /Applications/XAMPP/xamppfiles/bin/mysql. The error I get reads

Exception in thread "main" java.sql.SQLException: 
    No suitable driver found for jdbc:mysql://localhost/
at java.sql.DriverManager.getConnection(DriverManager.java:602)
at java.sql.DriverManager.getConnection(DriverManager.java:185)

UPDATES BASED ON FEEDBACK FROM POSTS BELOW:

I downloaded the jar and add it to the project's build path. When I try to add Class.forName("com.mysql.jdbc.Driver") I get compile error so I comment it out. Then I run the program to get the following error:

Exception in thread "main" com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure

The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
at java.lang.reflect.Constructor.newInstance(Constructor.java:513)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:411)
at com.mysql.jdbc.SQLError.createCommunicationsException(SQLError.java:1117)
at com.mysql.jdbc.MysqlIO.<init>(MysqlIO.java:350)
at com.mysql.jdbc.ConnectionImpl.coreConnect(ConnectionImpl.java:2408)
at com.mysql.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:2445)
at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2230)
at com.mysql.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:813)
at com.mysql.jdbc.JDBC4Connection.<init>(JDBC4Connection.java:47)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
at java.lang.reflect.Constructor.newInstance(Constructor.java:513)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:411)
at com.mysql.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:399)
at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:334)
at java.sql.DriverManager.getConnection(DriverManager.java:582)
at java.sql.DriverManager.getConnection(DriverManager.java:185)
at learning.database.Classroom.getConnection(Classroom.java:42)
at learning.database.Classroom.main(Classroom.java:239)

Caused by: java.net.ConnectException: Connection refused
at java.net.PlainSocketImpl.socketConnect(Native Method)
at java.net.PlainSocketImpl.doConnect(PlainSocketImpl.java:351)
at java.net.PlainSocketImpl.connectToAddress(PlainSocketImpl.java:213)
at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:200)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:432)
at java.net.Socket.connect(Socket.java:529)
at java.net.Socket.connect(Socket.java:478)
at java.net.Socket.<init>(Socket.java:375)
at java.net.Socket.<init>(Socket.java:218)
at com.mysql.jdbc.StandardSocketFactory.connect(StandardSocketFactory.java:259)
at com.mysql.jdbc.MysqlIO.<init>(MysqlIO.java:300)
... 16 more

Java Result: 1
BUILD SUCCESSFUL (total time: 0 seconds)
kasavbere
  • 5,873
  • 14
  • 49
  • 72
  • U need to tell to JVM to load driver. here is how it is: Class.forName("com.mysql.jdbc.Driver"); – Elbek Sep 25 '12 at 20:20
  • Another problem that may be a part of this, I don't know. On linux going to localhost/phpmyadmin does not require a password (there is one to enter the mysql view). On mac however, I must enter a password for xampp and then one to log-in. – kasavbere Sep 25 '12 at 21:29

3 Answers3

7

The JAVA JDK does not come with specific SQL drivers(i.e mySQL, postgre, MS SQL, etc). You need to download and install them separately. The download link for the java mysql connector can be found here:

http://dev.mysql.com/downloads/connector/j/

It says to put the driver in your class path, but you can also put it in your IDE's build path, it might be easier depending on what you are doing.

Scott
  • 12,077
  • 4
  • 27
  • 48
  • I have updated the post to show the new error after adding the driver. – kasavbere Sep 25 '12 at 21:24
  • Well, the new error isn't entirely bad news. It does mean that the driver loaded properly and your project can see the database. The error you are getting now (java.net.ConnectException: Connection refused) is caused by your SQL server rejecting your credentials. Assuming this code worked properly on your linux box, the error exists outside of your code. Check your user table and make sure your credentials are right. One thing you can do (although it creates a security hole in the long run is give your user full permissions @'%'. ('root'@'%' allows the root user to log in from anywhere) – Scott Sep 25 '12 at 21:37
  • my knowledge is not that advance. How do I implement `'root'@'%'`? Do I just type it in the terminal? – kasavbere Sep 25 '12 at 21:52
  • Log into mySQL CREATE USER 'root'@'%' IDENTIFIED BY 'somepass'; GRANT ALL PRIVILEGES ON \*.\* TO 'root'@'%' WITH GRANT OPTION; I just want to repeat that this does open up a security loophole as it allows anybody to try and guess your root password. That is what the '%' sign does, but it might help you debug this issue. http://dev.mysql.com/doc/refman/5.1/en/create-user.html http://dev.mysql.com/doc/refman/5.1/en/adding-users.html – Scott Sep 25 '12 at 21:55
  • I create the new user per the links, and when I try to log into mysql as the new user, I get the error: `ERROR 1045 (28000): Access denied for user 'mrme'@'localhost' (using password: YES)`. However, when I do `select Host,User,Password from mysql.user;` I see the user with `Host` listed as `%` – kasavbere Sep 25 '12 at 22:50
  • create the user the same way (granting the same privileges) with @'localhost' – Scott Sep 26 '12 at 00:28
  • 2
    I solved the problem. I am accepting this answer because it solved the first part of the problem. The link to the solution of the second part is here: http://stackoverflow.com/questions/12593634/com-mysql-jdbc-exceptions-jdbc4-communicationsexception-communications-link/12605551#12605551 – kasavbere Sep 26 '12 at 15:48
  • I am glad you got it working, sorry I couldn't be more help with the 2nd error. – Scott Sep 26 '12 at 15:56
  • Oh really, thank you very much for your help. You persisted patiently when you didn't have to. People like you make stackoverflow.com great! Thanks – kasavbere Sep 26 '12 at 17:45
3

Make sure the mysql jdbc connector is in the CLASSPATH. You probably also need to load the driver, adding the following line before trying to get the connection:

Class.forName("com.mysql.jdbc.Driver");
José Ricardo
  • 1,479
  • 1
  • 14
  • 28
0

You need to have you mysql jdbc connector jar on the classpath for your mac.

Paul
  • 1,375
  • 4
  • 16
  • 29