Java JDBC Connection Create getConnection()

Here you can find the source of getConnection()

Description

get Connection

License

Open Source License

Declaration

public static Connection getConnection() throws SQLException, ClassNotFoundException, IOException 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import java.io.IOException;

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

public class Main {
    private static String url;
    private static String username;
    private static String password;
    private static ThreadLocal<Connection> conList = new ThreadLocal<Connection>();

    public static Connection getConnection() throws SQLException, ClassNotFoundException, IOException {
        Connection con = conList.get();
        if (con == null) {
            try {
                con = DriverManager.getConnection(url, username, password);
                conList.set(con);/*from ww  w  . j  av a 2s .  c o  m*/
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        }
        return con;
    }
}

Related

  1. getConnection()
  2. getConnection()
  3. getConnection()
  4. getConnection()
  5. getConnection()
  6. getConnection()
  7. getConnection()
  8. getConnection(@Nonnull String connectUrl)
  9. getConnection(final String driver, final String url)