9.Write a Java program to manage the driver with path, username and password, if not successful then throw an exception
💡Code:
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class DatabaseManager {
public static void main(String[] args) {
try {
// Set your database connection parameters
String driver = "your_database_driver";
String path = "your_database_path";
String username = "your_username";
String password = "your_password";
// Load the database driver
Class.forName(driver);
// Establish the database connection
Connection connection = DriverManager.getConnection(path, username, password);
// If connection is successful, print a success message
System.out.println("Connected to the database successfully!");
// Perform other database operations as needed
// Close the database connection
connection.close();
} catch (ClassNotFoundException e) {
// Handle ClassNotFoundException (e.g., if the database driver is not found)
System.err.println("Database driver not found: " + e.getMessage());
} catch (SQLException e) {
// Handle SQLException (e.g., if there's an issue with the database connection)
System.err.println("Error connecting to the database: " + e.getMessage());
}
}
}
📸Output :