I downloaded mysql-connector-java-5.1.18 and put it on my USB drive.
I brought my USB drive to school and opened up Eclipse, in there I added an external .jar
(the mysql-connector-java-5.1.18.jar).

Then I wrote this java code:

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

import java.util.Scanner;


public class Main
{
	private static Scanner scn = new Scanner(System.in);
	
	public static void main(String[] argv) throws Exception
	{
		
	Class.forName("com.mysql.jdbc.Driver");
	
		
Connection con = DriverManager.getConnection("jdbc:mysql://ip:3306/testdb", "username", "passwd");

PreparedStatement statement = con.prepareStatement("SELECT * FROM testtable");
ResultSet result = statement.executeQuery();
		
while( result.next() )
		{
	System.out.println(result.getString(1) + "\t\t" + result.getString(2));
		}
		result.close();
		con.close();
	
		
	}
}

I get this error:
java.io.EOFException: Can not read response from server. Expected to read 4 bytes, read 0 bytes before connection was unexpectedly lost.

What could be causing this?

After a few seconds, this showed up instead:

Exception in thread "main" com.mysql.jdbc.exceptions.jdbc4.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.
	at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
	at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
	at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
	at java.lang.reflect.Constructor.newInstance(Unknown Source)
	at com.mysql.jdbc.Util.handleNewInstance(Util.java:411)
	at com.mysql.jdbc.SQLError.createCommunicationsException(SQLError.java:1116)
	at com.mysql.jdbc.MysqlIO.<init>(MysqlIO.java:344)
	at com.mysql.jdbc.ConnectionImpl.coreConnect(ConnectionImpl.java:2332)
	at com.mysql.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:2369)
	at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2153)
	at com.mysql.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:792)
	at com.mysql.jdbc.JDBC4Connection.<init>(JDBC4Connection.java:47)
	at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
	at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
	at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
	at java.lang.reflect.Constructor.newInstance(Unknown Source)
	at com.mysql.jdbc.Util.handleNewInstance(Util.java:411)
	at com.mysql.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:381)
	at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:305)
	at java.sql.DriverManager.getConnection(Unknown Source)
	at java.sql.DriverManager.getConnection(Unknown Source)
	at Main.main(Main.java:16)
Caused by: java.net.ConnectException: Connection timed out: connect
	at java.net.DualStackPlainSocketImpl.connect0(Native Method)
	at java.net.DualStackPlainSocketImpl.socketConnect(Unknown Source)
	at java.net.AbstractPlainSocketImpl.doConnect(Unknown Source)
	at java.net.AbstractPlainSocketImpl.connectToAddress(Unknown Source)
	at java.net.AbstractPlainSocketImpl.connect(Unknown Source)
	at java.net.PlainSocketImpl.connect(Unknown Source)
	at java.net.SocksSocketImpl.connect(Unknown Source)
	at java.net.Socket.connect(Unknown Source)
	at java.net.Socket.connect(Unknown Source)
	at java.net.Socket.<init>(Unknown Source)
	at java.net.Socket.<init>(Unknown Source)
	at com.mysql.jdbc.StandardSocketFactory.connect(StandardSocketFactory.java:257)
	at com.mysql.jdbc.MysqlIO.<init>(MysqlIO.java:294)
	... 15 more

Edited 5 Years Ago by sha11e: n/a

Are you able to ping the given IP address? Can you connect to the database using a SQL explorer like MySQL workbench on the given port? How are you confirming that the database really is alive?

This article has been dead for over six months. Start a new discussion instead.