我遇到了问题,当我尝试连接数据库时出现错误:
[MySQL]无法连接到mysql服务器:无法创建连接 到数据库服务器。尝试重新连接3次。放弃。
我能够与"数据库" 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>
答案 0 :(得分:1)
这与Intelij无关。看起来您的服务器没有在localhost端口3306上运行。检查您的服务器是否正在运行。
如果服务器是5.x,也尝试使用5.1.6连接器。