3

I have installed Tomcat 6 and apache XAMPP on MAC OS. XAMPP includes MySQL.

I turn on TOMCAT and XAMPP.

Then i try to connect with JDBC to MySQL.

public class main {

    public static void main(String[] args) {

        Connection conn = null;
        try
        {
            String userName = "root";
            String password = "";
            //<facility> is the name of the database i created
            String url = "jdbc:mysql://localhost/facility"; 
            Class.forName ("com.mysql.jdbc.Driver").newInstance ();
            conn = DriverManager.getConnection (url, userName, password);
            System.out.println ("Database connection established");
        }
        catch (Exception e)
        {
            System.out.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 */ }
            }
        }


    }
}

Well it gives me back "Cannot connect to database server".

7
  • Is it possible to interact XAMPP with TOMCAT without any further configuration. In my example i get an exception that connection failed. Commented Mar 22, 2011 at 11:23
  • Can the phpMyAdmin in xampp connect with mysql? Commented Mar 22, 2011 at 11:24
  • 1
    print the exception and read the message: System.out.println ("Cannot connect to database server " + e.getMessage()); Commented Mar 22, 2011 at 11:26
  • Yes it can. With phpMyAdmin i created the "facility" database Commented Mar 22, 2011 at 11:26
  • 1
    Can you also print the exception type e.getClass().getName()? I'm thinking it might not be able to find/load the driver. Commented Mar 22, 2011 at 11:30

1 Answer 1

2

The problem is at the JDBC driver. You must include it to your classpath.

You download it here: http://dev.mysql.com/downloads/connector/j/5.0.html

Add the mysql-connector-java-5.1.15-bin.jar to your classpath

Then it will work.

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

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.