I can connect from the services window but whenever I try to set the code myself on my app it tells me it cannot connect to the database.

Here's my code:

import java.sql.*;
public class LogBook {

    public static void main (String[] args)
       {
           connectToDB();
       }
    // Connect to the Database
    public static void connectToDB() {
        
        Connection connection = null;

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

Not sure what I'm missing here

Dani AI

Generated

Good catch by — the symptom ("cannot connect") very often means the MySQL Connector/J JAR was not on the runtime classpath, so the driver class could not be found when DriverManager tried to open a connection. The official Connector/J notes call this out; make sure the connector JAR you tested with the services window is also on the same classpath that runs your app. (See MySQL Connector/J usage notes.)
MySQL Connector/J — DriverManager usage

Checklist to make the fix repeatable and avoid surprises:

  • For simple programs, add the connector JAR to the JVM classpath (or build a fat JAR). In IDEs add it as a project library (Eclipse/IntelliJ/NetBeans have explicit Library settings).
  • For webapps, put the driver where the container expects it or follow the container classloader guidance — putting a driver in the wrong place can hide it from the code that creates pooled DataSources and can cause classloader conflicts. (Tomcat documents these classloader and JNDI datasource issues.)
    Tomcat — Class Loader / JNDI datasource notes

A few modern gotchas to be aware of:

  • Use the right Connector/J version for your MySQL server; the driver class name and supported features changed in newer Connector/J releases (com.mysql.cj.jdbc.Driver for recent Connector/J). If your server is MySQL 8+, make sure the connector supports the default auth plugin (caching_sha2_password) or create the DB user with an auth method the driver understands.
    Connector/J driver nameMySQL caching_sha2_password notes

Quick troubleshooting tips: print the exception stacktrace instead of swallowing the exception, confirm the server is listening on the expected host/port (and whether you need TCP vs socket), verify username/password and privileges, and prefer dependency management (Maven/Gradle) so the correct connector version is always on the runtime classpath.

God I;m stupid, I forgot to add the classpath to my application for the driver

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.