0

I am trying to connect to my MySQL database on remote server but I get an exception that query is to large for packet. I don't even sent any queries.

com.mysql.cj.jdbc.exceptions.PacketTooBigException: Packet for query is too large (4 739 923 > 65 535). You can change this value on the server by setting the 'max_allowed_packet' variable.

Code:

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);
    }
}
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Sande
  • 131
  • 1
  • 1
  • 9
  • what is the version of mysql ? – Elarbi Mohamed Aymen Apr 17 '18 at 09:10
  • 1
    Check this link its already answered here! https://stackoverflow.com/questions/11320236/com-mysql-jdbc-packettoobigexception – arjun Apr 17 '18 at 09:29
  • This answer seem to be more helpful: [https://stackoverflow.com/a/27303694/8097737](https://stackoverflow.com/a/27303694) It suggest that you are using the wrong url/portnumber. –  Apr 17 '18 at 09:37
  • @devpuh seems you were right, I failed with my URL . Apparently you need go IP:PORT/DATABASE. Thought that hit me with another exception. `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.` – Sande Apr 17 '18 at 17:35
  • For this exception take a look at these answers: [https://stackoverflow.com/a/2985169](https://stackoverflow.com/a/2985169/8097737) and [https://stackoverflow.com/a/10772407](https://stackoverflow.com/a/10772407/8097737) –  Apr 18 '18 at 05:26
  • That was helpfull had few other errors but that also helped, thanks alot @devpuh – Sande Apr 19 '18 at 09:55

1 Answers1

-1

If you have access to server side, you must change the mysql configuration file. See https://dev.mysql.com/doc/refman/5.6/en/option-files.html

Elarbi Mohamed Aymen
  • 1,617
  • 2
  • 14
  • 26