JDBC Mysql Connection Example

JDBC Mysql Connection Example

By Yashwant Chavan, Views 1069, Date 25-May-2012

Jdbc Mysql Connection Example

tags java

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!!
Yashwant

Yashwant

Hi there! I am founder of technicalkeeda.com and programming enthusiast. My skills includes Java,J2EE, Spring Framework, Nodejs, PHP and lot more. If you have any idea that you would want me to develop? Lets connect: yashwantchavan[at][gmail.com]