Java JDBC MySQL Connection getConnection()

Here you can find the source of getConnection()

Description

get Connection

License

Apache License

Declaration

static public Connection getConnection() 

Method Source Code


//package com.java2s;
/*//w w w . j  av  a  2 s  .c o  m
 * Copyright 2015 Shivakshi Chaudhary/Ionite Technologies LLP
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *   http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * 
 */

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

public class Main {
    static public Connection getConnection() {

        String url = "jdbc:mysql://xxxxinstance.xxxxxxx.us-east-1.rds.amazonaws.com:3306/";
        String userName = "xxxxxxxxx";
        String password = "xxxxxxxxx";
        String dbName = "xxxxxxxx";
        String driver = "com.mysql.jdbc.Driver";

        Connection connection = null;

        try {
            Class.forName("com.mysql.jdbc.Driver");
            connection = DriverManager.getConnection(url + dbName, userName, password);
            connection.setAutoCommit(false);
            if (connection != null) {
                System.out.println("connected to database.");
            }
        } catch (ClassNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return connection;
    }
}

Related

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