【发布时间】:2018-09-27 04:15:47
【问题描述】:
我正在尝试连接到远程服务器上的 MySQL 数据库,但我收到一个异常,即查询对于数据包来说太大了。我什至没有发送任何查询。
com.mysql.cj.jdbc.exceptions.PacketTooBigException:用于查询的数据包太大(4 739 923 > 65 535)。您可以通过设置“max_allowed_packet”变量在服务器上更改此值。
代码:
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
public class Main {
static Connection connection = null;
static PreparedStatement preparedStatement = null;
public static void main(String[] argv) {
try {
makeJDBCConnection();
preparedStatement.close();
connection.close(); // connection close
} catch (SQLException e) {
e.printStackTrace();
}
}
private static void makeJDBCConnection() {
try {
Class.forName("com.mysql.cj.jdbc.Driver");
} catch (ClassNotFoundException e) {
log("Couldn't found JDBC driver.");
e.printStackTrace();
return;
}
try {
// DriverManager: The basic service for managing a set of JDBC drivers.
connection = DriverManager.getConnection("jdbc:mysql://193.219.91.103:13667", "root", "root");
if (connection != null) {
log("Connection Successful! ");
} else {
log("Failed to make connection!");
}
} catch (SQLException e) {
log("MySQL Connection Failed!");
e.printStackTrace();
return;
}
}
private static void log(String string) {
System.out.println(string);
}
}
【问题讨论】:
-
mysql的版本是多少?
-
检查这个链接它已经在这里回答了! stackoverflow.com/questions/11320236/…
-
这个答案似乎更有帮助:https://stackoverflow.com/a/27303694/8097737 它表明你使用了错误的 url/portnumber。
-
@devpuh 看来你是对的,我的 URL 失败了。显然你需要去 IP:PORT/DATABASE。认为这对我来说是另一个例外。
com.mysql.cj.jdbc.exceptions.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.