create MySQL Connection - Java java.sql

Java examples for java.sql:MySQL

Description

create MySQL Connection

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(createConnection());
    }//  w w  w.  j  a v a  2  s .co m

    private static final String DRIVER_CLASS = "com.mysql.jdbc.Driver";
    private static final String CONNECTION_URL = "jdbc:mysql://127.0.0.1:3306/guns";
    private static final String CONNECTION_USERNAME = "root";
    private static final String CONNECTION_PASSWORD = "";

    public static Connection createConnection() throws SQLException {
        try {
            Class.forName(DRIVER_CLASS);

        } catch (Exception e) {
            System.err.println("Driver class is not found, cause:"
                    + e.getMessage());
        }
        return DriverManager.getConnection(CONNECTION_URL,
                CONNECTION_USERNAME, CONNECTION_PASSWORD);
    }
}

Related Tutorials