Java JDBC MySQL Connection getConnection()

Here you can find the source of getConnection()

Description

get Connection

License

Open Source License

Declaration

public static Connection getConnection() 

Method Source Code


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

import java.sql.SQLException;

public class Main {
    public static final String url = "jdbc:mysql://127.0.0.1/myapp";
    public static final String user = "hyjapp";
    public static final String password = "123";
    public static final ThreadLocal<Connection> session = new ThreadLocal<Connection>();

    public static Connection getConnection() {
        Connection conn = session.get();
        if (conn == null) {
            try {
                conn = DriverManager.getConnection(url, user, password);
                conn.setAutoCommit(false);
                session.set(conn);//from   www .  j av  a 2  s.  c o m
            } catch (SQLException e) {
                e.printStackTrace();
            }

        }
        return conn;
    }
}

Related

  1. getConnection()
  2. getConnection()
  3. getConnection()
  4. getConnection()
  5. getConnection()
  6. getConnection()
  7. getConnection(final String host, final int port, final String schema, final String user, final String password)
  8. getConnection(String connectionType)
  9. getConnection(String dbFile)