Java JDBC Connection Create getConnection()

Here you can find the source of getConnection()

Description

Creates a sql connection.

License

Open Source License

Exception

Parameter Description
Exception if any error occurs

Return

the created sql connection

Declaration

public static Connection getConnection() throws Exception 

Method Source Code


//package com.java2s;
import java.io.FileInputStream;

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

import java.util.Properties;

public class Main {
    /**//from ww w . j av a2s  .  co  m
     * Represents the database configure file path.
     */
    private static final String DATABASE_PROPERTY_FILE_PATH = "./config/application.properties";

    /**
     * Creates a sql connection.
     *
     * @return the created sql connection
     * @throws Exception if any error occurs
     */
    public static Connection getConnection() throws Exception {
        //gets connection properties from file database.properties
        Properties prop = new Properties();
        FileInputStream fis = null;
        try {
            fis = new FileInputStream(DATABASE_PROPERTY_FILE_PATH);
            prop.load(fis);
        } finally {
            if (fis != null) {
                fis.close();
            }
        }

        //creates the connection
        Class.forName(prop.getProperty("jdbc.driverClassName"));
        return DriverManager.getConnection(prop.getProperty("jdbc.url"), prop.getProperty("jdbc.username"),
                prop.getProperty("jdbc.password"));
    }
}

Related

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