6 Replies Latest reply on Feb 8, 2010 5:02 PM by 843859

    MySQL DJBC driver communications link failure

    843859

      I'm having difficulty connecting to a DB using JDBC. Here is my code.

      package db.playground; import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; public class MySQLConnect { /** * Test connecting to a database. * @param args */ public static void main(String[] args) { Connection conn = null; try { System.out.println("trying connection"); Class.forName("com.mysql.jdbc.Driver").newInstance();     conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/test_database", "brian", "letmein");     System.out.println("connected");     // Do something with the Connection     conn.close(); } catch (SQLException ex) {     // handle any errors         //ex.printStackTrace();     System.out.println("SQLException: " + ex.getMessage());     System.out.println("SQLState: " + ex.getSQLState());     System.out.println("VendorError: " + ex.getErrorCode()); }catch(Exception e) { System.out.println("Error:" + e); e.printStackTrace(); } } }

      This results in:

      trying connection SQLException: Communications link failure Last packet sent to the server was 0 ms ago. SQLState: 08S01 VendorError: 0

      I verified that the server was running (/etc/init.d/mysqld restart), and that it's running with networking enabled. The server is running locally, so I doubt that a firewall is an issue. I also verified that the port is correct.

      I'm lead to believe that the problem is with the driver, because I receive the same problem no matter if the server is running or not. The MySQL administrator program capable of connecting to the DB, so I doubt that the problem is with the DB, but I haven't ruled that out yet. I was also able to telnet to the port MySQL listens on, and received the expected garbage characters.

      I have been working on this for about 7 hours, and can not figure out what the problem is. The first few hundred google results were not of much help, either.

      Edited by: bnlott on Feb 7, 2010 10:09 PM