Skip to Main Content

Java Database Connectivity (JDBC)

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

[Help me!!] Getting a Stupid Exception when trying to connect Mysql Db

843859Nov 28 2007 — edited Nov 20 2014
I am gone insane with the exception that i am getting. I have a wrote an application which connects MySql database using JDBC and SSH. SSH part is ok but i am getting the exception in the JDBC part. The part is that everything was ok 2 weeks ago the code was working fine. But suddenly everything turned upside down with an unknown reason.

Here is my Exception:
Communications link failure due to underlying exception: 

** BEGIN NESTED EXCEPTION ** 

com.mysql.jdbc.CommunicationsException
MESSAGE: Communications link failure due to underlying exception: 

** BEGIN NESTED EXCEPTION ** 

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

STACKTRACE:

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:1997)
        at com.mysql.jdbc.MysqlIO.readPacket(MysqlIO.java:573)
        at com.mysql.jdbc.MysqlIO.doHandshake(MysqlIO.java:1044)
        at com.mysql.jdbc.Connection.createNewIO(Connection.java:2748)
        at com.mysql.jdbc.Connection.<init>(Connection.java:1553)
        at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:285)
        at java.sql.DriverManager.getConnection(DriverManager.java:582)
        at java.sql.DriverManager.getConnection(DriverManager.java:185)
        at simpledbaccess.Main.startDbConnection(Main.java:126)
        at simpledbaccess.Main.main(Main.java:154)


** END NESTED EXCEPTION **



Last packet sent to the server was 0 ms ago.
And here is the simplified version of my code:
 private static Connection sqlConn = null;
    private static String sqlUserId="xxxxx", sqlPassword = "yyyyy";
    private static String sqlUrl = "jdbc:mysql://localhost:5000/dbName";
    private static JSch jsch=null;
    private static Session session=null;
    private static String last_uh="";
    
      public static Session getSession(String user, String host, String password) {
        try {
            if (jsch == null) {
                jsch = new JSch();
            }

            String foo = user + "@" + host;
            if (last_uh.equals(foo) && session != null) {
                return session;
            }
            if (session != null) {
                try {
                    session.disconnect();
                } catch (Exception e) {
                }
                session = null;
            }
            Session _session = jsch.getSession(user, host, 22);
            _session.setUserInfo(new MyUserInfo(password));

            _session.connect();
            _session.setPortForwardingL(5000, host, 3306);
            last_uh = foo;
            session = _session;        
        } catch (JSchException ex) {
            java.util.logging.Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
        }
        return session;
    }
    /**
     * Starts the SSH Connection to the server
     */
    public static void sshConnect() {
     String user="caglar";
     String host="111.111.111.111";
     String pass="password";
     System.out.println("Connecting to "+user+"@"+host+"...");
     try{
      Session sesn=null;
      sesn=getSession(user,host, pass);
      System.out.println("Connected to "+user+"@"+host);
     }
     catch(Exception e){
      System.err.println(e);
     }
     }
   /**
    * Stops the Ssh Connection to the Server
    */
   public static void sshDisconnect() {
    try {
      if(session!=null){
	session.disconnect();
	session=null;
      }
    }
    catch (Exception e) {
        System.err.println(e.getMessage());
    }
    }
    /**
     * Starting the connection to the Mysql Database
     * @return an instance of Mysql Connection Object
     */
    
    public static void startDbConnection() {
      sshConnect();
      if(session.isConnected()){
          System.out.println("Successful SSH connection");
          try
           {
               Class.forName ("com.mysql.jdbc.Driver").newInstance ();
               sqlConn = (Connection) DriverManager.getConnection (sqlUrl, sqlUserId, sqlPassword);
               System.out.println ("Database connection established");
           }
           catch (Exception e)
           {
               System.err.println (e.getMessage());
           }
           finally
           {
               if (sqlConn != null)
               {
                   try
                   {
                       sqlConn.close ();
                       System.out.println ("Database connection terminated");
                   }
                   catch (Exception e) { /* ignore close errors */ }
               }
           }
        }
      else{
          System.err.println("No SSH and MYSQL connection!!!");
      }
    }
 
    public static void main(String[] args) {
        startDbConnection();
    }
The exception appears at the :
sqlConn = (Connection) DriverManager.getConnection (sqlUrl, sqlUserId, sqlPassword);
Although I am sure that sqlUrl , password and userid is correct. Because it was working 2 weeks ago. And I can connect to the Database by Putty, phpMyAdmin and Sql Manager for MySql. So I don't think that anything is wrong with the server side. And SSH is ok. I can establish SSH tunnel successfully with this code.

Thanks in advance.

Hope that someone could save me. I will take the pills beneath my computer.

Ps: I don't have much time :(.
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Dec 27 2007
Added on Nov 28 2007
8 comments
915 views