JDBC Mysql Connection Example

Jdbc Mysql Connection Example

Jdbc Mysql Connection Example

package com.technicalkeeda.jdbc;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

public class JdbcConnectionExample {

    public static void main(String[] args) {

        JdbcConnectionExample example = new JdbcConnectionExample();

        try {
            Connection connection = example.getConnection();
            connection.close();
            System.out.println("Connection Closed!!");
        } catch (SQLException e) {
            System.out.println(e.getMessage());
        }

    }

    private Connection getConnection() {
        Connection connection = null;
        try {
            Class.forName("com.mysql.jdbc.Driver");
            connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/technicalkeeda", "root", "");
            System.out.println("Connection Created!!");
        } catch (ClassNotFoundException e) {
            System.out.println(e.getMessage());
        } catch (SQLException e) {
            System.out.println(e.getMessage());
        }

        return connection;
    }
}

Output

Connection Created!!
Connection Closed!!