Apache Tomcat uses the MySQL XAMPP database

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

I include TOMCAT and XAMPP.

Then I try to connect to 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 returns to me "Unable to connect to the database server."

+3
source share
1 answer

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

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

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

Then it will work.

+2
source

All Articles