tags:

views:

104

answers:

2

I am getting this error:

       FOR REAL Looking for database...
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:409)
    at com.mysql.jdbc.SQLError.createCommunicationsException(SQLError.java:1118)
    at com.mysql.jdbc.MysqlIO.readPacket(MysqlIO.java:675)
    at com.mysql.jdbc.MysqlIO.doHandshake(MysqlIO.java:1078)
    at com.mysql.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:2312)
    at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2122)
    at com.mysql.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:774)
    at com.mysql.jdbc.JDBC4Connection.<init>(JDBC4Connection.java:49)
    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:409)
    at com.mysql.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:375)
    at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:289)
    at java.sql.DriverManager.getConnection(Unknown Source)
    at java.sql.DriverManager.getConnection(Unknown Source)
    at test.init(test.java:38)
    at sun.plugin2.applet.Plugin2Manager$AppletExecutionRunnable.run(Unknown Source)
    at java.lang.Thread.run(Unknown Source)
Caused by: java.io.EOFException: Can not read response from server. Expected to read 4 bytes, read 0 bytes before connection was unexpectedly lost.
    at com.mysql.jdbc.MysqlIO.readFully(MysqlIO.java:2502)
    at com.mysql.jdbc.MysqlIO.readPacket(MysqlIO.java:599)
    ... 17 more
java.lang.NullPointerException
    at test.init(test.java:69)
    at sun.plugin2.applet.Plugin2Manager$AppletExecutionRunnable.run(Unknown Source)
    at java.lang.Thread.run(Unknown Source)
Exception: java.lang.NullPointerException

when I am trying to connect to my MySQL online.

Here's my code:

(yes, it's signed)

  //package mysqltest;
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import java.applet.Applet;
    import java.awt.TextArea.*;
    import java.sql.*;
    import java.util.*;
    import javax.swing.plaf.*;
    import javax.swing.plaf.basic.*;
    import java.net.*;
    import java.applet.*;

    public class test extends JApplet
    {
        public JTextArea c;
        public void init()
        {
            c = new JTextArea();
            add(c);
            c.append("xxxLooking for database...");
            Connection conn = null;
            Properties props = new Properties();
            String url = "jdbc:mysql://localhost:3306/";
            String dbName = "mystik";
            String driver = "com.mysql.jdbc.Driver";
            String userName = "root";
            String password = "";
            String loggedusername = getParameter("name");
            boolean online = false;
            try
            {
                Class.forName(driver).newInstance();
                online = true;
                if (online)
                {
                    // if user loads applet online 
conn = DriverManager.getConnection("jdbc:mysql://epic.0sites.net:208/*********?user=*******&password=**********");
                }
                else
                {
                    // for localhost - testing purposes props.put("user", "root");
                    conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/mystik", props);
                }
                c.append("\nConnected to the database");
                c.append("\nGetting stats for: " + loggedusername);
                PreparedStatement statement = conn.prepareStatement( "select * from `user` where `username` = '"+loggedusername+"'");
                ResultSet result = statement.executeQuery();
                // just a dumb mysql statement! while(result.next())
                {
                    c.append("\nUsername: "+result.getString(2)+ "\nLevel: "+result.getString(6)+"\nEXP: "+result.getString(8)+"\n");
                }
                PreparedStatement updateEXP = conn.prepareStatement( "update`user` set `exp` = '666' where `username` = '"+loggedusername+"'");
                updateEXP.executeUpdate();
                ResultSet xresult = statement.executeQuery();
                while(xresult.next())
                {
                    c.append("\nUsername: "+xresult.getString(2)+ "\nLevel: "+xresult.getString(6)+"\nEXP: "+xresult.getString(8)+"\n");
                }
                conn.close();
                c.append("\nDisconnected from database");
            }
            catch (Exception e)
            {
                e.printStackTrace();
            }
        }
    }

What am I doing wrong? Where did i get the epic0.sites.net URL, you say? well If I go to https://epic.0sites.net:2083/3rdparty/phpMyAdmin/ I can reach my phpMyAdmin... I didn't think it would work.. it didn't. I starred out sensitive info.

A: 

The important part of the exception stack trace seems to be:

Caused by: java.io.EOFException: Can not read response from server. Expected to read 4 bytes, read 0 bytes before connection was unexpectedly lost.

So it looks like you can connect but then the connection is closed. Maybe you're not connecting to a mysql server ? (Is there something else running on this port ?)

Andre Holzner
No I don't think so.
Dan
What `jdbc:mysql://epic.0sites.net:208/***` in your example starred out from `jdbc:mysql://epic.0sites.net:2083/***`, where 2083 is the port where an HTTPS server is running (from your HTTPS URL example)? If so, @Andre is right.
Bruno
+4  A: 

Most likely your DB has run out of connections. That's one of the caveats of not closing connections properly in finally. I've warned about this in your previous question.

The remedy is to restart the DB in question and fix your code accordingly that it gracefully closes the resources in finally.

BalusC
I have conn.close() at the very end...
Dan
It was originally at end of the `try`, yes. It won't be invoked when an exception is been thrown *before* that line is reached. As per your previous question, it has likely occurred more than once. Now the DB has run out of connections. You need to restart the DB or wait about 8 hours until the DB forcibly closes the too-long opened connections. Otherwise you keep getting this exception, even when you have fixed the code.
BalusC
add I see. that worked. :) but now i got new problem. i added it in. i will see if i can fix it.
Dan
You need to ask a new question about the new problem.
BalusC
Wait It doesn't even connect to mysql... let me rework on this
Dan
back to the old problem.
Dan
Well, either the DB is not properly restarted or the user is been blocked due to abuse (when it concerns a 3rd party hosted DB). By the way, why did you add this unrelated NPE to the stacktrace?
BalusC
It's a regular Exception. I don't get what you are saying.
Dan
Maybe I didn't sign the applet right?
Dan
The sign is not the issue. That would have thrown a different exception. What I am saying, if you are still getting a communications link failure, then it means that the DB still doesn't have a connection to give back to you. To fix this you need to restart the DB or to wait about 8 hours (depending on DB configuration). Because the DB seem to be a 3rd party hosted one which is been shared, I suspect that you didn't restart it at all.
BalusC