3

我正在尝试将我的代码从 linux 移植到 mac OSX LION。以下方法适用于 linux 就好了。

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

但它不适用于我的mac。我正在使用 XAMMP,所以我的数据库的路径是/Applications/XAMPP/xamppfiles/bin/mysql. 我得到的错误读取

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)

根据以下帖子的反馈进行更新:

我下载了 jar 并将其添加到项目的构建路径中。当我尝试添加时Class.forName("com.mysql.jdbc.Driver"),出现编译错误,因此我将其注释掉。然后我运行程序得到以下错误:

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)
4

3 回答 3

7

JAVA JDK 不附带特定的 SQL 驱动程序(即 mySQL、postgre、MS SQL 等)。您需要单独下载和安装它们。可以在此处找到 java mysql 连接器的下载链接:

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

它说将驱动程序放在你的类路径中,但你也可以将它放在你的 IDE 的构建路径中,这可能更容易取决于你在做什么。

于 2012-09-25T20:12:30.863 回答
3

确保 mysql jdbc 连接器位于 CLASSPATH 中。您可能还需要加载驱动程序,在尝试获取连接之前添加以下行:

Class.forName("com.mysql.jdbc.Driver");
于 2012-09-25T20:14:44.450 回答
0

你需要在你的mac的类路径上有你的mysql jdbc连接器jar。

于 2012-09-25T20:11:44.233 回答