Constructor will create a connection to the MySQL Database. - Java java.sql

Java examples for java.sql:MySQL

Description

Constructor will create a connection to the MySQL Database.

Demo Code


//package com.java2s;
import java.sql.Connection;
import java.sql.DriverManager;

import java.sql.SQLException;

public class Main {


    final static String JDBC_DRIVER = "com.mysql.jdbc.Driver";
    private static Connection connection;

    /**//  www  .  java2  s.  c  o m
     * Constructor will create a connection to the DB. This will need to be
     * improved to use connection pooling and non-persistent connections
     * 
     * @param databaseName
     *            - The name of the database being used
     * @param username
     *            - The username for the database
     * @param password
     *            - The password for the database
     * @return 
     */
    public static Connection createConnection(String url, String username,
            String password) throws ClassNotFoundException, SQLException {

        // Make sure the JDBC driver is loaded into memory
        Class.forName(JDBC_DRIVER);
        try {
            connection = DriverManager.getConnection(url, username,
                    password);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return connection;
    }
}

Related Tutorials