【发布时间】:2017-08-12 21:04:46
【问题描述】:
我只是无法使用 Eclipse 连接到 mySQL,也不知道为什么。
这是我的连接类:
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import com.mysql.jdbc.Connection;
import com.mysql.jdbc.Statement;
public class ConnexionBDD {
static String url = "jdbc:mysql://localhost:8888/Peoples?autoReconnect=true&useSSL=false";
static String login = "root";
static String password = "";
static Connection connection = null;
static Statement statement = null;
static ResultSet result = null;
static String request = "";
public static void main (String[] args) {
System.out.println("will load driver");
loadingDrive();
System.out.println("will connect");
connection();
}
public static void loadingDrive() {
try {
Class.forName("com.mysql.jdbc.Driver");
} catch ( ClassNotFoundException e ) {
e.getMessage();
}
}
public static void connection() {
try
{
connection = (Connection) DriverManager.getConnection(url, login, password);
System.out.println("Connected");
statement = (Statement) connection.createStatement();
result = statement.executeQuery(request);
}
catch ( SQLException e )
{
System.out.println(e.getMessage());
} finally {
if ( result != null ) {
try {
result.close();
} catch ( SQLException ignore ) {
}
}
if ( statement != null ) {
try {
statement.close();
} catch ( SQLException ignore ) {
}
}
if ( connection != null ) {
try {
connection.close();
} catch ( SQLException ignore ) {
}
}
}
}
}
这是控制台中的结果:
will load driver
will connect
Could not create connection to database server. Attempted reconnect 3 times. Giving up.
我在文件 WebContent/WEB-INF/lib 中有连接器 (mysql-connector-java-5.1.41-bin.jar)。
我在我的 Mac 上安装了 mySQL。 我安装了 MAMP,我可以访问 phpmyadmin 并添加一个新数据库,但这有点奇怪,phpmyadmin 已经被记录为默认值,单击“退出”按钮并没有断开我的连接,我不知道为什么..
phpmyadmin 的网址是: http://localhost:8888/phpmyadmin/index.php
为什么我无法连接到 mySQL ?
编辑: 在 MAMP 上检查 mySQL 端口后,我使用正确的端口 (8889) 更改了我的 url,但控制台的输出没有任何变化。
【问题讨论】:
-
如果phpmyadmin在8888端口,那我觉得你的mysql也不可能在8888端口。你能检查一下吗?
-
可以发一下连接字符串吗?
-
显然是 8889,但在 url 中更改它不会更改控制台上的结果。
-
连接字符串是什么意思?
-
getConnection方法的url参数