package com.demo;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class Demo {
public static void main(String[] args) {
if (ConnectDB.connection() != null) {
System.out.println("Successful Connection");
} else {
System.out.println("Connection Failed");
}
ConnectDB.disconnect();
}
public class ConnectDB {
private static Connection connection;
public static Connection connection() {
try {
Class.forName("com.mysql.cj.jdbc.Driver");
connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/your_database", "username",
"password");
} catch (Exception e) {
e.printStackTrace();
connection = null;
}
return connection;
}
public static void disconnect() {
if (connection != null) {
try {
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
}