我正在尝试连接到远程服务器上的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);
}
}
答案 0 :(得分:-1)
如果您有权访问服务器端,则必须更改mysql配置文件。见https://dev.mysql.com/doc/refman/5.6/en/option-files.html