StudyLover
  • Home
  • Study Zone
  • Features
    • Workout Interval Timer
    • Typing Tutor
  • Profiles
  • Contact us
  • Sign in
StudyLover The JDBC Connector
Download
  1. Java
  2. Programs
The Phonebook Map : The SQL Executor
Programs
Initiative: To establish a database connection using the DriverManager and handle SQL exceptions.
import java.sql.*; // Import all SQL related classes
import java.util.Properties; // Import properties for connection settings

public class JdbcConnector { // Define class to handle database connectivity
    public static void main(String[] args) { // Entry point for the DB lab
        System.out.println("--- JDBC Handshake Lab ---"); // Print system header
        
        String url = "jdbc:mysql://localhost:3306/college_db"; // Define connection URL for local MySQL
        Properties auth = new Properties(); // Create a properties object to hold credentials
        auth.put("user", "admin"); // Set the username property
        auth.put("password", "root123"); // Set the password property
        
        System.out.println("Attempting connection to: " + url); // Log the connection attempt
        
        // Establish connection inside try-with-resources for automatic cleanup
        try (Connection conn = DriverManager.getConnection(url, auth)) { // Attempt the DB connection
            
            if (conn != null) { // Check if the connection was successful (not null)
                System.out.println("CONNECTION SUCCESSFUL!"); // Log success message
                
                DatabaseMetaData meta = conn.getMetaData(); // Fetch metadata about the connected database
                System.out.println("DB Product: " + meta.getDatabaseProductName()); // Show DB name
                System.out.println("DB Version: " + meta.getDatabaseProductVersion()); // Show DB version
            } // End of success block
            
        } catch (SQLException e) { // Catch any errors that occur during the DB attempt
            System.err.println("CONNECTION FAILED!"); // Log error header
            System.err.println("Message: " + e.getMessage()); // Print the specific error message
            System.err.println("SQL State: " + e.getSQLState()); // Print the standardized SQL error code
            System.out.println("Fix: Check if MySQL is running on port 3306."); // Offer troubleshooting advice
        } // End of catch block
        
        System.out.println("JDBC Lab: Connectivity Test Ended."); // Final log for the JDBC lab
    } // End of main method
} // End of JdbcConnector class
The Phonebook Map The SQL Executor
Our Products & Services
  • Home
Connect with us
  • Contact us
  • info.studylover@gmail.com

StudyLover - About us

The Best knowledge for Best people.

Copyright © StudyLover
Powered by Odoo - Create a free website