gpt4 book ai didi

mysql - 我无法使用 Intellij 连接到 Maven 中的 mysql 服务器

转载 作者:行者123 更新时间:2023-11-29 10:20:27 26 4
gpt4 key购买 nike

我遇到问题,当我尝试连接数据库时出现错误:

[MySQL] Can't connect to the mysql server: Could not create connection to database server. Attempted reconnect 3 times. Giving up.

我能够连接到 Intelij 中的“数据库”选项卡,所以数据库可能没有任何问题,或者它是一个错误,但我无法修复它。我将衷心感谢您的帮助。我预先感谢您。

这是我的代码:

package Code;

import Database.MySQLManager;

public class ModuleSearcher {

public static void main(String[] args) {

MySQLManager mySQLManager = new
MySQLManager("localhost","username","password","login",3306);
mySQLManager.connect();


}

}

另一个:

package Database;

import java.sql.*;

public class MySQLManager {

private String hostname, username, password, database;
private int port;

private Connection con;

public MySQLManager( String hostname, String username, String password,
String database, int port ) {
this.hostname = hostname;
this.username = username;
this.password = password;
this.database = database;
this.port = port;
}

//<editor-fold desc="Connect">
public void connect( ) {
try {
this.con = DriverManager.getConnection( "jdbc:mysql://" + hostname +
":" + port + "/" + database +
"?autoReconnect=" + true, username, password );

if ( !con.isClosed( ) && con != null ) {
System.out.println( "[MySQL] Sucessfully connected." );
}

} catch ( SQLException err ) {
System.err.println( "[MySQL] Can't connect to the mysql server: " +
err.getMessage( ) );
}
}
//</editor-fold>

//<editor-fold desc="Close">
public void close( ) {
try {
if ( !con.isClosed( ) && con != null ) {
con.close( );
if ( con.isClosed( ) ) {
System.out.println( "[MySQL] Connection successfully
closed." );
} else {
System.err.println( "[MySQL] Can't close the mysql
connection" );
}
}
} catch ( SQLException err ) {
System.err.println( "[MySQL] Can't close the mysql connection: " +
err.getMessage( ) );
}
}
//</editor-fold>

//<editor-fold desc="Update">
public void update( String query ) {
try {
PreparedStatement ps = con.prepareStatement( query );
ps.executeUpdate( );
} catch ( SQLException err ) {
System.err.println( "[MySQL] Update error: " + err.getMessage( ) );
}
}
//</editor-fold>

//<editor-fold desc="Query">
public ResultSet query( String query ) {
ResultSet rs = null;
try {
PreparedStatement ps = con.prepareStatement( query );
rs = ps.executeQuery( );
} catch ( SQLException err ) {
System.err.println( "[MySQL] Query error: " + err.getMessage( ) );
}
return rs;
}
//</editor-fold>

//<editor-fold desc="Getter + Setter">
public String getHostname( ) {
return hostname;
}

public String getUsername( ) {
return username;
}

public String getPassword( ) {
return password;
}

public String getDatabase( ) {
return database;
}

public int getPort( ) {
return port;
}

public Connection getCon( ) {
return con;
}
//</editor-fold>

}

pom:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>ModuleSearcher</groupId>
<artifactId>ModuleSearcher</artifactId>
<version>1.0-SNAPSHOT</version>

<dependencies>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.9-rc</version>
</dependency>
</dependencies>

</project>

最佳答案

这与 Intellij 无关。看起来您的服务器没有在本地主机端口 3306 上运行。请检查您的服务器是否正在运行。

如果服务器是 5.x,也尝试使用 5.1.6 连接器。

关于mysql - 我无法使用 Intellij 连接到 Maven 中的 mysql 服务器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49353711/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com