Java JDBC MySQL Connection getConnection()

Here you can find the source of getConnection()

Description

get Connection

License

BSD License

Declaration

public static Connection getConnection() 

Method Source Code

//package com.java2s;
/*L//w w  w  .j  a  v  a2  s  .  c o  m
 * Copyright Georgetown University, Washington University.
 *
 * Distributed under the OSI-approved BSD 3-Clause License.
 * See http://ncip.github.com/cab2b/LICENSE.txt for details.
 */

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

import java.util.Properties;

public class Main {
    private final static String driver = "com.mysql.jdbc.Driver";
    private static Properties serverProperties;

    public static Connection getConnection() {
        String ip = serverProperties.getProperty("database.server.ip");
        String port = serverProperties.getProperty("database.server.port");
        String name = serverProperties.getProperty("database.name");
        String userName = serverProperties.getProperty("database.username");
        String password = serverProperties.getProperty("database.password");
        String url = "jdbc:mysql://" + ip + ":" + port + "/" + name + "";

        Connection con = null;
        try {
            Class.forName(driver).newInstance();
            con = DriverManager.getConnection(url, userName, password);
        } catch (Exception e) {
            e.printStackTrace();
        }
        if (con == null)
            System.out.println("Got null connection");
        return con;
    }
}

Related

  1. getConnection()
  2. getConnection()
  3. getConnection()
  4. getConnection()
  5. getConnection()
  6. getConnection()
  7. getConnection()
  8. getConnection()
  9. getConnection()