get MySQL Connection from single connection string - Java java.sql

Java examples for java.sql:MySQL

Description

get MySQL Connection from single connection string

Demo Code


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

public class Main {
    public static void main(String[] argv) throws Exception {
        System.out.println(getConnection());
    }/*from   ww  w .  j  a  va 2s. c  om*/

    static Connection conn = null;

    /**
     * @return connection object.
     */
    public static Connection getConnection() {
        try {
            conn = DriverManager.getConnection(getConnectionURL());
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return conn;
    }

    /**
     * @return connection url for the DB.
     */
    private static String getConnectionURL() {
        return "jdbc:mysql://localhost/contactmanager?user=root&password=lakers";
    }
}

Related Tutorials