1

I am trying to connect to a external database , but it seems that i may be making a mistake on the way i am putting the DriverManager connection, this is the first time i am connecting using this driver, Can you please point me in the right direction? thank you (might be something wrong on the getConnection call)

Class.forName( "com.mysql.jdbc.Driver" ) ;

       // Get a connection to the database
       Connection conn = DriverManager.getConnection( "jdbc:mysql://cs.cis.can.edu;databaseName=mar200;user=utest;password=utest" ) ;

Error

SQL Exception:
State  : 08S01
Message: Communications link failure

Last packet sent to the server was 0 ms ago.
Error  : 0
3
  • What error or exception are you seeing? Commented Feb 10, 2012 at 3:06
  • I posted the exception but can you tell if my code is correct (the connection string)? Commented Feb 10, 2012 at 3:12
  • Check this link and it will help you create proper connection string. MySql Connection String Commented Feb 10, 2012 at 3:22

2 Answers 2

2

instead of passing one string argument, try passing each separatly like

    getConnection(String url, String user, String password)

here is the guide to the DriverManager class http://docs.oracle.com/javase/7/docs/api/java/sql/DriverManager.html

Sign up to request clarification or add additional context in comments.

Comments

1

Instead of passing argument in one instance you can use separate of username, password and driver.

Connection conn = null;

           try
           {
               String userName = "testuser";
               String password = "testpass";
               String url = "jdbc:mysql://localhost/test";
               Class.forName ("com.mysql.jdbc.Driver").newInstance ();
               conn = DriverManager.getConnection (url, userName, password);
               System.out.println ("Database connection established");
           }
           catch (Exception e)
           {
               System.err.println ("Cannot connect to database server");
           }
           finally
           {
               if (conn != null)
               {
                   try
                   {
                       conn.close ();
                       System.out.println ("Database connection terminated");
                   }
                   catch (Exception e) { /* ignore close errors */ }
               }
           }

http://www.kitebird.com/articles/jdbc.html

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.